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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java并發編程中使用Executors類創建和管理線程的用法

Java并發編程中使用Executors類創建和管理線程的用法

2020-04-07 11:39zhangjunhd JAVA教程

這篇文章主要介紹了Java并發編程中使用Executors類創建和管理線程的用法,文中舉了用其啟動線程和設置線程優先級的例子,需要的朋友可以參考下

1. 類 Executors
Executors類可以看做一個“工具類”。援引JDK1.6 API中的介紹:
  此包中所定義的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 類的工廠和實用方法。此類支持以下各種方法:
(1)創建并返回設置有常用配置字符串的 ExecutorService 的方法。
(2)創建并返回設置有常用配置字符串的 ScheduledExecutorService 的方法。
(3)創建并返回“包裝的”ExecutorService 方法,它通過使特定于實現的方法不可訪問來禁用重新配置。
(4)創建并返回 ThreadFactory 的方法,它可將新創建的線程設置為已知的狀態。
(5)創建并返回非閉包形式的 Callable 的方法,這樣可將其用于需要 Callable 的執行方法中。
    通過這個類能夠獲得多種線程池的實例,例如可以調用newSingleThreadExecutor()獲得單線程的ExecutorService,調 用newFixedThreadPool()獲得固定大小線程池的ExecutorService,等等。拿到ExecutorService可以做的事情就比 較多了,最簡單的是用它來執行Runnable對象,也可以執行一些實現了Callable<T>的對象。用Thread的start()方 法沒有返回值,如果該線程執行的方法有返回值那用ExecutorService就再好不過了,可以選擇submit()、invokeAll()或者 invokeAny(),根據具體情況選擇合適的方法即可。
此類中提供的一些方法有:
1.1 public static ExecutorService newCachedThreadPool()
創建一個可根據需要創建新線程的線程池,但是在以前構造的線程可用時將重用它們。對于執行很多短期異步任務的程序而言,這些線程池通常可提高程序性能。
 
1.2 public static ExecutorService newFixedThreadPool(int nThreads)
創建一個可重用固定線程數的線程池,以共享的無界隊列方式來運行這些線程。
 
1.3 public static ExecutorService newSingleThreadExecutor()
創建一個使用單個 worker 線程的 Executor,以無界隊列方式來運行該線程。
 
這三個方法都可以配合接口ThreadFactory的實例一起使用。并且返回一個ExecutorService接口的實例。
2. 接口 ThreadFactory
根據需要創建新線程的對象。使用線程工廠就無需再手工編寫對 new Thread 的調用了,從而允許應用程序使用特殊的線程子類、屬性等等。
此接口最簡單的實現就是:

?
1
2
3
4
5
class SimpleThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
   return new Thread(r);
  }
 }

3. 接口ExecutorService
該接口提供了管理終止的方法。
4.創建標準線程池啟動線程
4.1 提供一個簡單的實現Runnable接口的線程

MyThread.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.zj.concurrency.executors;
 
public class MyThread implements Runnable {
  private int count = 1, number;
 
  public MyThread(int num) {
    number = num;
    System.out.println("Create Thread-" + number);
  }
 
  public void run() {
    while (true) {
      System.out.println("Thread-" + number + " run " + count+" time(s)");
      if (++count == 3)
       return;
    }
  }
}

這個線程會打印出相應的創建和執行信息。
 
4.2使用CachedThreadPool啟動線程
CachedThreadPool.java

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class CachedThreadPool {
  public static void main(String[] args) {
    ExecutorService exec = Executors.newCachedThreadPool();
    for (int i = 0; i < 5; i++)
      exec.execute(new MyThread(i));
    exec.shutdown();
  }
}

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create Thread-0
Create Thread-1
Create Thread-2
Create Thread-3
Thread-0 run 1 time(s)
Thread-0 run 2 time(s)
Thread-1 run 1 time(s)
Thread-1 run 2 time(s)
Thread-2 run 1 time(s)
Thread-2 run 2 time(s)
Create Thread-4
Thread-4 run 1 time(s)
Thread-4 run 2 time(s)
Thread-3 run 1 time(s)
Thread-3 run 2 time(s)

 
4.3 使用FixedThreadPool啟動線程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
FixedThreadPool.java
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class FixedThreadPool {
  public static void main(String[] args) {
    ExecutorService exec = Executors.newFixedThreadPool(2);
    for (int i = 0; i < 5; i++)
      exec.execute(new MyThread(i));
    exec.shutdown();
  }
}

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create Thread-0
Create Thread-1
Create Thread-2
Create Thread-3
Create Thread-4
Thread-0 run 1 time(s)
Thread-0 run 2 time(s)
Thread-2 run 1 time(s)
Thread-2 run 2 time(s)
Thread-3 run 1 time(s)
Thread-3 run 2 time(s)
Thread-4 run 1 time(s)
Thread-4 run 2 time(s)
Thread-1 run 1 time(s)
Thread-1 run 2 time(s)

 
4.4 使用SingleThreadExecutor啟動線程

SingleThreadExecutor.java

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class SingleThreadExecutor {
  public static void main(String[] args) {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    for (int i = 0; i < 5; i++)
      exec.execute(new MyThread(i));
    exec.shutdown();
  }
}

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create Thread-0
Create Thread-1
Create Thread-2
Create Thread-3
Create Thread-4
Thread-0 run 1 time(s)
Thread-0 run 2 time(s)
Thread-1 run 1 time(s)
Thread-1 run 2 time(s)
Thread-2 run 1 time(s)
Thread-2 run 2 time(s)
Thread-3 run 1 time(s)
Thread-3 run 2 time(s)
Thread-4 run 1 time(s)
Thread-4 run 2 time(s)

5.配合ThreadFactory接口的使用
我們試圖給線程加入daemon和priority的屬性設置。
5.1設置后臺線程屬性
DaemonThreadFactory.java

?
1
2
3
4
5
6
7
8
9
10
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
 
public class DaemonThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r);
    t.setDaemon(true);
    return t;
  }
}

 
5.2 設置優先級屬性
最高優先級MaxPriorityThreadFactory.java

?
1
2
3
4
5
6
7
8
9
10
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
 
public class MaxPriorityThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r);
    t.setPriority(Thread.MAX_PRIORITY);
    return t;
  }
}

最低優先級MinPriorityThreadFactory.java

?
1
2
3
4
5
6
7
8
9
10
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
 
public class MinPriorityThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r);
    t.setPriority(Thread.MIN_PRIORITY);
    return t;
  }
}

 
5.3啟動帶有屬性設置的線程
ExecFromFactory.java

?
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
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.zj.concurrency.executors.factory.DaemonThreadFactory;
import com.zj.concurrency.executors.factory.MaxPriorityThreadFactory;
import com.zj.concurrency.executors.factory.MinPriorityThreadFactory;
 
public class ExecFromFactory {
  public static void main(String[] args) throws Exception {
    ExecutorService defaultExec = Executors.newCachedThreadPool();
    ExecutorService daemonExec = Executors
       .newCachedThreadPool(new DaemonThreadFactory());
    ExecutorService maxPriorityExec = Executors
       .newCachedThreadPool(new MaxPriorityThreadFactory());
    ExecutorService minPriorityExec = Executors
       .newCachedThreadPool(new MinPriorityThreadFactory());
    for (int i = 0; i < 10; i++)
      daemonExec.execute(new MyThread(i));
    for (int i = 10; i < 20; i++)
      if (i == 10)
       maxPriorityExec.execute(new MyThread(i));
      else if (i == 11)
       minPriorityExec.execute(new MyThread(i));
      else
       defaultExec.execute(new MyThread(i));
  }
}

結果:

?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Create Thread-0
Create Thread-1
Create Thread-2
Create Thread-3
Thread-0 run 1 time(s)
Thread-0 run 2 time(s)
Thread-1 run 1 time(s)
Thread-1 run 2 time(s)
Thread-2 run 1 time(s)
Thread-2 run 2 time(s)
Create Thread-4
Thread-4 run 1 time(s)
Thread-4 run 2 time(s)
Create Thread-5
Thread-5 run 1 time(s)
Thread-5 run 2 time(s)
Create Thread-6
Create Thread-7
Thread-7 run 1 time(s)
Thread-7 run 2 time(s)
Create Thread-8
Thread-8 run 1 time(s)
Thread-8 run 2 time(s)
Create Thread-9
Create Thread-10
Thread-10 run 1 time(s)
Thread-10 run 2 time(s)
Create Thread-11
Thread-9 run 1 time(s)
Thread-9 run 2 time(s)
Thread-6 run 1 time(s)
Thread-6 run 2 time(s)
Thread-3 run 1 time(s)
Thread-3 run 2 time(s)
Create Thread-12
Create Thread-13
Create Thread-14
Thread-12 run 1 time(s)
Thread-12 run 2 time(s)
Thread-13 run 1 time(s)
Thread-13 run 2 time(s)
Create Thread-15
Thread-15 run 1 time(s)
Thread-15 run 2 time(s)
Create Thread-16
Thread-16 run 1 time(s)
Thread-16 run 2 time(s)
Create Thread-17
Create Thread-18
Create Thread-19
Thread-14 run 1 time(s)
Thread-14 run 2 time(s)
Thread-17 run 1 time(s)
Thread-17 run 2 time(s)
Thread-18 run 1 time(s)
Thread-18 run 2 time(s)
Thread-19 run 1 time(s)
Thread-19 run 2 time(s)
Thread-11 run 1 time(s)
Thread-11 run 2 time(s)

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久天天影视香蕉 | 青青国产在线观看 | 日韩在线观看一区二区不卡视频 | www在线视频在线播放 | 精品精品国产自在香蕉网 | 九九久久精品 | 九色PORNY蝌蚪视频首页 | 91精品91久久久久久 | 久久九九久精品国产尤物 | 91在线播 | 陈峰姚瑶全集小说无删节 | 高h视频免费观看 | 美女毛片在线 | 国产精品猎奇系列在线观看 | 四虎精品在线观看 | 欧洲肥女大肥臀tv | 国自产拍在线天天更新91 | 桥本有菜在线四虎福利网 | chinese老太granny chinese国产人妖hd | 动态图啪啪120秒免费看 | 亚洲国产日韩欧美在线vip1区 | 天天干夜夜添 | 国产123区| 成人女人天堂午夜视频 | 视频大全在线观看免费 | 亚洲美女啪啪 | 乌克兰一级毛片 | 国产成人成人一区二区 | 操一炮| 韩国免费特一级毛片 | 鬼吹灯天星术在线高清观看 | 欧美18一19性高清hd4k | 精品久久亚洲 | 亚洲欧美日韩另类精品一区二区三区 | 欧美日一级片 | 四虎小视频 | xxx久久| 国产一级一级一级成人毛片 | 亚洲AV蜜桃永久无码精品红樱桃 | 嫩草成人国产精品 | 视频在线免费看 |