猿问

关于Java多线程遇到的问题.

1.最近在学习Java多线程,看到视频中要实现一个类似闹钟和小明的情景, 要求闹钟响,小明关闹钟, 三秒后闹钟再响,小明再关, 重复10次程序结束. 不知道为什么我的程序小明只能输出一次.


public class Test {

    public static void main(String[] args) {

        Clock ck = new Clock();

        new XiaoMing(ck);

    }

}



public class Clock implements Runnable{

    public Boolean isAlarm;

    public Boolean shutdown;


    public Clock() {

        isAlarm = false;

        shutdown = false;

        new Thread(this).start();

    }


    @Override

    public void run() {

        for (int i = 0; i < 10; i++) {

            isAlarm = true;

            System.out.println("Get up * 3 !");

            synchronized (this) {

                notifyAll();

                try {

                    wait(3000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

        shutdown = true;

    }

}



public class XiaoMing implements Runnable{

    private Clock clk;


    public XiaoMing(Clock clk) {

        this.clk = clk;

        new Thread(this).start();

    }


    @Override

    public void run() {

        while(true) {

            if (clk.isAlarm) {

                System.out.println("Woshixiaoming!");

                clk.isAlarm = false;

                synchronized (this) {

                    try {

                        wait();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

            else{

                if(clk.shutdown)    break;

            }

        }

    }

}

输出结果如下


Get up * 3 !

Woshixiaoming!

Get up * 3 !

Get up * 3 !

...(10 次 Getup, 之后程序停在那里)


元芳怎么了
浏览 568回答 1
1回答

一只斗牛犬

这是因为你没有理解到wait(),notify(),notifyAll()这三个方法的真正作用,它们不是控制的所有线程,而是所对象的线程。也就是说,XiaoMing 的同步锁里调用的 wait() 方法阻塞的是 XiaoMing 这个线程;Clock 的同步锁唤醒的是 Clock 类的对象的线程,所以 XiaoMing 的对象的线程就一直阻塞下去了可以修改 XiaoMing 类run()方法的同步锁@Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; while(true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (clk.isAlarm) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Woshixiaoming!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clk.isAlarm = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (this.clk) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.clk.wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(clk.shutdown)&nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这样 XiaoMing 的线程会因为clk域被阻塞,直到Clock的线程调用notifyAll()
随时随地看视频慕课网APP

相关分类

Java
我要回答