傳統執行緒技術(一)

2021-09-07 05:31:28 字數 1129 閱讀 6933

一. 傳統執行緒建立方法

1. 覆蓋thread子類的run方法中編寫詳細**

2. 在傳遞給thread的runnable物件的run方法中編寫詳細**

二. 實現**

public class traditionalthread  catch (interruptedexception e) }}

};thread1.start();

// 方法2:給thread類傳乙個實現了runnable介面的類

thread thread2 = new thread(new runnable() catch (interruptedexception e) }}

});thread2.start();

}}

小知識:

1. new thread()   這樣的寫法事實上是建立了thread的子類

2. new thread(new runnable(){})   傳遞給thread的引數事實上是runnable的實現類物件

三. 乙個典型樣例:

請問以下的類是執行runnable中的run方法還是thread子類的方法?

public class test  catch (interruptedexception e) 

system.out.println("runnable:" + thread.currentthread().getname());}}

}) catch (interruptedexception e)

system.out.println("thread:" + thread.currentthread().getname());}}

}.start(); }

/*** thread 類中的run方法

* private runnable target;

* * public void run()

* }**/

}

答案是執行thread子類的run方法。 由於它覆蓋了父類的run方法,根本就不會執行到runnable的方法,即target.run()

執行緒傳統技術

1.實現thread類建立執行緒 public class mythread extends thread mythread mythread1 newmythread mythread mythread2 newmythread mythread1.start mythread2.start 2....

傳統多執行緒技術

執行緒有2種方式 1 重寫thread的run方法 例子 new thread start 2 thread中構造方法有帶引數的傳入runable 推薦 例子 new thread new runable public void run start 解釋 thread類中的run方法 為 priva...

傳統執行緒同步通訊技術

什麼是執行緒同步通訊呢?其實簡單來說就是執行緒間的等待與喚醒 下面我們來了解一下 1.簡單多執行緒通訊 現在有a b執行緒,讓執行緒a先執行10次迴圈,隨後讓執行緒b執行20次,之後反覆100次 該如何實現呢?需要注意哪些問題?如下 public class main start new threa...