• 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方法执行后并不会释放锁

  • 相关阅读:
    DS4700磁盘阵列的控制器微码升级操作记录(收录百度文库)
    Android 解决布局无法对齐的情况
    android 模仿大众点评团购卷列表多余3条时折叠,点击时显示剩余全部的功能
    android 解决ScrollView中的子布局不能够填充整个ScrollView的情况。
    Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。
    android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法
    Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用
    Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
    Android2.3系统 自定义的PopupWindow在实例化时报空指针异常
    android精品开源项目整理
  • 原文地址:https://www.cnblogs.com/qf123/p/9668717.html
Copyright © 2020-2023  润新知