锁(四):多线程debug


  • 案例
public class ReentrantLockDebugDemo {

    // 变量i
    private int i = 0;

    // new1个reentrantLock 
    private ReentrantLock reentrantLock = new ReentrantLock();

    // 测试用的方法
    public void inCreate() {
        // 获取锁
        reentrantLock.lock();
        try {
            i++;
            System.out.println(i);
        } finally {
            // 一定要在finall方法中释放锁
            reentrantLock.unlock();
        }

    }

    public static void main(String[] args) {
        ReentrantLockDebugDemo reentrantLockDebugDemo = new ReentrantLockDebugDemo();
        for (int i = 0; i < 3; i++) {      // 3个线程中执行同一个方法
            new Thread(() -> {
                reentrantLockDebugDemo.inCreate();
            }).start();
        }
    }

}
  • 选择线程级别debug

  • 查看线程