一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Java 多線程編程之Executor

Java 多線程編程之Executor

2020-10-09 21:53CBDoctor JAVA教程

這篇文章為大家講解Java 多線程編程之Executor的實現方法,Executor實際上是提供了一個線程池的概念,他的優點是實現了多線程任務的提交和執行的解耦。

?
1
2
3
public interface Executor{
    void executor(Runnable command);
}

如上所寫,Executor實際上是一個接口,他提供了唯一的接口方法executor(Runnable command)

Executor實際上是提供了一個線程池的概念, 他的優點是實現了多線程任務的提交和執行的解耦。 通過Executor,使用者可以不用關心任務是被哪一個線程執行的,什么時候執行的。只需要等待線程執行完畢,并獲得最后的結果就可以了。舉個簡答的例子:

我們工作中當老大的老大(且稱作LD^2)把一個任務交給我們老大(LD)的時候,到底是LD自己干,還是轉過身來拉來一幫苦逼的兄弟加班加點干,那LD^2是不管的。LD^2只用把人描述清楚提及給LD,然后喝著咖啡等著收LD的report即可。等LD一封郵件非常優雅

地報告LD^2report結果時,實際操作中是碼農A和碼農B干了一個月,還是碼農ABCDE加班干了一個禮拜,大多是不用體現的。這套機制的優點就是LD^2找個合適的LD出來提交任務即可,接口友好有效,不用為具體怎么干費神費力。

-----(戲(細)說Executor框架線程池任務執行全過程)

下面是一個簡單的套用Executor的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package concurrency.practice;
 
package com.journaldev.threadpool;
 
public class WorkerThread implements Runnable {
     
    private String command;
     
    public WorkerThread(String s){
        this.command=s;
    }
 
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }
 
    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public String toString(){
        return this.command;
    }
}

Here is the test program where we are creating fixed thread pool from Executors framework.

SimpleThreadPool.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.journaldev.threadpool;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class SimpleThreadPool {
 
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    }
 
}

讓我們查看一下輸出結果:

Here is the output of the above program.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pool-1-thread-2 Start. Command = 1
pool-1-thread-4 Start. Command = 3
pool-1-thread-1 Start. Command = 0
pool-1-thread-3 Start. Command = 2
pool-1-thread-5 Start. Command = 4
pool-1-thread-4 End.
pool-1-thread-5 End.
pool-1-thread-1 End.
pool-1-thread-3 End.
pool-1-thread-3 Start. Command = 8
pool-1-thread-2 End.
pool-1-thread-2 Start. Command = 9
pool-1-thread-1 Start. Command = 7
pool-1-thread-5 Start. Command = 6
pool-1-thread-4 Start. Command = 5
pool-1-thread-2 End.
pool-1-thread-4 End.
pool-1-thread-3 End.
pool-1-thread-5 End.
pool-1-thread-1 End.
Finished all threads

In above program, we are creating fixed size thread pool of 5 worker threads. Then we are submitting 10 jobs to this pool, since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state, as soon as one of the job is finished, another job from the wait queue will be picked up by worker thread and get’s executed.

注意在 SimpleThreadPool.java 中我們調用了ExecutorService 接口。該接口實現了Executor并且提供了一個額外的方法

?
1
public interface ExecutorService extends Executor

原文地址:https://www.cnblogs.com/CBDoctor/p/5078199.html  

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品男人的天堂 | wc凹凸撒尿间谍女厕hd | 女老板用丝袜脚夹我好爽 | 无限在线观看免费入口 | 国产黄频在线观看高清免费 | 无人区乱码1区2区3区网站 | 精品手机在线视频 | 欧美一区二区三区不卡视频 | 九九九久久久 | 精品国产免费久久久久久 | japanese秘书丝袜 | 7mav视频 | 99在线免费观看视频 | freefron性中国国产高清 | 国产成人精品视频一区二区不卡 | 日日网| 无码精品一区二区三区免费视频 | 欧美一区二区三区四区在线观看 | 从后面撕开老师的丝袜动态图 | 日本中文字幕一区二区三区不卡 | 久久久久久久国产精品视频 | 成人亚洲欧美日韩中文字幕 | 天天操网 | 拍拍叫痛的无挡视频免费 | 四虎黄色影视 | 青青国产在线观看 | www在线免费观看 | 清纯漂亮女友初尝性过程 | free哆拍拍免费永久视频 | 99精品在线视频观看 | 国产伦精品一区二区三区免费迷 | h肉动漫在线视频无修无遮挡 | 精品久久伦理中文字幕 | 肉大捧一进一出视频免费播放 | 91青青视频| 国产成人h视频在线播放网站 | avtt天堂在线| 韩国三级 720p | 国产在线激情视频 | 视频在线观看一区二区 | 成人午夜剧场 |