[英]does the main method wait until all the threads are done before executing the next line?
在Java中,如果我有一個從構造函數創建線程的類(通過調用該類的某些函數),我在main方法中創建該類的對象。 主方法是等待所有線程完成還是繼續到下一行?
例如:
public static void main(String[] args) {
WorksWithThreads obj = new WorksWithThreads ( );
//does something else - does this line happen after all the 9 threads finished their job?
}
class WorksWithThreads(){
public WorksWithThreads(){
for(int i=0;i<9;i++)
WithThread tread= new WithThread();
}
private static class WithThread extends Thread {
public WithThread () {
run();
}
public void run(){
//does something
}
}
}
我希望我不會太混亂..提前謝謝你.. Shiran
不。線程的關鍵在於它們不會停止生成它們的線程的執行。 一旦WorksWithThreads
完成所有線程的生成, main()
將繼續執行,但它生成的線程將與main
的其余部分同時執行。
是的,它將在創建9個線程后運行。你甚至沒有啟動那9個線程。所以在它們執行之前,下一行將在main中執行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.