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