ReentrantLock之tryLock()、lock()、lockInterruptibly()

 * tryLock不管是否获得锁,立即返回;获得锁,返回true,没有获得锁,返回false;
 * lock不受interr影响,会一直waiting;
 * lockInterruptibly受interr影响,无论是先lockInterruptibly后interrupt,还是先interrupt后lockInterruptibly,会TERMINATED,并且interrupt触发lockInterruptibly抛出InterruptedException;
   如果没有interrupt,lockInterruptibly则会一直waiting,不会抛出InterruptedException。

 

@Test
public void test1(){
    ReentrantLock lock = new ReentrantLock();
    lock.lock();
    System.out.println("main lock...");

    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("thread lock start...");
            lock.lock();
            System.out.println("thread lock end...");


        }
    });

    t1.start();

    try {
        Thread.sleep(3000L);
        System.out.println("status1: " + t1.getState());
        t1.interrupt();

        System.out.println("interrupt...");
        System.out.println("status2: " + t1.getState());
        Thread.sleep(10000L);
        System.out.println("status3: " + t1.getState());

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
@Test
    public  void test4(){
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        System.out.println("main lock...");

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100L);
                    System.out.println("thread lockInterruptibly start...");
                    lock.lockInterruptibly();
                    System.out.println("thread lockInterruptibly end...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("EXCEPTION: " + e.getMessage());
                }
            }
        });

        t.start();

        System.out.println("status1: " + t.getState());
//        t.interrupt();
        System.out.println("interrupt...");

        System.out.println("status2: " + t.getState());

        try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("status3: " + t.getState());

    }

 

测试结果:

main lock...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.xw.LockTest$2.run(LockTest.java:61)
at java.lang.Thread.run(Thread.java:748)
status1: RUNNABLE
interrupt...
status2: RUNNABLE
EXCEPTION: sleep interrupted
thread lock start...
status3: WAITING

 

main lock...
status1: RUNNABLE
interrupt...
status2: RUNNABLE
thread lockInterruptibly start...
EXCEPTION: null
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
at com.xw.LockTest$3.run(LockTest.java:142)
at java.lang.Thread.run(Thread.java:748)
status3: TERMINATED


如果注释掉interrupt:
main lock...
status1: RUNNABLE
status2: RUNNABLE
thread lockInterruptibly start...
status3: WAITING

你可能感兴趣的:(lock)