Bug:StampedLock的中斷問題導致CPU爆滿
阿新 • • 發佈:2018-12-22
public class TestStampedLock { public static void main(String[] args) throws InterruptedException{ final StampedLock lock = new StampedLock(); new Thread(){ public void run(){ long readLong = lock.writeLock(); LockSupport.parkNanos(6100000000L); lock.unlockWrite(readLong); } }.start(); Thread.sleep(100); for( int i = 0; i < 3; ++i) new Thread(new OccupiedCPUReadThread(lock)).start(); } private static class OccupiedCPUReadThread implements Runnable{ private StampedLock lock; public OccupiedCPUReadThread(StampedLock lock){ this.lock = lock; } public void run(){ Thread.currentThread().interrupt(); long lockr = lock.readLock(); System.out.println(Thread.currentThread().getName() + " get read lock"); lock.unlockRead(lockr); } } }
boolean interrupted = false;
if(interrupted) Thread.currentThread().interrupt(); return ns;
if(Thread.interrupted()){ if(interruptible) return cancelWaiter(node, p, true); else interrupted = true; }