图解Condition 0. demo
我先给出一个demo, 这样大家就可以根据我给的这段代码, 边调试边看源码了. 还是那句话: 注意"My" , 我把ReentrantLock类 改名为了 "MyReentrantLock"类 , "Lock"类 改名为了"MyLock"类. 大家粘贴我的代码的时候, 把相应的"My"都去掉就好了, 否则会编译报错哦.
import java.util.Scanner; import java.util.concurrent.locks.Condition; import java.util.function.Supplier; public class ConditionTest { static final Scanner scanner = new Scanner(System.in); static volatile String cmd = ""; private static MyReentrantLock lock = new MyReentrantLock(true); private static Condition condition = lock.newCondition(); public static void main(String[] args) { for (String name : new String[]{"w1", "w2", "w3", "w4", "w5", "w6"}) new Thread(() -> func(() -> lock, name)).start(); new Thread(() -> signalOne(() -> lock, "s")).start(); while (scanner.hasNext()) { cmd = scanner.nextLine(); } } public static void func(Supplier<MyLock> myLockSupplier, String name) { blockUntilEquals(() -> cmd, name); myLockSupplier.get().lock(); System.out.println(name + "阻塞等待..."); try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("释放了" + name); myLockSupplier.get().unlock(); } public static void signalOne(Supplier<MyLock> myLockSupplier, String name) { while (true) { blockUntilEquals(() -> cmd, name); myLockSupplier.get().lock(); condition.signal(); System.out.println("通知唤醒了一个等待..."); myLockSupplier.get().unlock(); } } private static void blockUntilEquals(Supplier<String> cmdSupplier, final String expect) { while (!cmdSupplier.get().equals(expect)) quietSleep(1000); clearCmd(); } private static void quietSleep(int mills) { try { Thread.sleep(mills); } catch (InterruptedException e) { e.printStackTrace(); } } private static void clearCmd() { cmd = ""; } }