[精讚] [會員登入]
563

停止多執行緒

利用主程式呼叫多執行緒時, 要怎麼停止正在執行的多執行緒

分享此文連結 //n.sfs.tw/16083

分享連結 停止多執行緒@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2023-03-14 13:54:41 最後編修
2023-03-08 10:47:32 By igogo
 

 

q.利用主程式呼叫多執行緒時, 要怎麼停止正在執行的多執行緒

ans. 在thread裡利用InterruptedException 把工作結束

例如這裡是for loop 10次, 就把工作數直接設定到滿

 

建立Car

每一台車工作量10次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Car implements Runnable {
 
    private String name;
 
    public Car(String name) {
        this.name = name;
    }
 
    @Override
    public void run() {
 
        for (int i = 0; i < 10; i++) {
            System.out.println(name + " move :" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println(name + " is interrupted.");
                i = 10;
            }
 
        }
    }
}

 

 

主程式

1
2
3
4
5
6
7
8
9
10
11
12
Car toyota = new Car("toyota");
Car ford = new Car("ford");
Thread thread1 = new Thread(toyota);
Thread thread2 = new Thread(ford);
 
thread1.start();
thread2.start();
 
Thread.sleep(2000);
thread2.interrupt();
 
System.out.println("main job is done");

thread1, thread2 啟動後, 等待兩秒,

再呼叫thread2.interrupt() , 這樣執行緒收到interrupt, 就會以interrupted exception 處理, 終止工作

而另一支thread1 繼續進行

 

如果我想等所有的執行緒都結束, 再回到主程式, 則加上join()即可
因為interrupted exception也是讓該執行緒結束, 所以主程式還是視此子執行緒成功結束了

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Car toyota = new Car("toyota");
Car ford = new Car("ford");
Thread thread1 = new Thread(toyota);
Thread thread2 = new Thread(ford);
 
thread1.start();
thread2.start();
 
Thread.sleep(2000);
thread2.interrupt();
 
thread2.join();
thread1.join();
 
System.out.println("main job is done");

 

如上

ford會在兩秒後中斷, 但主程式會繼續等待toyota跑完全部工作, 主程式再結束

 

ThreadGroup

如果我是想從另外一支程式停止特定的thread,則可以利用threadgroup

再取得符合threadgroup name的所有執行緒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Car toyota = new Car("toyota");
       Car ford = new Car("ford");
 
 
       ThreadGroup tg = new ThreadGroup("CarGroup");
 
       Thread thread1 = new Thread(tg,toyota);
       Thread thread2 = new Thread(tg,ford);
       logger.info(tg.getName());
 
       thread1.start();
       thread2.start();
 
 
       thread2.join();
       thread1.join();
 
 
       logger.info("main job is done");

 

request stop

1
2
3
4
5
6
7
8
9
Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
      for (Map.Entry<Thread, StackTraceElement[]> entry : threads.entrySet()) {
          Thread thread = entry.getKey();
          ThreadGroup threadGroup = thread.getThreadGroup();
          if (threadGroup.getName().equals("CarGroup")) {
              threadGroup.interrupt();
 
          }
      }

 

 

END

你可能感興趣的文章

Thread java thread

java lambda files filter java, files filter, lambda

ArrayList 與 HashMap 範例 public static void main(String[] args) { String titleIds = &

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

java.time 時間 instant java.time

Arrays.asList 後想再add出現 UnsupportedOperationException 這個問題真是搞死我了 List<String> fruits = Arrays.asList("a

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

vue.js modal 作兩個選項按鈕並導向不同頁面 vue.js modal 作兩個選項按鈕

編碼的順序 utf8 big5

讀取特定資料夾下的xls檔 讀取特定資料夾下的xls檔

利用google apps script 將Google sheet 中兩個工作表中的資料對應 如何整理google測驗收回來的學生資料