⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notify.java

📁 这是java 2应用开发指南这本书上所有例子的源代码
💻 JAVA
字号:
import java.lang.*;
public class notify {
  public notify() {
  }
  public static void main(String[] args) {
    Object obj=new Object();
    Thread wait1=new Thread(new LockWait(obj));
    Thread wait2=new Thread(new LockWait(obj));
    Thread notify1=new Thread(new LockNotify(obj));
    wait1.start();
    wait2.start();
    notify1.start();
  }
}
class LockWait implements Runnable{
  private Object obj;
  public LockWait(Object obj){
    this.obj=obj;
  }
  public void run(){
    synchronized(obj){
      try{
        System.out.println(Thread.currentThread().getName()+"  is waiting ");
        obj.wait();
        System.out.println(Thread.currentThread().getName() +" woke up ");
      }
      catch (InterruptedException e){
      }
    }
  }
}
class LockNotify implements Runnable{
  Object obj;
  public LockNotify(Object obj){
    this.obj=obj;
  }
  public void run(){
    synchronized(obj){
      System.out.println(Thread.currentThread().getName()+"  will wake up some thread ");
      obj.notify();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -