当前位置 : 主页 > 编程语言 > java >

Java object wait notify notifyAll代码解析

来源:互联网 收集:自由互联 发布时间:2021-04-03
测试代码: public static Object loc=new Object(); public static void main(String[] args) throws Exception{ Thread t1=new Thread(new Runnable() { @Override public void run() { try { java.text.SimpleDateFormat sdf=new SimpleDateFormat("H

测试代码:

public static Object loc=new Object();

  public static void main(String[] args) throws Exception{

    Thread t1=new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
          System.out.println("["+sdf.format(new Date())+"] t1___等待锁...");
          synchronized (loc) {
            System.out.println("["+sdf.format(new Date())+"] t1___获得锁 ...");
            Thread.sleep(5000);
            System.out.println("["+sdf.format(new Date())+"] t1___loc..开始执行wait...");
            loc.wait();
            System.out.println("["+sdf.format(new Date())+"] t1___loc..执行wait后续...");
            Thread.sleep(1000);
          }
          System.out.println("["+sdf.format(new Date())+"] t1___loc..离开锁...");
        }catch (Exception e){
          e.printStackTrace();
        }
      }
    });
    Thread t3=new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
          System.out.println("["+sdf.format(new Date())+"] t3___等待锁...");
          synchronized (loc) {
            System.out.println("["+sdf.format(new Date())+"] t3___进入锁...");
            Thread.sleep(5000);
            System.out.println("["+sdf.format(new Date())+"] t3___loc..开始 wait...");
            loc.wait();
            System.out.println("["+sdf.format(new Date())+"] t3___loc..执行 wait后续...");
            Thread.sleep(1000);
          }
          System.out.println("["+sdf.format(new Date())+"] t3___离开锁...");
        }catch (Exception e){
          e.printStackTrace();
        }
      }
    });

    Thread t2=new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
          System.out.println("["+sdf.format(new Date())+"] t2___等待锁...");
          synchronized (loc) {
            System.out.println("["+sdf.format(new Date())+"] t2___获得锁...");
            Thread.sleep(5000);
            System.out.println("["+sdf.format(new Date())+"] t2___loc..开始执行 notify...");
            loc.notify();
            System.out.println("["+sdf.format(new Date())+"] t2___loc..执行 notify后续...");
            Thread.sleep(1000);
          }
          System.out.println("["+sdf.format(new Date())+"] t2___loc 离开锁....");
        }catch (Exception e){
          e.printStackTrace();
        }
      }
    });
    t1.start();
    t3.start();
    Thread.sleep(500);
    t2.start();


    System.out.println("t1___before join ....");
    t1.join();
    System.out.println("t2___before join ....");
    t2.join();
    System.out.println("t3____before join ....");
    t3.join();
    System.out.println("main exit....");
  }

执行结果:

[16:55:59 384] t1___等待锁...

[16:55:59 384] t1___获得锁 ...

[16:55:59 384] t3___等待锁...
t1___before join ....
[16:55:59 836] t2___等待锁...
[16:56:04 392] t1___loc..开始执行wait...

[16:56:04 392] t2___获得锁...
[16:56:09 392] t2___loc..开始执行 notify...
[16:56:09 392] t2___loc..执行 notify后续...
[16:56:10 392] t2___loc 离开锁....


[16:56:10 392] t3___进入锁...
[16:56:15 392] t3___loc..开始 wait...
[16:56:15 392] t1___loc..执行wait后续...
[16:56:16 392] t1___loc..离开锁...
t2___before join ....
t3____before join ....

总结:

1. 执行wait后‘'暂时‘ 释放当前对象锁给其他线程,当前线程处于等待状态

2. syn块中的wait收到notify通知后 唤醒cpu 继续判断锁状态

3. 执行notify且当前的对象锁释放后 wait等待的线程激活

4. notifyAll 是一次唤醒所有的wait

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

网友评论