juc下locks之LockSupport

 public static void main(String[] args) {
        Thread t = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                if (i == 3){
                    LockSupport.park(); //当前线程停止
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("main线程6秒后");

        LockSupport.unpark(t);  //t线程继续执行
    }
发布了48 篇原创文章 · 获赞 1 · 访问量 2827

猜你喜欢

转载自blog.csdn.net/Forest24/article/details/101536978