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