24.park和unpark方法

1.park方法可以暂停线程,线程状态为wait。

2.unpark方法可以恢复线程,线程状态为runnable。

3.LockSupport的静态方法。

4.park和unpark方法调用不分先后,unpark先调用,park后执行也可以恢复线程。

public class ParkDemo {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程t1->park....");
            LockSupport.park(); //对应的线程状态wait
            System.out.println("线程t1恢复运行....");

        }, "t1");
        t1.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();

你可能感兴趣的:(java并发编程,java)