首先看一个使用Condition的简单示例
package org.iti.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadProConsumerII {
public static void main(String args[]) {
Basket basket = new Basket();
for (int i = 0; i < 15; i++) {
new Thread(new Add(basket)).start();
new Thread(new Get(basket)).start();
}
}
}
class Add implements Runnable {
private Basket basket;
private Object apple = new Object();
public Add(Basket basket) {
this.basket = basket;
}
public void run() {
basket.putApple(apple);
}
}
class Get implements Runnable {
private Basket basket;
public Get(Basket basket) {
this.basket = basket;
}
public void run() {
basket.getApple();
}
}
class Basket {
List<Object> apples = new ArrayList<Object>();
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public Object getApple() {
lock.lock();
try {
while (apples.size() == 0) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object apple = apples.get(0);
apples.remove(apple);
condition.signal();
System.out.println("拿到苹果");
return apple;
} finally {
lock.unlock();
}
}
public void putApple(Object apple) {
lock.lock();
try {
while (apples.size() > 3) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
apples.add(apple);
condition.signal();
System.out.println("放入苹果");
} finally {
lock.unlock();
}
}
}
Condition和传统的线程通信没什么区别,Condition的强大之处在于它可以绑定多个条件,再看看下面的代码:
package org.iti.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadProConsumerII {
public static void main(String args[]) {
Basket basket = new Basket();
for (int i = 0; i < 15; i++) {
new Thread(new Add(basket)).start();
new Thread(new Get(basket)).start();
}
}
}
class Add implements Runnable {
private Basket basket;
private Object apple = new Object();
public Add(Basket basket) {
this.basket = basket;
}
public void run() {
basket.putApple(apple);
}
}
class Get implements Runnable {
private Basket basket;
public Get(Basket basket) {
this.basket = basket;
}
public void run() {
basket.getApple();
}
}
class Basket {
List<Object> apples = new ArrayList<Object>();
Lock lock = new ReentrantLock();
Condition get = lock.newCondition();
Condition put = lock.newCondition();
public Object getApple() {
lock.lock();
try {
while (apples.size() == 0) {
try {
get.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object apple = apples.get(0);
apples.remove(apple);
put.signal();
System.out.println("拿到苹果");
return apple;
} finally {
lock.unlock();
}
}
public void putApple(Object apple) {
lock.lock();
try {
while (apples.size() >= 2) {
try {
put.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
apples.add(apple);
get.signal();
System.out.println("放入苹果");
} finally {
lock.unlock();
}
}
}