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();
        }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值