- public class Tset
-
- {
-
- public static void main(String[] args) throws InterruptedException
-
- {
- ThreadB b=new ThreadB();
-
- b.start();
-
- System.out.println("b is start....");
-
- synchronized(b)
-
- {
- System.out.println("Waiting for b to complete...");
- b.wait();
- System.out.println("Completed.Now back to main thread");
-
- }
-
- System.out.println("Total is :"+b.total);
-
- }
-
- }
-
- class ThreadB extends Thread
-
- {
-
- int total;
-
- public void run()
-
- {
-
- synchronized(this)
-
- {
-
- System.out.println("ThreadB is running..");
-
- for (int i=0;i<5;i++ )
-
- {
- total +=i;
- System.out.println("total is "+total);
- }
- notify();
- }
-
- }
-
- }
复制代码 运行结果怎么会出现
b is start....
ThreadB is running..
total is 0
total is 1
total is 3
total is 6
total is 10
Waiting for b to complete...
然后主线程无法获得锁 一直等待。。
正常情况不是应该主线程wait 然后睡眠,等线程B执行完之后 notify,主线程再执行吗?
|
|