在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ java兩個線程間隔遞增打印數(shù)字 (synchronized wait noti

java兩個線程間隔遞增打印數(shù)字 (synchronized wait notify的使用)

如下代碼
package liupeng.cn.Algorithm.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;



public class ThreadTest {
    public static int counter  = 0;
    public static Integer lock = 0;;
    public static void main(String ... arg){
        final Semaphore sp = new Semaphore(2);
        Thread t1 = new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (lock) {
                        try {
                            while(lock%2==0){
                                lock.wait();
                            }
                            System.out.println("1 "+lock);
                            lock+=1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        lock.notify();
                    }
                }
                
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (lock) {
                        try {
                            while(lock%2==1){
                                lock.wait();
                            }
                            System.out.println("2 "+lock);
                            lock+=1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        lock.notify();
                    }
                }
            }
        };
        t1.start();
        t2.start();
        
    }
}
輸出報錯,求教為什么會這樣,感覺沒有地方錯?。。?!
2 0
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at liupeng.cn.Algorithm.thread.ThreadTest$2.run(ThreadTest.java:45)
回答
編輯回答
笨小蛋
public class ThreadTest {
    public static int counter  = 0;
    public static Integer lock = 0;;
    public static void main(String ... arg){
        Thread t1 = new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (lock) {
                          lock.notify();
                        try {
                           System.out.println("thread" + Thread.currentThread().getId() +  " print " + i);
                           lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                       
                    }
                }
                
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (lock) {
                        lock.notify();
                        try {
                            System.out.println("thread" + Thread.currentThread().getId() +  " print " + i);
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        t1.start();
        t2.start();
        
    }
}

你在lock對象上面上鎖,但是你居然修改lock的值。

你的代碼為報錯原因是:

This method(notify/wait) should only be called by a thread that is the owner of this object's monitor.
hread t1 = new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (lock) {//獲取lock=0的對象的監(jiān)視器
                        try {
                            while(lock%2==0){
                                lock.wait();
                            }
                            System.out.println("1 "+lock);
                            lock+=1;//你居然修改了這個對象
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        lock.notify();//此時lock的值不為0了,但是該線程擁有的是lock=0的對象監(jiān)視器,這個時候調(diào)用notify方法就報錯了,因為notify必須使用線程擁有的監(jiān)視器對應(yīng)對象調(diào)用。
                    }
                }
                
            }
        };
2017年4月16日 20:41