【336天】我愛刷題系列095(2018.01.07)

叨叨兩句

  1. ~

牛客網——java專項練習074

1

下列代碼執行結果爲()java

public static void main(String argv[])throws InterruptedException{
            Thread t=new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.print("2");
                }
            });
            t.start();
             
            t.join();
            System.out.print("1");
        }

正確答案: A 你的答案: 空 (錯誤)
A 21
B 12
C 可能爲12,也可能爲21
D 以上答案都不對線程

thread.Join把指定的線程加入到當前線程,能夠將兩個交替執行的線程合併爲順序執行的線程。好比在線程B中調用了線程A的Join()方法,直到線程A執行完畢後,纔會繼續執行線程B。
t.join();      //使調用線程 t 在此以前執行完畢。 
t.join(1000);  //等待 t 線程,等待時間是1000毫秒
相關文章
相關標籤/搜索