关于线程的一个问题

zhf0021 2005-03-14 07:16:11
public class ThreadA
{
public static void main(String[] args)
{
ThreadB b=new ThreadB();
b.start();
System.out.println("b is start....");
synchronized(b)
{
try
{
System.out.println("Waiting for b to complete...");
b.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
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<100;i++ )
{
total +=i;
System.out.println("total is "+total);
}
//notify(); 这里没有Notify
}
}
}

wait()的作用是这样说的,Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
但是这里没有对b Notify啊,为什么线程还是被唤醒了?
...全文
166 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhf0021 2005-04-09
  • 打赏
  • 举报
回复
up
LuckyBall 2005-03-15
  • 打赏
  • 举报
回复
第一个只有b一个线程,它的wait只是等待自身被执行完以后,就会自动往下执行
第二个有o,b两个线程,必须用notify或nitifyall唤醒,才能往下执行
改为:
class ThreadB extends Thread
{
Object o;
ThreadB(Object o) { this.o =o;}
int total;
public void run()
{
synchronized(o)
{
System.out.println("ThreadB is running..");
for (int i=0;i<100;i++ )
{
total +=i;
System.out.println("total is "+total);
}
o.notify();
}
}
}
即可
taglib 2005-03-15
  • 打赏
  • 举报
回复
>>>第一个只有b一个线程,它的wait只是等待自身被执行完以后,就会自动往下执行


不完全对,试下面例子

synchronized(this)
{
try
{
this.wait();
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}


我的感觉是假如线程对象做为synchronized的对象的话,该线程终止时,会自动发出signal或notify
zhf0021 2005-03-14
  • 打赏
  • 举报
回复
有没有这方面详细介绍的资料?
realaaaaa 2005-03-14
  • 打赏
  • 举报
回复
第一个
try
{
System.out.println("Waiting for b to complete...");
b.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
b已经结束就往下执行

try
{
System.out.println("Waiting for b to complete...");
o.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
System.out.println("Total is :"+b.total);
}
this.o没有指定等待的时间,就一直等下去

zhf0021 2005-03-14
  • 打赏
  • 举报
回复
public class ThreadA
{
public static void main(String[] args)
{
ThreadB b=new ThreadB();
b.start();
System.out.println("b is start....");
synchronized(b)
{
try
{
System.out.println("Waiting for b to complete...");
b.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
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<100;i++ )
{
total +=i;
System.out.println("total is "+total);
}
//notify(); 这里没有Notify
}
}
}
这个程序可以正常结束

public class ThreadA
{
public static void main(String[] args)
{
Object o = new Object();
ThreadB b=new ThreadB(o);
b.start();
System.out.println("b is start....");
synchronized(o)
{
try
{
System.out.println("Waiting for b to complete...");
o.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
System.out.println("Total is :"+b.total);
}
}


class ThreadB extends Thread
{
Object o;
ThreadB(Object o) { this.o =o;}
int total;
public void run()
{
synchronized(o)
{
System.out.println("ThreadB is running..");
for (int i=0;i<100;i++ )
{
total +=i;
System.out.println("total is "+total);
}
//o.notify(); //这里没有Notify
}
}
}

却不能,一个是用this,一个是用一个object,结果不一样,楼上的大哥解释一下啊
realaaaaa 2005-03-14
  • 打赏
  • 举报
回复
有什么问题呢。你们说说。。。。感觉正常的么。呵呵。
realaaaaa 2005-03-14
  • 打赏
  • 举报
回复
上面的b还是在运行,a在死等啊
zhf0021 2005-03-14
  • 打赏
  • 举报
回复
是啊,奇怪,怎么解释这个现象?
taglib 2005-03-14
  • 打赏
  • 举报
回复
线程对象有点怪,试试


public class ThreadA
{
public static void main(String[] args)
{
Object o = new Object();
ThreadB b=new ThreadB(o);
b.start();
System.out.println("b is start....");
synchronized(o)
{
try
{
System.out.println("Waiting for b to complete...");
o.wait();//没有被notify,它为什么还能被唤醒呢?
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}
System.out.println("Total is :"+b.total);
}
}


class ThreadB extends Thread
{
Object o;
ThreadB(Object o) { this.o =o;}
int total;
public void run()
{
synchronized(o)
{
System.out.println("ThreadB is running..");
for (int i=0;i<100;i++ )
{
total +=i;
System.out.println("total is "+total);
}
//o.notify(); //这里没有Notify
}
}
}
zhf0021 2005-03-14
  • 打赏
  • 举报
回复
我试过了,线程一直傻傻的等待,
taglib 2005-03-14
  • 打赏
  • 举报
回复
但假如谁也没有拥有这b对象呢?譬如你说,对下列情形,系统该怎么做?

synchronized(this)
{
try
{
this.wait();
System.out.println("Completed.Now back to main thread");
}catch (InterruptedException e){System.out.println("interrupted!");}
}

zhf0021 2005-03-14
  • 打赏
  • 举报
回复
是啊,主线程的synchronized(b)是在b运行完了之后再进入的,然后synchronized块里面有个b.wait(),此后,没有人Notify他,他怎么醒过来的呢?
taglib 2005-03-14
  • 打赏
  • 举报
回复
因为b是先运行的,在ThreadA里,synchronized(b)只有在b运行完了,才进入的

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧