java notify 的作用_java 为什么notify和notifyAll都不起作用?

每个线程初始化的时候都是 c=new C(),锁住的是不同的对象。

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()也没效果;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值