图解ReentrantLock 0. demo
我先给出一个demo, 这样大家就可以根据我给的这段代码, 边调试边看源码了. 还是那句话: 注意"My" , 我把ReentrantLock类 改名为了 "MyReentrantLock"类 , "Lock"类 改名为了"MyLock"类. 大家粘贴我的代码的时候, 把相应的"My"都去掉就好了, 否则会编译报错哦.
import java.util.Scanner; import java.util.function.Supplier; public class Main { static final Scanner scanner = new Scanner(System.in); static volatile String cmd = ""; private static MyReentrantLock lock = new MyReentrantLock(true); public static void main(String[] args) { for (String name : new String[]{"1", "2", "3", "4", "5", "6"}) new Thread(() -> func(() -> lock, name)).start(); while (scanner.hasNext()) { cmd = scanner.nextLine(); } } public static void func(Supplier<MyLock> myLockSupplier, String name) { blockUntilEquals(() -> cmd, "lock " + name); myLockSupplier.get().lock(); System.out.println("获取了" + name + "号锁"); blockUntilEquals(() -> cmd, "unlock " + name); myLockSupplier.get().unlock(); System.out.println("释放了" + name + "号锁"); } private static void blockUntilEquals(Supplier<String> cmdSupplier, final String expect) { while (!cmdSupplier.get().equals(expect)) quietSleep(1000); } private static void quietSleep(int mills) { try { Thread.sleep(mills); } catch (InterruptedException e) { e.printStackTrace(); } } }