当前位置: 首页 > 工具软件 > Tick Tock > 使用案例 >

【六】Java多线程之线程通信notify()、wait()、notifyAll()实现tick tock轮流输出

戚成礼
2023-12-01

notify()、wait()、notifyAll()这些方法只能在同步化环境中被调用

wait()会释放当前锁,允许其他线程使用对象,让出CPU。

notify()是通知一个线程获取锁

notifyAll()虽然所有的线程都被通知了。但是这些线程都会进行竞争,且只会有一个线程成功获取到锁,在这个线程没有执行完毕之前,其他的线程就必须等待了(只是这里不需要再notifyAll通知了,因为已经notifyAll了,只差获取锁了)

代码需要的结果是tick tock轮流输出

class TickTock {
    synchronized void tick(boolean running,int i){
        if(!running){
            System.out.println("Tick"+i+" running false notify");
            notify();
            return;
        }
        System.out.println("Tick"+i+" ");
        System.out.println("Tick"+i+" running true notify");
        notify();
        try{
            System.out.println("Tick"+i+" wait begin");
            wait();
            System.out.println("Tick"+i+" wait end");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    synchronized void tock(boolean running,int i){
        if(!running){
            System.out.println("Tock"+i+" running false notify");
            notify();
            return;
        }
        System.out.println("Tock"+i+" ");
        System.out.println("Tock"+i+" running true notify");
        notify();
        try{
            System.out.println("Tock"+i+" wait begin");
            wait();
            System.out.println("Tock"+i+" wait end");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
class WaitNotifyThread implements Runnable{
    TickTock tt;
    WaitNotifyThread(TickTock tt){

        this.tt = tt;
    }
    public void run() {
        if(Thread.currentThread().getName().equals("Tick")){
            for(int i = 0;i<5;i++){
                tt.tick(true,i);
            }
            tt.tock(false,-1);

        }else{
            for(int i = 0;i<5;i++){
                tt.tock(true,i);
            }
            tt.tick(false,-1);

        }
    }
}

public class WaitNotify{
    public static void main(String[] args) {
        TickTock tt = new TickTock();
        WaitNotifyThread r1 = new WaitNotifyThread(tt);
        WaitNotifyThread r2 = new WaitNotifyThread(tt);
        Thread t1 = new Thread(r1,"Tick");
        Thread t2 = new Thread(r2,"Tock");

        t1.start();
        t2.start();
        try {
        t1.join();

            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

F:\JDK\jdk1.8.0_91\bin\java.exe "-javaagent:F:\intellij\idea2018\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=54021:F:\intellij\idea2018\IntelliJ IDEA 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath F:\JDK\jdk1.8.0_91\jre\lib\charsets.jar;F:\JDK\jdk1.8.0_91\jre\lib\deploy.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\dnsns.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\jaccess.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\localedata.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\nashorn.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\ojdbc6.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\sunec.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;F:\JDK\jdk1.8.0_91\jre\lib\ext\zipfs.jar;F:\JDK\jdk1.8.0_91\jre\lib\javaws.jar;F:\JDK\jdk1.8.0_91\jre\lib\jce.jar;F:\JDK\jdk1.8.0_91\jre\lib\jfr.jar;F:\JDK\jdk1.8.0_91\jre\lib\jfxswt.jar;F:\JDK\jdk1.8.0_91\jre\lib\jsse.jar;F:\JDK\jdk1.8.0_91\jre\lib\management-agent.jar;F:\JDK\jdk1.8.0_91\jre\lib\plugin.jar;F:\JDK\jdk1.8.0_91\jre\lib\resources.jar;F:\JDK\jdk1.8.0_91\jre\lib\rt.jar;F:\myeclipseWorkSpace\thread-train\target\classes WaitNotify
Tick0 
Tick0 running true notify
Tick0 wait begin
Tock0 
Tock0 running true notify
Tock0 wait begin
Tick0 wait end
Tick1 
Tick1 running true notify
Tick1 wait begin
Tock0 wait end
Tock1 
Tock1 running true notify
Tock1 wait begin
Tick1 wait end
Tick2 
Tick2 running true notify
Tick2 wait begin
Tock1 wait end
Tock2 
Tock2 running true notify
Tock2 wait begin
Tick2 wait end
Tick3 
Tick3 running true notify
Tick3 wait begin
Tock2 wait end
Tock3 
Tock3 running true notify
Tock3 wait begin
Tick3 wait end
Tick4 
Tick4 running true notify
Tick4 wait begin
Tock3 wait end
Tock4 
Tock4 running true notify
Tock4 wait begin
Tick4 wait end
Tock-1 running false notify
Tock4 wait end
Tick-1 running false notify

Process finished with exit code 0

 

 类似资料: