会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132452个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2012楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 2013楼

map.get(u)值返回为null,而不是我put的值。但是我用比较器进行比较,结果又是正常的。

public class TreeMapTest {
    public static void main(String[] args) {
        Map<Users,String> map = new TreeMap<>();
        Users u1 = new Users("小a",18);
        Users u2 = new Users("小b",5);
        Users u3 = new Users("小c",9);

        String value = map.put(u1,"a");
        System.out.println(value);
        System.out.println(map.get(u1));
        map.put(u2,"b");
        map.put(u3,"c");

        Set<Users> keys = map.keySet();
        for(Users u: keys){
            System.out.println(u+"--- "+map.get(u));
        }
    }
}


public class Users implements Comparable<Users>{
    private String uname;
    private int uage;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public int getUage() {
        return uage;
    }

    public void setUage(int uage) {
        this.uage = uage;
    }

    public Users(String uname, int uage) {
        this.uname = uname;
        this.uage = uage;
    }

    public Users() {
    }


//如果大于返回1,小于返回-1 ,相等返回0
    @Override
    public int compareTo(Users o) {
        if(this.uage > o.uage){
            return 1;
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return "Users{" +
                "uname='" + uname + '\'' +
                ", uage=" + uage +
                '}';
    }
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2014楼

package com.bjsxt.ls.DuoXianCheng.线程并发;
/*
线程并发:生产者和消费者模型
定义一个做馒头和取馒头的类,中间有一个缓冲区。做好的馒头放入缓冲区,取馒头从缓冲区取。
假设缓冲区容量是10,那么当做了十个馒头的时候,做馒头线程就要停止(阻塞)。当缓冲区没有馒头的时候,取馒头线程就要停止(阻塞)。
但是当做馒头线程放入馒头的时候,就要用notify提醒取馒头线程,不要一直处于阻塞状态;
同样当取馒头线程拿馒头的时候,也要提醒做馒头线程要做馒头,不要一直处于阻塞状态;
*/

//定义馒头类
class ManTou{
    private int id;
    public ManTou(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
}

//定义缓冲区,用数组来存放馒头
class resitor{
    
    private ManTou[] arr =new ManTou[10]; //创建一个长度为10,类型为ManTou的数组
    private int index; //定义索引

    //定义做馒头方法
    //因为做馒头和取馒头都是对同样对象进行操作,所以这两个状态是要互斥,即同步的。所以要用synchronized使得这两个状态处于同步状态
    //synchronzied放在方法名上相当于将synchronized(this){}将方法体包裹起来。
    public synchronized void makeMantou(ManTou manTou){
        //用while做出判断,如果当数组满的时候,就要用wait方法,使得此线程进入阻塞状态,不再生产馒头了;
        while (this.index == this.arr.length){
            try {
                wait();    /*wait属于Object类只能用在synchronized块中。
                           此方法执行之后,在本方法所在的对象锁会被阻塞, 其他需要该对象锁的线程就可以继续运行了。*/
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }              //notify也属于Object类
        this.notify();//当放入馒头的时候,要用notify唤醒取馒头的线程,以防拿馒头线程处于阻塞状态 。
                     //该方法会唤醒处于等待状态队列中的一个线程
        this.arr[this.index]=manTou;
        index++;
    }

    //定义取馒头方法
    public synchronized ManTou takeMantou(){
        //用while判断,当索引为0,即缓冲区没有馒头的时候,就用wait阻塞此状态,不要再去取馒头了;
        while (this.index==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();//拿馒头的时候也要唤醒做馒头的线程,以防处于阻塞状态不做馒头了。
        this.index--;
        return this.arr[this.index];
    }
}


//创建做馒头线程
class makeThread extends Thread{
    private resitor r; //定义一个类型是resitor的变量r
    public makeThread(resitor r){
        this.r = r;
    }

    @Override
    public void run() {
        for (int i=1;i<11;i++){
            System.out.println("生成第"+i+"个馒头");
            ManTou manTou =new ManTou(i);
            this.r.push(manTou);
        }
    }
}

//创建取馒头线程
class takeThread extends Thread{
    private resitor r; //定义一个类型是resitor的变量r
    public takeThread(resitor r){
        this.r = r;
    }

    @Override
    public void run() {
        for (int i=1;i<11;i++){
            ManTou manTou =this.r.pop();
            System.out.println("拿走第"+i+"个馒头");
        }
    }
}

public class ProducerThread {
    public static void main(String[] args) {
        resitor tt =new resitor();
        new makeThread(tt).start();
        new takeThread(tt).start();
    }
}

QQ图片20210425162135.png

老师这是什么原因造成的?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2015楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2016楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2018楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2019楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2020楼

这一章节的文件拷贝实操总是报错,请老师看看,是需要我先创建文件吗?

package com.program;

import java.io.*;

public class CopyFileDirectory {
    public static void main(String[] args) {
        File src=new File("D:/a/java");

        File des=new File("E:/b");
       copyDir(src,des);



    }
    public static void copyDir(File SrcDir,File TargetDir) {
        if (TargetDir != null) {
            TargetDir.mkdir();//如果目的地的目录不存在,那么创建目录
        }
        File[] files = SrcDir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                copyFile(new File(SrcDir + "" + file.getName()), new File(TargetDir + "" + file.getName()));
            } else {
                copyDir(new File(SrcDir + "" + file.getName()), new File(TargetDir + "" + file.getName()));
            }
        }
    }

    public static void copyFile(File srcfile, File Desfile ){
        BufferedInputStream bis= null;
        BufferedOutputStream bos=null;
        try{
            bis=new BufferedInputStream(new FileInputStream(srcfile));
            bos=new BufferedOutputStream(new FileOutputStream(Desfile));
            byte[] by=new byte[1024];
            int temp=0;
            while((temp=bis.read(by))!=-1){
                bos.write(temp);
            }
            bos.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                if (bos!=null){
                    bos.close();
                }
                if (bis!=null){
                    bis.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2022楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2024楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 2025楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637