zoukankan      html  css  js  c++  java
  • wait/notify方法

    执行wait方法会释放锁,执行notify不会释放锁

     1 package com.qf.test05.pojo;
     2 
     3 /**
     4  * @author qf
     5  * @create 2018-09-18 10:41
     6  */
     7 public class Service {
     8     public void testMethod(Object lock){
     9         try {
    10             synchronized (lock){
    11                 System.out.println("begin wait");
    12                 lock.wait();
    13                 System.out.println("end wait");
    14             }
    15         } catch (InterruptedException e) {
    16             e.printStackTrace();
    17         }
    18     }
    19 }

    线程类

     1 package com.qf.test05.thread;
     2 
     3 import com.qf.test05.pojo.Service;
     4 
     5 /**
     6  * @author qf
     7  * @create 2018-09-18 10:43
     8  */
     9 public class ThreadA extends Thread {
    10     private Object lock;
    11 
    12     public ThreadA(Object lock) {
    13         this.lock = lock;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         Service service = new Service();
    19         service.testMethod(lock);
    20     }
    21 }
     1 package com.qf.test05.thread;
     2 
     3 import com.qf.test05.pojo.Service;
     4 
     5 /**
     6  * @author qf
     7  * @create 2018-09-18 10:43
     8  */
     9 public class ThreadB extends Thread {
    10     private Object lock;
    11 
    12     public ThreadB(Object lock) {
    13         this.lock = lock;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         Service service = new Service();
    19         service.testMethod(lock);
    20     }
    21 }

    测试运行

     1 package com.qf.test05;
     2 
     3 import com.qf.test05.thread.ThreadA;
     4 import com.qf.test05.thread.ThreadB;
     5 
     6 /**
     7  * @author qf
     8  * @create 2018-09-18 10:44
     9  */
    10 public class Run {
    11     public static void main(String[] args) {
    12         Object lock = new Object();
    13         ThreadA a = new ThreadA(lock);
    14         a.start();
    15         ThreadB b = new ThreadB(lock);
    16         b.start();
    17     }
    18 }

    控制台输出结果

    begin wait
    begin wait

    证明了wait方法执行后会释放锁

    ========================================================================

     1 package com.qf.test06.pojo;
     2 
     3 /**
     4  * @author qf
     5  * @create 2018-09-18 14:05
     6  */
     7 public class Service {
     8     public void testWait(Object lock){
     9         try {
    10             synchronized (lock){
    11                 System.out.println("线程名:"+Thread.currentThread().getName()+", begin wait time="+System.currentTimeMillis());
    12                 lock.wait();
    13                 System.out.println("线程名:"+Thread.currentThread().getName()+", --end wait time="+System.currentTimeMillis());
    14             }
    15         } catch (InterruptedException e) {
    16             e.printStackTrace();
    17         }
    18     }
    19 
    20     public void testNotify(Object lock){
    21         try {
    22             synchronized (lock){
    23                 System.out.println("线程名:"+Thread.currentThread().getName()+", begin notify time="+System.currentTimeMillis());
    24                 lock.notify();
    25                 Thread.sleep(5000);
    26                 System.out.println("线程名:"+Thread.currentThread().getName()+", --end notify time="+System.currentTimeMillis());
    27             }
    28         } catch (InterruptedException e) {
    29             e.printStackTrace();
    30         }
    31     }
    32 }

    线程类

     1 package com.qf.test06.thread;
     2 
     3 import com.qf.test06.pojo.Service;
     4 
     5 /**
     6  * @author qf
     7  * @create 2018-09-18 14:07
     8  */
     9 public class ThreadA extends Thread {
    10     private Object lock;
    11 
    12     public ThreadA(Object lock) {
    13         this.lock = lock;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         Service service = new Service();
    19         service.testWait(lock);
    20     }
    21 }
     1 package com.qf.test06.thread;
     2 
     3 import com.qf.test06.pojo.Service;
     4 
     5 /**
     6  * @author qf
     7  * @create 2018-09-18 14:11
     8  */
     9 public class ThreadB extends Thread {
    10     private Object lock;
    11 
    12     public ThreadB(Object lock) {
    13         this.lock = lock;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         Service service = new Service();
    19         service.testNotify(lock);
    20     }
    21 }
     1 package com.qf.test06.thread;
     2 
     3 import com.qf.test06.pojo.Service;
     4 
     5 /**
     6  * @author qf
     7  * @create 2018-09-18 14:11
     8  */
     9 public class ThreadC extends Thread {
    10     private Object lock;
    11 
    12     public ThreadC(Object lock) {
    13         this.lock = lock;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         Service service = new Service();
    19         service.testNotify(lock);
    20     }
    21 }

    测试运行

     1 package com.qf.test06;
     2 
     3 import com.qf.test06.thread.ThreadA;
     4 import com.qf.test06.thread.ThreadB;
     5 import com.qf.test06.thread.ThreadC;
     6 
     7 /**
     8  * @author qf
     9  * @create 2018-09-18 14:13
    10  */
    11 public class Run {
    12     public static void main(String[] args) {
    13         Object lock = new Object();
    14         ThreadA a = new ThreadA(lock);
    15         a.setName("A");
    16         a.start();
    17         ThreadB b = new ThreadB(lock);
    18         b.setName("B");
    19         b.start();
    20         ThreadC c = new ThreadC(lock);
    21         c.setName("C");
    22         c.start();
    23     }
    24 }

    打印结果

    线程名:A, begin wait time=1537252123977
    线程名:B, begin notify time=1537252123978
    线程名:B, --end notify time=1537252128978
    线程名:A, --end wait time=1537252128978
    线程名:C, begin notify time=1537252128978
    线程名:C, --end notify time=1537252133978

    证明了notify方法执行后并不会释放锁

  • 相关阅读:
    Hexo+Github 搭建一个自己的博客
    vue中sessionStorage存储的用法和问题
    vue 页面刷新
    vue渲染完页面后div滚动条定位在底部
    vue 定义全局函数
    vue filter过滤器用法
    vue中bus.$on事件被多次绑定
    vue中引入jQuery的方法
    vue2.0传值方式:父传子、子传父、非父子组件、路由跳转传参
    vue打包后显示为空白页的解决办法
  • 原文地址:https://www.cnblogs.com/qf123/p/9668717.html
Copyright © 2011-2022 走看看