這篇文章將為大家詳細講解有關怎么在Java中使用start方法實現多線程,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1、為什么啟動線程不用run()方法而是使用start()方法
run()方法只是一個類中的普通方法,調用run方法跟調用普通方法一樣
而start()是創建線程等一系列工作,然后自己調用run里面的任務內容。
驗證代碼:
/**
* @data 2019/11/8 - 下午10:29
* 描述:run()和start()
*/
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
};
runnable.run();
new Thread(runnable).start();
}
}結果:
main
Thread-0
2、start()源碼解讀
啟動新線程檢查線程狀態
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();關于threadStatus源碼:
/* * Java thread status for tools, default indicates thread 'not yet started' */ private volatile int threadStatus;
通過代碼可以看到就是threadStatus就是記錄Thread的狀態,初始線程默認為0.
加入線程組
/* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this);
調用start0()
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}start0()方法使用c++編寫的方法,這些代碼在gdk代碼中,所以這里不再這里探究了。
3、start()方法不能使用多次
通過剛剛源碼分析,就知道start方法剛開始就檢查線程狀態,當線程創建后或結束了,該狀態就不同于初始化狀態就會拋出IllegalThreadStateException異常。
測試代碼:
start不可以使用多次
public class CantStartTwice {
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
thread.start();
}
}4、注意點:
start方法是被synchronized修飾的方法,可以保證線程安全。
由jvm創建的main方法線程和system組線程,并不會通過start來啟動。
Java中的集合主要分為四類:1、List列表:有序的,可重復的;2、Queue隊列:有序,可重復的;3、Set集合:不可重復;4、Map映射:無序,鍵唯一,值不唯一。
關于怎么在Java中使用start方法實現多線程就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。