在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ LockSupport.unpark方法可以發(fā)生在park之前,為什么我的代碼還

LockSupport.unpark方法可以發(fā)生在park之前,為什么我的代碼還是會阻塞

在main方法中,有4個(gè)LockSupport.unpark方法,當(dāng)t1線程運(yùn)行時(shí)應(yīng)該Lock.park()不應(yīng)該堵塞代碼,但是實(shí)際是堵塞的為什么,為什么main方法中注釋中的方法就可以輸出11111,park方法沒有阻塞當(dāng)前進(jìn)程。
public class LockSupportDemo {
    public static Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    

    public static class ChangeObjectThread extends Thread{

        public ChangeObjectThread(String name)
        {
            super.setName(name);
        }

        @Override
        public void run() {
            synchronized (u)
            {

                LockSupport.park(Thread.currentThread());
                System.out.println("in "+ getName());
                if(Thread.interrupted())
                {
                    System.out.println(getName()+" interrupted");
                }
            }
            System.out.println(getName() +"isOver");

        }
    }

    public static void main(String[] args) throws InterruptedException {
        LockSupport.unpark(t1);
        LockSupport.unpark(t1);
        LockSupport.unpark(t1);
        LockSupport.unpark(t1);
        LockSupport.unpark(t1);
        t1.start();
        /**
        *LockSupport.unpark(Thread.cuurentThread);
        *LockSupport.unpark(Thread.cuurentThread);
        *Lock.park();
        *System.out.println("11111");
        *
        */
    }
}
回答
編輯回答
雨萌萌

LockSupport#unPark()
方法注釋有說明,如下:
Makes available the permit for the given thread, if it was not already available. If the thread was blocked on park then it will unblock. Otherwise, its next call to park is guaranteed not to block. This operation is not guaranteed to have any effect at all if the given thread has not been started.

加粗斜體部分已經(jīng)說明,不會保證方法功能,如果線程未啟動之前操作。
所以先要t1.start()

2018年6月18日 06:31
編輯回答
淚染裳

當(dāng)前線程要啟動才可以。

2017年7月7日 08:55