1. 程式人生 > >JAVA執行緒同步 waite,notify, notifyAll

JAVA執行緒同步 waite,notify, notifyAll

public class MyObject {
    private  static  Object lock = new Object();

    public void methidA(){
        synchronized (lock){
            for(int i = 0; i< 20; i++){
                System.out.println("methidA()");

                try {
                    if(10 == i){
                        lock.wait();//讓出CPU進入等待,釋放物件鎖
                    }
                    Thread.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public void methidB(){
        synchronized (lock){
            for(int i = 0; i< 20; i++){
                System.out.println("methidB()");
                try {
                    if(10 == i){
                        lock.notify();//喚醒一個處於等待該物件鎖的執行緒,然後往下執行
                    }
                    Thread.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public  static void main(String []args){
        MyObject myObject = new MyObject();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myObject.methidA();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                myObject.methidB();
            }
        }).start();
    }

}

輸出結果:

"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar=12708:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;C:\Users\LeeJH\IdeaProjects\untitled\out\production\untitled" MyObject
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidB()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()
methidA()

Process finished with exit code 0