實(shí)際應(yīng)用java多線程 java 多線程應(yīng)用
2023-06-09
情景1
如果有Thread11、Thread2、ThreaD3、四條線程各自統(tǒng)計(jì)CThread4。、D、E、四盤大小,全過程統(tǒng)計(jì)完畢,交給Thread5進(jìn)程進(jìn)行匯總,該如何實(shí)現(xiàn)?
第一,通過java.util.concurrent.在Executors中創(chuàng)建一個(gè)線程池,并使用這個(gè)線程池來啟動(dòng)這個(gè)過程。啟動(dòng)所有要啟動(dòng)的過程后,執(zhí)行線程池的shutdown()方法,即在所有過程完成后關(guān)閉線程池。然后通過isTerminated()線程池的方法來判斷線程池是否已關(guān)閉。成功關(guān)閉線程池,意味著整個(gè)過程已經(jīng)完成。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String args[]) throws InterruptedException {
ExecutorService exe = Executors.newFixedThreadPool(50);
for (int i = 1; i <= 5; i ) {
exe.execute(new SubThread(i));
}
exe.shutdown();
while (true) {
if (exe.isTerminated()) {
System.out.println(“結(jié)束了!");
break;
}
Thread.sleep(200);
}
}
}
View Code
2)使用T.join,讓主線程等待這些工作線程結(jié)束。
public class 011JoinTester implements Runnable {
private String name;
public 011JoinTester(String name) {
this.name = name;
}
public void run() {
System.out.printf("%s begins: %s\n", name, new Date());
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s has finished: %s\n", name, new Date());
}
public static void main(String[] args) {
while (true){
Thread thread1 = new Thread(new “JoinTester01”O(jiān)ne"));
Thread thread2 = new Thread(new “JoinTester01”Two"));
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Main thread is finished");
}
}
}
View Code
情景2
實(shí)現(xiàn)經(jīng)典消費(fèi)模式
1 package com.example.demo.consumer;
2
3 import java.util.LinkedList;
4
5 /**
6 * 生產(chǎn)者和消費(fèi)者之間的問題
7 * wait、notify/notifyAll() 實(shí)現(xiàn)
8 */
9 public class Storage1 implements AbstractStorage {
10 //倉庫最大容量
11 private static final int _1MB = 128;
12 private final int MAX_SIZE = 100;
13 //倉庫存儲(chǔ)載體
14 private LinkedList list = new LinkedList();
15 //private Object object = new Object();
16 //進(jìn)行生產(chǎn)
17 public void produce(int num){
18 //同步
19 synchronized (list){
20 //倉庫剩余容量不足以儲(chǔ)存即將生產(chǎn)的總產(chǎn)量,暫停生產(chǎn)
21 while(list.size() num > MAX_SIZE){
22 System.out.println("""要生產(chǎn)的產(chǎn)品數(shù)量":" num "\t【庫存量】:"
23 list.size() "\t暫時(shí)無法完成生產(chǎn)任務(wù)!");
24
25 try {
26 //不符合條件,生產(chǎn)堵塞
27 list.wait();
28 } catch (InterruptedException e) {
29 e.printStackTrace();
30 }
31 }
32
33 for(int i=0;i list.size()){
50 System.out.println(“要消費(fèi)的產(chǎn)品數(shù)量”:" num "\t【庫存量】:"
51 list.size() "\t暫時(shí)無法完成消費(fèi)任務(wù)!");
52
53 try {
54 list.wait();
55 } catch (InterruptedException e) {
56 e.printStackTrace();
57 }
58 }
59 //滿足消費(fèi)條件,開始消費(fèi)
60 for(int i=0;i
Storage1
public class Test{
public static void main(String[] args) {
// 倉庫目標(biāo)
AbstractStorage abstractStorage = new ()Storage1();
// 生產(chǎn)者目標(biāo)
Producer p1 = new Producer(abstractStorage);
Producer p2 = new Producer(abstractStorage);
Producer p3 = new Producer(abstractStorage);
Producer p4 = new Producer(abstractStorage);
Producer p5 = new Producer(abstractStorage);
Producer p6 = new Producer(abstractStorage);
Producer p7 = new Producer(abstractStorage);
// 顧客目標(biāo)
Consumer c1 = new Consumer(abstractStorage);
Consumer c2 = new Consumer(abstractStorage);
Consumer c3 = new Consumer(abstractStorage);
// 設(shè)定生產(chǎn)者產(chǎn)品制造數(shù)量。
p1.setNum(10);
p2.setNum(10);
p3.setNum(10);
p4.setNum(10);
p5.setNum(10);
p6.setNum(10);
p7.setNum(80);
// 設(shè)定客戶消費(fèi)商品的數(shù)量
c1.setNum(50);
c2.setNum(20);
c3.setNum(30);
// 進(jìn)程開始實(shí)施
c1.start();
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// abstractStorage.test();
c2.start();
c3.start();
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
p7.start();
}
}
main function
to be continued
本文為轉(zhuǎn)載內(nèi)容,我們尊重原作者對(duì)本文的作權(quán)。如有內(nèi)容錯(cuò)誤或侵權(quán)問題,歡迎原作者聯(lián)系我們更正或刪除內(nèi)容。
本文僅代表作者觀點(diǎn),版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請(qǐng)?jiān)谖闹凶⒚鱽碓醇白髡呙帧?/p>
免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)及時(shí)與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com

