对对象上的睡眠线程释放锁执行中断异常



当线程处于睡眠状态时,它仍然持有对象的锁,当它被中断时,它释放锁并进入就绪状态还是继续执行而不改变状态?

当它被中断时,是否释放锁并进入就绪状态还是在不改变状态的情况下继续执行?

线程被中断只是一个状态改变(一个已经设置的标志)而不是状态改变,它对是否释放锁没有影响。

持有对象监视器的线程只有在调用相应对象实例上的wait(有或没有超时)或退出同步块时才会释放它,被中断或不被中断都不会改变此规则。


下面是一个简单的代码来展示这个想法:

// Used to make sure that thread t holds the lock before t2
CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(
    () -> {
        synchronized (someObject) {
            // Release t2
            latch.countDown();
            for (int i = 1; i <= 2; i++) {
                try {
                    System.out.println("Sleeping " + i);
                    // Sleep 2 sec and keep holding the lock
                    Thread.sleep(2_000L);
                    System.out.println("Sleep over " + i);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted " + i);
                }
            }
        }
    }
);
Thread t2 = new Thread(
    () -> {
        try {
            // Wait to be release by t
            latch.await();
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
        System.out.println("Trying to get in");
        synchronized (someObject) {
            System.out.println("In");
        }
    }
);
// Start the threads
t.start();
t2.start();
// Waiting 1 sec (< 2 sec) only before interrupting t
Thread.sleep(1_000L);
// Interrupt t
t.interrupt();
输出:

Trying to get in
Sleeping 1
Interrupted 1
Sleeping 2
Sleep over 2
In

从输出中可以看到,只有当线程t从同步块退出时,线程t2才进入同步块(获得锁)。线程t被中断的事实并没有使它释放锁。

最新更新