LockSupport api詳解

今天學習LockSupport工具類,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現,初學乍練,有很多一知半解的地方,待後續有了深入理解再來補充

package test.java.util.concurrent.locks;


import java.util.concurrent.locks.LockSupport;
import org.junit.Test;

/**
 * LockSupport的測試類
 *
 * @author zqw
 * @date 2020-06-21 18:27:51
 */
public class LockSupportTest {
        public static Object object = new Object();

        public static class TestThread extends Thread {
                public TestThread(String name) {
                        super(name);
                }
                @Override public void run() {
                        LockSupport.park(object);
                                System.out.println("當前線程: " + getName());
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                }
        }
        /**
        * park和unPark 和wait和notify功能一樣,但是不能交叉使用
         * park和unPark沒有順序
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testParkAndUnpark() throws Exception {
                TestThread t1 = new TestThread("t1");
                TestThread t2 = new TestThread("t2");
                t1.start();
                Thread.sleep(1000L);
                t2.start();
                Thread.sleep(1000L);
                t1.interrupt();
                LockSupport.unpark(t2);
                t1.join();
                t2.join();
        }
        public static class TestThread1 extends Thread {
                public TestThread1(String name) {
                        super(name);
                }
                @Override public void run() {
                        synchronized (object) {
                                System.out.println("當前線程: " + getName());
                                long start=System.nanoTime();
                                LockSupport.parkNanos(100000000);
                                System.out.println("差值:"+(System.nanoTime()-start));
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                        }
                }
        }
        /**
         * park和unPark 和wait和notify功能一樣,但是不能交叉使用
         * park和unPark沒有順序
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testParkNanos()throws Exception{
                TestThread1 t1 = new TestThread1("t1");
                t1.start();
                Thread.sleep(200L);
                LockSupport.unpark(t1);
        }
        public static class TestThread2 extends Thread {
                public TestThread2(String name) {
                        super(name);
                }
                @Override public void run() {
                        synchronized (object) {
                                System.out.println("當前線程: " + getName());
                                long start=System.nanoTime();
                                LockSupport.parkUntil(object,1000);
                                System.out.println("blocker:"+LockSupport.getBlocker(this));
                                System.out.println("差值:"+(System.nanoTime()-start));
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                        }
                }
        }
        /**
         * 在指定的時限前禁用當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testParkUntil()throws Exception{
                TestThread2 t1 = new TestThread2("t1");
                t1.start();
//                LockSupport.unpark(t1);
        }
        /**
         * 在指定的時限前禁用當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testGetBlocker()throws Exception{
                TestThread2 t1 = new TestThread2("t1");
                t1.start();
                LockSupport.unpark(t1);
        }
        public static class TestThread3 extends Thread {
                public TestThread3(String name) {
                        super(name);
                }
                @Override public void run() {
                        synchronized (object) {
                                System.out.println("當前線程: " + getName());
                                long start=System.nanoTime();
                                LockSupport.park();
                                System.out.println("blocker:"+LockSupport.getBlocker(this));
                                System.out.println("差值:"+(System.nanoTime()-start));
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                        }
                }
        }
        /**
         * park和unPark 和wait和notify功能一樣,但是不能交叉使用
         * park和unPark沒有順序
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testPark()throws Exception{
                TestThread3 t1 = new TestThread3("t1");
                t1.start();
                LockSupport.unpark(t1);
        }
        public static class TestThread4 extends Thread {
                public TestThread4(String name) {
                        super(name);
                }
                @Override public void run() {
                        synchronized (object) {
                                System.out.println("當前線程: " + getName());
                                long start=System.nanoTime();
                                LockSupport.parkNanos(10000);
                                System.out.println("差值:"+(System.nanoTime()-start));
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                        }
                }
        }
        /**
         * park和unPark 和wait和notify功能一樣,但是不能交叉使用
         * park和unPark沒有順序
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testParkNanosLong()throws Exception{
                TestThread4 t1 = new TestThread4("t1");
                t1.start();
        }
        public static class TestThread5 extends Thread {
                public TestThread5(String name) {
                        super(name);
                }
                @Override public void run() {
                        synchronized (object) {
                                System.out.println("當前線程: " + getName());
                                long start=System.nanoTime();
                                LockSupport.parkUntil(10000);
                                System.out.println("差值:"+(System.nanoTime()-start));
                                if (Thread.currentThread().isInterrupted()) {
                                        System.out.println(getName()+"被中斷了");
                                }
                                System.out.println(getName()+"繼續執行");
                        }
                }
        }
        /**
         * 在指定的時限前禁用當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 19:18
         */
        @Test
        public void testParkUntilL()throws Exception{
                TestThread5 t1 = new TestThread5("t1");
                t1.start();
        }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章