关键字synchronized与wait()/notify()这两个方法一起使用可以实现等待/通知模式, Lock锁的newContition()方法返回Condition对象,Condition类也可以实现等待/通知模式。
用notify()通知时,JVM会随机唤醒某个等待的线程, 使用Condition类可以进行选择性通知, Condition比较常用的两个方法:
● await()会使当前线程等待,同时会释放锁,当其他线程调用signal()时,线程会重新获得锁并继续执行。
● signal()用于唤醒一个等待的线程。
注意:在调用Condition的await()/signal()方法前,也需要线程持有相关的Lock锁,调用await()后线程会释放这个锁,在singal()调用后会从当前Condition对象的等待队列中,唤醒 一个线程,唤醒的线程尝试获得锁, 一旦获得锁成功就继续执行。
package com.wkcto.lock.condition;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Condition等待与通知
*/
public class Test01 {
//定义锁
static Lock lock = new ReentrantLock();
//获得Condtion对象
static Condition condition = lock.newCondition();
//定义线程子类
static class SubThread extends Thread{
@Override
public void run() {
try {
lock.lock(); //在调用await()前必须先获得锁
System.out.println("method lock");
condition.await(); //等待
System.out.println("method await");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
System.out.println("method unlock");
}
}
}
public static void main(String[] args) throws InterruptedException {
SubThread t = new SubThread();
t.start();
//子线程启动后,会转入等待状态
Thread.sleep(3000);
//主线程在睡眠3秒后,唤醒子线程的等待
try {
lock.lock();
condition.signal();
} finally {
lock.unlock();
}
}
}
package com.wkcto.lock.condition;
import java.io.PipedOutputStream;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 多个Condition实现通知部分线程, 使用更灵活
*/
public class Test02 {
static class Service{
private ReentrantLock lock = new ReentrantLock(); //定义锁对象
//定义两个Condtion对象
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
//定义方法,使用conditionA等待
public void waitMethodA(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " begin wait:" + System.currentTimeMillis());
conditionA.await(); //等待
System.out.println(Thread.currentThread().getName() + " end wait:" + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
//定义方法,使用conditionB等待
public void waitMethodB(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " begin wait:" + System.currentTimeMillis());
conditionB.await(); //等待
System.out.println(Thread.currentThread().getName() + " end wait:" + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
//定义方法唤醒conditionA对象上的等待
public void signalA(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " sigal A time = " + System.currentTimeMillis());
conditionA.signal();
System.out.println(Thread.currentThread().getName() + " sigal A time = " + System.currentTimeMillis());
} finally {
lock.unlock();
}
}
//定义方法唤醒conditionB对象上的等待
public void signalB(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " sigal A time = " + System.currentTimeMillis());
conditionB.signal();
System.out.println(Thread.currentThread().getName() + " sigal A time = " + System.currentTimeMillis());
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
Service service = new Service();
//开启两个线程,分别调用waitMethodA(),waitMethodB()方法
new Thread(new Runnable() {
@Override
public void run() {
service.waitMethodA();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
service.waitMethodB();
}
}).start();
Thread.sleep(3000); //main线程睡眠3秒
// service.signalA(); //唤醒 conditionA对象上的等待,conditionB上的等待依然继续等待
service.signalB();
}
}
package com.wkcto.lock.condition;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 使用Condition实现生产者/消费者设计模式, 两个 线程交替打印
*/
public class Test03 {
static class MyService{
private Lock lock = new ReentrantLock(); //创建锁对象
private Condition condition = lock.newCondition(); //创建Condition对象
private boolean flag = true; //定义交替打印标志
//定义方法只打印----横线
public void printOne(){
try {
lock.lock(); //锁定
while (flag){ //当flag为true等待
System.out.println(Thread.currentThread().getName() + " waiting...");
condition.await();
}
//flag为false时打印
System.out.println(Thread.currentThread().getName() + " ---------------- ");
flag = true; //修改交替打印标志
condition.signal(); //通知另外的线程打印
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁对象
}
}
//定义方法只打印***横线
public void printTwo(){
try {
lock.lock(); //锁定
while (!flag){ //当flag为false等待
System.out.println(Thread.currentThread().getName() + " waiting...");
condition.await();
}
//flag为true时打印
System.out.println(Thread.currentThread().getName() + " ****** ");
flag = false; //修改交替打印标志
condition.signal(); //通知另外的线程打印
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁对象
}
}
}
public static void main(String[] args) {
MyService myService = new MyService();
//创建线程打印--
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
myService.printOne();
}
}
}).start();
//创建线程打印**
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
myService.printTwo();
}
}
}).start();
}
}