会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132373个问题

为啥我的每次运行结果都一模一样???

public class haunchongThread {
    public static void main(String[] args) {
        Zaocarcahng zaocar = new Zaocarcahng();
        Thread scz = new Thread(new scz(zaocar));
        Thread xfz = new Thread(new xfz(zaocar));
        scz.start();
        xfz.start();
    }
}

//缓冲区
class Zaocarcahng {
    ArrayList c = new ArrayList<>(10);
    //存车
    synchronized public void  scc(Ccar ccar, String s){
        while (c.size()>=10){//如果到10了就满了,休息
            try {
                this.wait();//线程休息
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }//到这里线程还在休息,无法执行下面的唤醒,所以没有冲突
        this.notify();//唤醒
        c.add(ccar);
        System.out.println(s+"生产了"+ccar.toString());
    }
    //取车
    synchronized public Ccar  xsc(int ccarid){
        if (c.size()<=0){//车不够了,等着休息
            try {
                this.wait();//线程休息
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }//到这里线程还在休息,无法执行下面的唤醒,所以没有冲突
        this.notify();//唤醒
        return (Ccar) c.remove(ccarid);
    }
}

//产物
class Ccar {
    private int id ;


    public Ccar() {
    }

    public Ccar(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Ccar{" +
                "id=" + id +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}


//生产者
class scz implements Runnable{

    Zaocarcahng zcc;
    public scz(Zaocarcahng zcc) {
        this.zcc = zcc;
    }

    @Override
    public void run() {
        //生产
        for (int i = 0; i < 20; i++) {
            zcc.scc(new Ccar(i),"生产者----");


        }


    }
}

//消费者
class xfz implements Runnable{
    Zaocarcahng zcc;
    public xfz(Zaocarcahng zcc) {
        this.zcc = zcc;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            Ccar xsc = zcc.xsc(0);//只取第1个
            System.out.println("消费:"+xsc.toString());
        }

    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 3467楼

package com.bjsxt.file;
import java.io.File;
import java.io.IOException;

public class TestFile {
    //使用File类操作文件
    public static void main(String[] args) throws IOException {
        //创建File类的对象
        //File f1 = new File("\\Users\\wanglx\\Desktop\\测试图片\\140F1132T93X.txt");//绝对路径
        File f2 = new File ( "/Users/wanglx/Desktop/测试图片/140F1132T93X.txt" );
        File f3 = new File ( "140F1132T93X.txt" );//相对路径
        File f4 = new File ("/Users/wanglx/Desktop/测试图片");//目录
        File f5 = new File ( f4,"140F1132T93X.txt" );//目录下的某个文件
        File f6 = new File ( "Desktop"+File.separator+"140F1132T93X.txt" );//获取系统相关的路径分隔符separator
        /**File操作文件相关的方法*/
        System.out.println (f2.createNewFile ());
       // System.out.println (f2.delete ());//直接从磁盘上删除
        System.out.println (f2.exists ());//不存在返回false
        System.out.println ("绝对路径:"+f3.getAbsolutePath ());
        System.out.println ("相对路径:"+f3.getPath ());
        System.out.println ("获取文件名:"+f3.getName ());
        System.out.println (f3.toString ());//相对路径
        System.out.println ("f3是否是文件:"+f3.isFile ());
        System.out.println ("是否是文件:"+f4.isFile ());

    }
}


 System.out.println ("f3是否是文件:"+f3.isFile ());

返回结果:false

为什么呢

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3468楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 3469楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 3471楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3472楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 3473楼

public class AutoBaoZhaung {
    private static  int value;
    private static AutoBaoZhaung[] shu;
    static int LOW = -128;
    static int HIGH = 127;
    {
        shu = new AutoBaoZhaung[HIGH-LOW+1];
        for(int i = LOW ; i < HIGH ; i++){
            shu[i-LOW] = new AutoBaoZhaung(i);
        }
        System.out.println(Arrays.toString(shu));
    }
    @Override
    public String toString() {
        return value+"";
    }
    private static AutoBaoZhaung valueOf(int k){
        if(k<HIGH&&k>LOW){
            return shu[k-LOW];
        }
        return new AutoBaoZhaung(k);
    }
    public  AutoBaoZhaung(int value){
        this.value = value;
    }
    private int intValue(){
        return value;
    }

    public static void main(String[] args) {
        AutoBaoZhaung a = AutoBaoZhaung.valueOf(100);
        AutoBaoZhaung a1 = AutoBaoZhaung.valueOf(100);
        AutoBaoZhaung a2= AutoBaoZhaung.valueOf(1000);
        AutoBaoZhaung a3 = AutoBaoZhaung.valueOf(1000);
        System.out.println(a==a1);
        System.out.println(a2==a3);
        System.out.println(a.equals(a1));
        System.out.println(a2.equals(a3));

    }
}

image.png

老师,这里为什么会报空指针异常

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3474楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 3479楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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