wrong. b.wait() is having the current thread wait on object b's lock. only when other thread (may be thread b) calls b.notify() or b.notifyAll can the current thread be awaken.
程序在加了一些输出之后,问题会更清楚一点public class ThreadA{ public static void main(String[] args) { ThreadB b=new ThreadB(); b.start(); System.out.println("b is start...."); synchronized(b)//括号里的b是什么意思,起什么作用? { try { System.out.println("Waiting for b to complete..."); b.wait();//这一句是什么意思,究竟让谁wait? System.out.println("waiting...."); } catch (InterruptedException e){} } System.out.println("Total is :"+b.total); }} class ThreadB extends Thread { int total; public void run() { System.out.println("ThreadB is running ...."); synchronized(this) { System.out.println("ThreadB is excuting for statement.."); for (int i=0;i<100 ;i++ ) { total +=i; System.out.println("total is "+total); } notify(); } } } 执行结果如下: b is start.... Waiting for b to complete... ThreadB is running .... ThreadB is excuting for statement.. total is 0 total is 1 total is 3 total is 6 total is 10 total is 15 total is 21 total is 28 total is 36 total is 45 total is 55 total is 66 total is 78 total is 91 total is 105 total is 120 total is 136 total is 153 total is 171 total is 190 total is 210 total is 231 total is 253 total is 276 total is 300 total is 325 total is 351 total is 378 total is 406 total is 435 total is 465 total is 496 total is 528 total is 561 total is 595 total is 630 total is 666 total is 703 total is 741 total is 780 total is 820 total is 861 total is 903 total is 946 total is 990 total is 1035 total is 1081 total is 1128 total is 1176 total is 1225 total is 1275 total is 1326 total is 1378 total is 1431 total is 1485 total is 1540 total is 1596 total is 1653 total is 1711 total is 1770 total is 1830 total is 1891 total is 1953 total is 2016 total is 2080 total is 2145 total is 2211 total is 2278 total is 2346 total is 2415 total is 2485 total is 2556 total is 2628 total is 2701 total is 2775 total is 2850 total is 2926 total is 3003 total is 3081 total is 3160 total is 3240 total is 3321 total is 3403 total is 3486 total is 3570 total is 3655 total is 3741 total is 3828 total is 3916 total is 4005 total is 4095 total is 4186 total is 4278 total is 4371 total is 4465 total is 4560 total is 4656 total is 4753 total is 4851 total is 4950 waiting.... Total is :4950
大家看一看这程序又是什么结果: public class ThreadA{ public static void main(String[] args) { ThreadB b=new ThreadB("Thread B"); ThreadB c=new ThreadB("Thread C"); System.out.println(b); b.start(); c.start(); System.out.println("b is start...."); synchronized(b)//括号里的b是什么意思,起什么作用? { try { System.out.println("Waiting for b to complete..."); b.wait();//这一句是什么意思,究竟让谁wait? System.out.println("waiting...."); } catch (InterruptedException e){} } System.out.println("Total is :"+b.total); }} class ThreadB extends Thread { int total; String st=""; ThreadB(String str) { this.st=str; } public void run() { System.out.println("ThreadB is running ...."); System.out.println(this); total=0;
synchronized(this) { System.out.println("ThreadB is excuting for statement.."); System.out.println(total); for (int i=0;i<100 ;i++ ) { total =i; System.out.println("total is "+total+"-----:"+st); } notify(); } } }
注意b.wait()这个wait()不是线程的wait(),而是线程对象b的wait()方法,即此wait()方法和后面的notify()方法都是属于Object类,而不是Thread类,它们作用于同一个对象b
b.wait() is having the current thread wait on object b's lock.
only when other thread (may be thread b) calls b.notify() or b.notifyAll can the current thread be awaken.
{
ThreadB b=new ThreadB();
b.start();
System.out.println("b is start....");
synchronized(b)//括号里的b是什么意思,起什么作用?
{
try
{
System.out.println("Waiting for b to complete...");
b.wait();//这一句是什么意思,究竟让谁wait?
System.out.println("waiting....");
}
catch (InterruptedException e){}
}
System.out.println("Total is :"+b.total);
}}
class ThreadB extends Thread
{
int total;
public void run()
{
System.out.println("ThreadB is running ....");
synchronized(this)
{ System.out.println("ThreadB is excuting for statement..");
for (int i=0;i<100 ;i++ )
{
total +=i;
System.out.println("total is "+total);
}
notify();
}
}
}
执行结果如下:
b is start....
Waiting for b to complete...
ThreadB is running ....
ThreadB is excuting for statement..
total is 0
total is 1
total is 3
total is 6
total is 10
total is 15
total is 21
total is 28
total is 36
total is 45
total is 55
total is 66
total is 78
total is 91
total is 105
total is 120
total is 136
total is 153
total is 171
total is 190
total is 210
total is 231
total is 253
total is 276
total is 300
total is 325
total is 351
total is 378
total is 406
total is 435
total is 465
total is 496
total is 528
total is 561
total is 595
total is 630
total is 666
total is 703
total is 741
total is 780
total is 820
total is 861
total is 903
total is 946
total is 990
total is 1035
total is 1081
total is 1128
total is 1176
total is 1225
total is 1275
total is 1326
total is 1378
total is 1431
total is 1485
total is 1540
total is 1596
total is 1653
total is 1711
total is 1770
total is 1830
total is 1891
total is 1953
total is 2016
total is 2080
total is 2145
total is 2211
total is 2278
total is 2346
total is 2415
total is 2485
total is 2556
total is 2628
total is 2701
total is 2775
total is 2850
total is 2926
total is 3003
total is 3081
total is 3160
total is 3240
total is 3321
total is 3403
total is 3486
total is 3570
total is 3655
total is 3741
total is 3828
total is 3916
total is 4005
total is 4095
total is 4186
total is 4278
total is 4371
total is 4465
total is 4560
total is 4656
total is 4753
total is 4851
total is 4950
waiting....
Total is :4950
b.wait()是让B来等待
this 代表以当前对象来进行同步
b.wait()是使当前线程阻塞,直到得到b的通知...
b线程中的notify()方法就是通知通知对象b,使b.wait()可以返回
public class ThreadA{ public static void main(String[] args)
{
ThreadB b=new ThreadB("Thread B");
ThreadB c=new ThreadB("Thread C");
System.out.println(b);
b.start();
c.start();
System.out.println("b is start....");
synchronized(b)//括号里的b是什么意思,起什么作用?
{
try
{
System.out.println("Waiting for b to complete...");
b.wait();//这一句是什么意思,究竟让谁wait?
System.out.println("waiting....");
}
catch (InterruptedException e){}
}
System.out.println("Total is :"+b.total);
}}
class ThreadB extends Thread
{
int total;
String st="";
ThreadB(String str)
{
this.st=str;
}
public void run()
{
System.out.println("ThreadB is running ....");
System.out.println(this);
total=0;
synchronized(this)
{ System.out.println("ThreadB is excuting for statement..");
System.out.println(total);
for (int i=0;i<100 ;i++ )
{
total =i;
System.out.println("total is "+total+"-----:"+st);
}
notify();
}
}
}
阻塞的是当前线程,b相当于一个信号量、资源或者说就是锁。要其他线程调用b.notify()或b.notifyAll()解开这吧锁,被阻塞的线程才能运行!
不好意思误人子弟了,可是core in java2 38页上面明显是这句话:
dummy.wait();//阻塞这个线程
难道是翻译错了?
的意思是定义一个同步块,使用b作为资源锁。
b.weit();的意思是让b休息。
的意思是定义一个同步块,使用b作为资源锁。
b.weit();的意思是临时释放锁,并组赛线程。好让其他使用同一把锁的线程有机会执行,这个线程在执行到一定地方后通知wait的线程继续执行。
典型的应用是通信中,一个线程A收数据,一个线程B处理数据,他们用同一把锁buf,当A发现buf满了时在同步块中调用wait(), 使B能得到锁并执行程序处理数据,当b处理一部分数据后调用notify()通知A继续执行接收数据的任务。当buf空时,B wait(), A notify().