激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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  

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄色网址免费在线播放 | 欧美日韩色 | 久久精品视频16 | 国产精品视频二区不卡 | a视频在线播放 | 欧美成人三级视频 | 成人18免费观看 | a网在线 | 精品一区二区在线播放 | 手机免费看一级片 | 欧美国产一区二区三区激情无套 | 黄污网址 | 国产日韩在线观看视频 | 日本精品婷婷久久爽一下 | 视频一区二区三区中文字幕 | a一级黄| 日本一区二区免费在线观看 | 国产一国产精品一级毛片 | 日韩色电影 | 国产一国产精品一级毛片 | 九九热精 | 国产无遮挡一级毛片 | 奶子吧naiziba.cc免费午夜片在线观看 | 男女羞羞视频 | 素人视频在线观看免费 | 免费小毛片 | 久草在线观看首页 | 欧美韩国日本在线 | 欧美国产一区二区三区激情无套 | 亚洲午夜久久久精品一区二区三区 | 免费看成年人网站 | 亚洲第一成人久久网站 | 日韩视频一区二区三区在线观看 | 欧美专区视频 | 一区二区三区黄色 | 国产高清永久免费 | 色婷婷久久久亚洲一区二区三区 | 久久av一区二区 | 亚洲精品午夜电影 | 中文字幕在线日韩 | 国内免费视频成人精品 |