更新時(shí)間:2022-09-19 11:37:52 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1185次
Java多線程編程中首先對任務(wù)進(jìn)行排隊(duì)。調(diào)用執(zhí)行器服務(wù)點(diǎn)新的固定線程池并提供大小。此大小表示同時(shí)任務(wù)的最大數(shù)量。例如,如果將一千個(gè)事物添加到隊(duì)列中,但池大小為 50,那么任何時(shí)候都只有 50 個(gè)在運(yùn)行。只有當(dāng)前 50 個(gè)中的一個(gè)完成執(zhí)行時(shí),才會(huì)占用第 51 個(gè)執(zhí)行。像 100 這樣的數(shù)字作為池大小不會(huì)使系統(tǒng)過載。
ExecutorService taskList = Executors.newFixedThreadPool( poolSize );
然后,用戶必須將一些可運(yùn)行類型的任務(wù)放入任務(wù)隊(duì)列。Runnable 只是一個(gè)單一的接口,它有一個(gè)名為 run 的方法。系統(tǒng)在適當(dāng)?shù)臅r(shí)候通過啟動(dòng)一個(gè)單獨(dú)的線程在任務(wù)之間來回切換時(shí)調(diào)用run方法。
taskList.execute( someRunnable )
Execute 方法有點(diǎn)用詞不當(dāng),因?yàn)楫?dāng)一個(gè)任務(wù)被添加到上面創(chuàng)建的隊(duì)列中的任務(wù)中時(shí),執(zhí)行器 dot new 固定線程池,它不一定立即開始執(zhí)行它。當(dāng)同時(shí)執(zhí)行的其中一個(gè)(池大小)完成執(zhí)行時(shí),它開始執(zhí)行。
首先要做的是創(chuàng)建一個(gè)單獨(dú)的類,并且是一個(gè)完全獨(dú)立的類,它實(shí)現(xiàn)了可運(yùn)行接口。
公共類 MyRunnable實(shí)現(xiàn) Runnable {
公共無效運(yùn)行(){...}
}
其次制作主類的一些實(shí)例并將它們傳遞給執(zhí)行。讓我們應(yīng)用第一種方法來制作只計(jì)數(shù)的線程。因此,每個(gè)線程都會(huì)打印線程名稱、任務(wù)號和計(jì)數(shù)器值。
在此之后使用 pause 方法坐下來等待,以便系統(tǒng)來回切換。打印語句將因此被交錯(cuò)。
將構(gòu)造函數(shù)參數(shù)傳遞給 Runnable 的構(gòu)造函數(shù),以便不同的實(shí)例計(jì)算不同的次數(shù)。
調(diào)用關(guān)閉方法意味著關(guān)閉正在監(jiān)視的線程以查看是否添加了任何新任務(wù)。
實(shí)際實(shí)施
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author evivehealth on 08/02/19.
*/
// Java program depicting
// concurrent programming in action.
// Runnable Class that defines the logic
// of run method of runnable interface
public class Counter implements Runnable
{
private final MainApp mainApp;
private final int loopLimit;
private final String task;
// Constructor to get a reference to the main class
public Counter
(MainApp mainApp, int loopLimit, String task)
{
this.mainApp = mainApp;
this.loopLimit = loopLimit;
this.task = task;
}
// Prints the thread name, task number and
// the value of counter
// Calls pause method to allow multithreading to occur
@Override
public void run()
{
for (int i = 0; i < loopLimit; i++)
{
System.out.println("Thread: " +
Thread.currentThread().getName() + " Counter: "
+ (i + 1) + " Task: " + task);
mainApp.pause(Math.random());
}
}
}
class MainApp
{
// Starts the threads. Pool size 2 means at any time
// there can only be two simultaneous threads
public void startThread()
{
ExecutorService taskList =
Executors.newFixedThreadPool(2);
for (int i = 0; i < 5; i++)
{
// Makes tasks available for execution.
// At the appropriate time, calls run
// method of runnable interface
taskList.execute(new Counter(this, i + 1,
"task " + (i + 1)));
}
// Shuts the thread that's watching to see if
// you have added new tasks.
taskList.shutdown();
}
// Pauses execution for a moment
// so that system switches back and forth
public void pause(double seconds)
{
try
{
Thread.sleep(Math.round(1000.0 * seconds));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
// Driver method
public static void main(String[] args)
{
new MainApp().startThread();
}
}
輸出:
線程:pool-1-thread-1 計(jì)數(shù)器:1 任務(wù):任務(wù) 1
線程:pool-1-thread-2 計(jì)數(shù)器:1 任務(wù):任務(wù) 2
線程:pool-1-thread-2 計(jì)數(shù)器:2 任務(wù):任務(wù) 2
線程:pool-1-thread-1 計(jì)數(shù)器:1 任務(wù):任務(wù) 3
線程:pool-1-thread-2 計(jì)數(shù)器:1 任務(wù):任務(wù) 4
線程:pool-1-thread-1 計(jì)數(shù)器:2 任務(wù):任務(wù) 3
線程:pool-1-thread-1 計(jì)數(shù)器:3 任務(wù):任務(wù) 3
線程:pool-1-thread-1 計(jì)數(shù)器:1 任務(wù):任務(wù) 5
線程:pool-1-thread-2 計(jì)數(shù)器:2 任務(wù):任務(wù) 4
線程:pool-1-thread-2 計(jì)數(shù)器:3 任務(wù):任務(wù) 4
線程:pool-1-thread-1 計(jì)數(shù)器:2 任務(wù):任務(wù) 5
線程:pool-1-thread-2 計(jì)數(shù)器:4 任務(wù):任務(wù) 4
線程:pool-1-thread-1 計(jì)數(shù)器:3 任務(wù):任務(wù) 5
線程:pool-1-thread-1 計(jì)數(shù)器:4 任務(wù):任務(wù) 5
線程:pool-1-thread-1 計(jì)數(shù)器:5 任務(wù):任務(wù) 5
優(yōu)點(diǎn):
松散耦合:由于可以重用單獨(dú)的類,因此它促進(jìn)了松散耦合。
構(gòu)造函數(shù):參數(shù)可以傳遞給不同情況的構(gòu)造函數(shù)。例如,描述線程的不同循環(huán)限制。
競爭條件:如果數(shù)據(jù)已共享,則不太可能使用單獨(dú)的類作為方法,如果它沒有共享數(shù)據(jù),則無需擔(dān)心競爭條件。
缺點(diǎn):
回調(diào)主應(yīng)用有點(diǎn)不方便。必須通過Java構(gòu)造函數(shù)傳遞引用,即使可以訪問引用,也只能調(diào)用主應(yīng)用程序中的公共方法(給定示例中的暫停方法)。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743