更新時間:2022-09-19 11:37:52 來源:動力節(jié)點 瀏覽1259次
Java多線程編程中首先對任務進行排隊。調(diào)用執(zhí)行器服務點新的固定線程池并提供大小。此大小表示同時任務的最大數(shù)量。例如,如果將一千個事物添加到隊列中,但池大小為 50,那么任何時候都只有 50 個在運行。只有當前 50 個中的一個完成執(zhí)行時,才會占用第 51 個執(zhí)行。像 100 這樣的數(shù)字作為池大小不會使系統(tǒng)過載。
ExecutorService taskList = Executors.newFixedThreadPool( poolSize );
然后,用戶必須將一些可運行類型的任務放入任務隊列。Runnable 只是一個單一的接口,它有一個名為 run 的方法。系統(tǒng)在適當?shù)臅r候通過啟動一個單獨的線程在任務之間來回切換時調(diào)用run方法。
taskList.execute( someRunnable )
Execute 方法有點用詞不當,因為當一個任務被添加到上面創(chuàng)建的隊列中的任務中時,執(zhí)行器 dot new 固定線程池,它不一定立即開始執(zhí)行它。當同時執(zhí)行的其中一個(池大小)完成執(zhí)行時,它開始執(zhí)行。
首先要做的是創(chuàng)建一個單獨的類,并且是一個完全獨立的類,它實現(xiàn)了可運行接口。
公共類 MyRunnable實現(xiàn) Runnable {
公共無效運行(){...}
}
其次制作主類的一些實例并將它們傳遞給執(zhí)行。讓我們應用第一種方法來制作只計數(shù)的線程。因此,每個線程都會打印線程名稱、任務號和計數(shù)器值。
在此之后使用 pause 方法坐下來等待,以便系統(tǒng)來回切換。打印語句將因此被交錯。
將構(gòu)造函數(shù)參數(shù)傳遞給 Runnable 的構(gòu)造函數(shù),以便不同的實例計算不同的次數(shù)。
調(diào)用關(guān)閉方法意味著關(guān)閉正在監(jiān)視的線程以查看是否添加了任何新任務。
實際實施
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 計數(shù)器:1 任務:任務 1
線程:pool-1-thread-2 計數(shù)器:1 任務:任務 2
線程:pool-1-thread-2 計數(shù)器:2 任務:任務 2
線程:pool-1-thread-1 計數(shù)器:1 任務:任務 3
線程:pool-1-thread-2 計數(shù)器:1 任務:任務 4
線程:pool-1-thread-1 計數(shù)器:2 任務:任務 3
線程:pool-1-thread-1 計數(shù)器:3 任務:任務 3
線程:pool-1-thread-1 計數(shù)器:1 任務:任務 5
線程:pool-1-thread-2 計數(shù)器:2 任務:任務 4
線程:pool-1-thread-2 計數(shù)器:3 任務:任務 4
線程:pool-1-thread-1 計數(shù)器:2 任務:任務 5
線程:pool-1-thread-2 計數(shù)器:4 任務:任務 4
線程:pool-1-thread-1 計數(shù)器:3 任務:任務 5
線程:pool-1-thread-1 計數(shù)器:4 任務:任務 5
線程:pool-1-thread-1 計數(shù)器:5 任務:任務 5
優(yōu)點:
松散耦合:由于可以重用單獨的類,因此它促進了松散耦合。
構(gòu)造函數(shù):參數(shù)可以傳遞給不同情況的構(gòu)造函數(shù)。例如,描述線程的不同循環(huán)限制。
競爭條件:如果數(shù)據(jù)已共享,則不太可能使用單獨的類作為方法,如果它沒有共享數(shù)據(jù),則無需擔心競爭條件。
缺點:
回調(diào)主應用有點不方便。必須通過Java構(gòu)造函數(shù)傳遞引用,即使可以訪問引用,也只能調(diào)用主應用程序中的公共方法(給定示例中的暫停方法)。