84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
光阴似箭催人老,日月如移越少年。
Each thread is initialized with c=new C() and locks different objects.
class Example{ public static void main(String arr[]){ C c = new C(); ThreadB th1=new ThreadB("th1",c); ThreadB th2=new ThreadB("th2",c); } } class ThreadB implements Runnable{ C c; Thread thread; ThreadB(String name,C c){ //c=new C(); this.c=c; thread=new Thread(this,name); thread.start(); } public void run(){ if(thread.getName().equals("th1")){ for(int i=0;i<3;i++)c.t1(false); c.t1(true); } if(thread.getName().equals("th2")){ for(int i=0;i<3;i++)c.t2(false); c.t2(true); } System.out.println("end"); } } class C{ synchronized void t1(boolean boo){ if(boo){ notify(); return; } System.out.println("t1"); notify(); try{ wait(); }catch(InterruptedException exc){System.out.println(exc);} } synchronized void t2(boolean boo){ if(boo){ System.out.println();notify();return; } System.out.println("t2"); notify();//notifyAll()也没效果; } }
The wait object C of these two threads is not the same, and they cannot sense the other party’s lock
Each thread is initialized with c=new C() and locks different objects.
The wait object C of these two threads is not the same, and they cannot sense the other party’s lock