线程的wait()问题

sun88sz 2010-03-10 03:34:50

public class ThreadTest extends Thread {

public void run() {
while(true){
System.out.println(this.getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception {
ThreadTest t = new ThreadTest();
t.start();

t.wait();//这样是错的
}
}


能否在main线程中调用其他线程的wait(),使之等待
是否wait()只能由自身线程调用?

那想要调用wait()来实现使其他线程等待,那要如何实现呢?(不在while()中用标示)
...全文
255 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
sun88sz 2010-03-11
  • 打赏
  • 举报
回复
我的意思仅仅是能否在A线程中调用B线程的wait()方法,使B线程等待?
autumn1987 2010-03-11
  • 打赏
  • 举报
回复
支持20楼的 学习……
cool_scorpion 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 sun88sz 的回复:]
我的意思仅仅是能否在A线程中调用B线程的wait()方法,使B线程等待?
[/Quote]

B线程要阻塞,为什么要用A线程来掉呢?完全可以B线程自己调用wait嘛。
cool_scorpion 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 sun88sz 的回复:]
我的意思仅仅是能否在A线程中调用B线程的wait()方法,使B线程等待?
[/Quote]



看api文档:
wait()
Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

wait()方法只能让当前线程阻塞,你在在A线程中调用B线程的wait()方法,结果还是A线程阻塞。
在A线程中调用任何对象的wait()都是使A线程阻塞
sun88sz 2010-03-11
  • 打赏
  • 举报
回复
哎~咋地都没人来了~~~~~~~~
waaadoooo 2010-03-10
  • 打赏
  • 举报
回复
刚学Java,高人很多啊,多学习呢
sun88sz 2010-03-10
  • 打赏
  • 举报
回复
也就是说无法A线程中把B线程给wait()?
wait()只能作用在执行这个方法的线程上,而非这个方法属于的那个线程?
notlogin 2010-03-10
  • 打赏
  • 举报
回复
引用 2 楼 wzju64676266 的回复:
wait方法是会释放锁,如果你的方法不是同步的,那线程根本就不会获得锁,何来释放锁之说呢!notify(),notifyAll()也都要在synchronized里面!不这么做的话你也可以使用sleep去代替wait

oooooooooooooooooooooooooooooooooo
wzju64676266 2010-03-10
  • 打赏
  • 举报
回复

public class ThreadTest extends Thread {

public void run() {
while(true){

System.out.println(this.getName());
this.pause();
}
}

public synchronized void pause(){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) throws InterruptedException {
ThreadTest t = new ThreadTest();
t.start();

t.pause();
}
}

sun88sz 2010-03-10
  • 打赏
  • 举报
回复
其实我的意思就是能否在A线程中把B线程给wait()了~
huajia136651 2010-03-10
  • 打赏
  • 举报
回复
引用 9 楼 sun88sz 的回复:
这样?也不行啊Java codepublicclass ThreadTestextends Thread {publicvoid run() {while(true){
System.out.println(this.getName());try {
Thread.sleep(500);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}publicsynchronizedvoid pause(){try {this.wait();
}catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();
}
}publicstaticvoid main(String[] args)throws InterruptedException {
ThreadTest t=new ThreadTest();
t.start();

t.pause();
}
}
你这个好像是wait住了主线程了,而不是那个t,应该放在 t的run函数里才可以吧。共同学习啊,我也刚学java
huajia136651 2010-03-10
  • 打赏
  • 举报
回复
我看到书上有个例子是这样的,在线程类中加入个Flag,然后if(flag)就wait,否则不wait,然后再在线程类中加入一个设置flag的函数,和一个notify这个线程的函数,来完成 挂起与唤醒的。
huajia136651 2010-03-10
  • 打赏
  • 举报
回复
我也是刚学Java,看到多线程这一块,为什么LZ非要把wait放在线程类中呢。我看的例子都是放在公用的资源块中的吧。而且得放一个notify吧,不然怎么取消等待呢。感觉我们还是应该先理解下书本啊。
sun88sz 2010-03-10
  • 打赏
  • 举报
回复
这样?也不行啊

public class ThreadTest extends Thread {

public void run() {
while(true){
System.out.println(this.getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public synchronized void pause(){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) throws InterruptedException {
ThreadTest t = new ThreadTest();
t.start();

t.pause();
}
}
xllee 2010-03-10
  • 打赏
  • 举报
回复
引用 2 楼 wzju64676266 的回复:
wait方法是会释放锁,如果你的方法不是同步的,那线程根本就不会获得锁,何来释放锁之说呢!notify(),notifyAll()也都要在synchronized里面!不这么做的话你也可以使用sleep去代替wait


正解
ychao0417 2010-03-10
  • 打赏
  • 举报
回复
没有synchronized,不能wait()
可以sleep()
dinghun8leech 2010-03-10
  • 打赏
  • 举报
回复
引用 2 楼 wzju64676266 的回复:
wait方法是会释放锁,如果你的方法不是同步的,那线程根本就不会获得锁,何来释放锁之说呢!notify(),notifyAll()也都要在synchronized里面!不这么做的话你也可以使用sleep去代替wait

学习了
顺便引一篇相关的老帖,看5楼的回答。
http://topic.csdn.net/t/20030902/02/2213119.html
yukang_ky 2010-03-10
  • 打赏
  • 举报
回复
引用 1 楼 wzju64676266 的回复:
t.wait();//这样是错的
wait一个要在同步方法或者同步块里面synchronized


楼上正解
hanwangyu 2010-03-10
  • 打赏
  • 举报
回复
wait方法是会释放锁,如果你的方法不是同步的,那线程根本就不会获得锁,何来释放锁之说呢!notify(),notifyAll()也都要在synchronized里面!不这么做的话你也可以使用sleep去代替wait



这种说法更好说明了sleep与wait用法的区别
梦_枫 2010-03-10
  • 打赏
  • 举报
回复
引用 1 楼 wzju64676266 的回复:
t.wait();//这样是错的
wait一个要在同步方法或者同步块里面synchronized


正解
加载更多回复(2)

62,615

社区成员

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

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