日期:2014-05-20 浏览次数:20855 次
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class NotifySpecfiedThread {
private static final Map<Thread, Condition> notifyRelationship = new ConcurrentHashMap<Thread, Condition>();
private static final ReentrantLock locker = new ReentrantLock(false);
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
final int waitThreadNumber = 10;
for (int i = 1; i <= waitThreadNumber; i++) {
final Thread t = new WaitThreadTest();
notifyRelationship.put(t, locker.newCondition());
}
final Set<Thread> threads = notifyRelationship.keySet();
for (final Thread t : threads) {
t.start();
}
new NotifyThreadTest().join();
}
static class WaitThreadTest extends Thread {
public WaitThreadTest() {
}
/**
* notify the specified thread using different condition and lock with
* the same ReentrantLock instance
*/
public void run() {
locker.lock();