线程:方法join的使用
生活随笔
收集整理的這篇文章主要介紹了
线程:方法join的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 方法join()的作用是等待線程對象銷毀。
? 方法join()的作用是所屬的線程對象x正常執行run()方法中的任務, 而使當前線程z進行無限期的阻塞,等待線程x銷毀后再繼續執行線程z后面的代碼。
public class MyThread extends Thread{@Overridepublic void run(){try {int secondValue = (int)(Math.random() * 10000);System.out.println(secondValue);Thread.sleep(secondValue);} catch (InterruptedException e) {e.printStackTrace();}} }public class Test {public static void main(String[] args){try {MyThread threadTest = new MyThread();threadTest.start();threadTest.join();System.out.println("我想當threadTest對象執行完畢后我再執行");} catch (InterruptedException e) {e.printStackTrace();}} }方法join(long)與sleep(long)的區別
? ?方法join(long)的功能內部是使用wait(long)方法來實現的, 所以join(long)方法具有釋放鎖的特點。
??
總結
以上是生活随笔為你收集整理的线程:方法join的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程:等待/通知机制
- 下一篇: 线程:类ThreadLocal的使用