
正文
wait/notify方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
执行wait方法会释放锁,执行notify不会释放锁
package com.qf.test05.pojo; /**
* @author qf
* @create 2018-09-18 10:41
*/
public class Service {
public void testMethod(Object lock){
try {
synchronized (lock){
System.out.println("begin wait");
lock.wait();
System.out.println("end wait");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类
package com.qf.test05.thread; import com.qf.test05.pojo.Service; /**
* @author qf
* @create 2018-09-18 10:43
*/
public class ThreadA extends Thread {
private Object lock; public ThreadA(Object lock) {
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
}
}
package com.qf.test05.thread; import com.qf.test05.pojo.Service; /**
* @author qf
* @create 2018-09-18 10:43
*/
public class ThreadB extends Thread {
private Object lock; public ThreadB(Object lock) {
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
}
}
测试运行
package com.qf.test05; import com.qf.test05.thread.ThreadA;
import com.qf.test05.thread.ThreadB; /**
* @author qf
* @create 2018-09-18 10:44
*/
public class Run {
public static void main(String[] args) {
Object lock = new Object();
ThreadA a = new ThreadA(lock);
a.start();
ThreadB b = new ThreadB(lock);
b.start();
}
}
控制台输出结果
begin wait
begin wait
证明了wait方法执行后会释放锁
========================================================================
package com.qf.test06.pojo; /**
* @author qf
* @create 2018-09-18 14:05
*/
public class Service {
public void testWait(Object lock){
try {
synchronized (lock){
System.out.println("线程名:"+Thread.currentThread().getName()+", begin wait time="+System.currentTimeMillis());
lock.wait();
System.out.println("线程名:"+Thread.currentThread().getName()+", --end wait time="+System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} public void testNotify(Object lock){
try {
synchronized (lock){
System.out.println("线程名:"+Thread.currentThread().getName()+", begin notify time="+System.currentTimeMillis());
lock.notify();
Thread.sleep(5000);
System.out.println("线程名:"+Thread.currentThread().getName()+", --end notify time="+System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类
package com.qf.test06.thread; import com.qf.test06.pojo.Service; /**
* @author qf
* @create 2018-09-18 14:07
*/
public class ThreadA extends Thread {
private Object lock; public ThreadA(Object lock) {
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testWait(lock);
}
}
package com.qf.test06.thread; import com.qf.test06.pojo.Service; /**
* @author qf
* @create 2018-09-18 14:11
*/
public class ThreadB extends Thread {
private Object lock; public ThreadB(Object lock) {
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testNotify(lock);
}
}
package com.qf.test06.thread; import com.qf.test06.pojo.Service; /**
* @author qf
* @create 2018-09-18 14:11
*/
public class ThreadC extends Thread {
private Object lock; public ThreadC(Object lock) {
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testNotify(lock);
}
}
测试运行
package com.qf.test06; import com.qf.test06.thread.ThreadA;
import com.qf.test06.thread.ThreadB;
import com.qf.test06.thread.ThreadC; /**
* @author qf
* @create 2018-09-18 14:13
*/
public class Run {
public static void main(String[] args) {
Object lock = new Object();
ThreadA a = new ThreadA(lock);
a.setName("A");
a.start();
ThreadB b = new ThreadB(lock);
b.setName("B");
b.start();
ThreadC c = new ThreadC(lock);
c.setName("C");
c.start();
}
}
打印结果
线程名: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方法执行后并不会释放锁







