首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

线程notify()解决方法

2012-01-29 
线程notify()package studentclass teacher{public synchronized void say(){System.out.println(在说话

线程notify()
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异常!、
请大虾看看!


[解决办法]
同步块中才能使用wait和notify
否则就是报这个错

热点排行