线程notify()

wodeshuangyuzuo 2011-10-12 08:32:22
package student;
class teacher{
public synchronized void say(){


System.out.println("在说话");
//Thread.sleep(100000);

try {
this.wait();
} catch (InterruptedException e) {

e.printStackTrace();
}
System.out.println("我醒了@");
}

public synchronized void sayhello(){
System.out.println("我在说hello");


}
}
class thread1 implements Runnable{
public static teacher tea=new teacher();
public void run(){


try {
tea.say();



} catch (Exception e) {

e.printStackTrace();
}

}



}

public class Demo{

public static void main(String[] args){


thread1 tre=new thread1();
new Thread(tre).start();
try {
Thread.sleep(1222);
} catch (InterruptedException e) {

e.printStackTrace();
}
thread1.tea.sayhello();
thread1.tea.notify();//这里提示错误了!


}

}
提示是:java.lang.IllegalMonitorStateException异常!、
请大虾看看!
...全文
24 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟祥月 2011-10-12
  • 打赏
  • 举报
回复
只有在同步块中才可以使用这两个函数
wodeshuangyuzuo 2011-10-12
  • 打赏
  • 举报
回复
谢谢知道了,忘记了
阳明 to life 2011-10-12
  • 打赏
  • 举报
回复
同步块中才能使用wait和notify
否则就是报这个错

62,614

社区成员

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

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