最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法

 更新時(shí)間:2016年03月01日 08:51:43   作者:zhangjunhd  
這篇文章主要介紹了Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法,文中舉了用其啟動(dòng)線程和設(shè)置線程優(yōu)先級(jí)的例子,需要的朋友可以參考下

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

class SimpleThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
   return new Thread(r);
  }
 }

3. 接口ExecutorService
該接口提供了管理終止的方法。
4.創(chuàng)建標(biāo)準(zhǔn)線程池啟動(dòng)線程
4.1 提供一個(gè)簡(jiǎn)單的實(shí)現(xiàn)Runnable接口的線程
MyThread.java

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;
    }
  }
}

這個(gè)線程會(huì)打印出相應(yīng)的創(chuàng)建和執(zhí)行信息。
 
4.2使用CachedThreadPool啟動(dòng)線程
CachedThreadPool.java

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();
  }
}

結(jié)果:

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啟動(dòng)線程

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();
  }
}

結(jié)果:

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啟動(dòng)線程
SingleThreadExecutor.java

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();
  }
}

結(jié)果:

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接口的使用
我們?cè)噲D給線程加入daemon和priority的屬性設(shè)置。
5.1設(shè)置后臺(tái)線程屬性
DaemonThreadFactory.java

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 設(shè)置優(yōu)先級(jí)屬性
最高優(yōu)先級(jí)MaxPriorityThreadFactory.java

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;
  }
}

最低優(yōu)先級(jí)MinPriorityThreadFactory.java

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啟動(dòng)帶有屬性設(shè)置的線程
ExecFromFactory.java

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));
  }
}

結(jié)果:

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)

相關(guān)文章

  • Java多例設(shè)計(jì)模式實(shí)例詳解

    Java多例設(shè)計(jì)模式實(shí)例詳解

    這篇文章主要介紹了Java多例設(shè)計(jì)模式,結(jié)合實(shí)例形式分析了基于Java的多例模式概念、原理、定義與使用方法,需要的朋友可以參考下
    2018-05-05
  • 利用Java實(shí)現(xiàn)圖片馬賽克效果

    利用Java實(shí)現(xiàn)圖片馬賽克效果

    馬賽克效果是一種常見的圖像處理技術(shù),通過(guò)將圖像劃分為多個(gè)小塊并對(duì)每個(gè)小塊進(jìn)行平均色處理,模擬馬賽克的效果,在本項(xiàng)目中,我們將使用Java的Swing庫(kù)和圖像處理技術(shù)來(lái)實(shí)現(xiàn)圖片的馬賽克特效,需要的朋友可以參考下
    2025-02-02
  • java中使用POI生成Excel并導(dǎo)出過(guò)程

    java中使用POI生成Excel并導(dǎo)出過(guò)程

    這篇文章主要介紹了java中使用POI生成Excel并導(dǎo)出過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • System.currentTimeMillis()計(jì)算方式與時(shí)間的單位轉(zhuǎn)換詳解

    System.currentTimeMillis()計(jì)算方式與時(shí)間的單位轉(zhuǎn)換詳解

    這篇文章主要介紹了System.currentTimeMillis()計(jì)算方式與時(shí)間的單位轉(zhuǎn)換詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java%(取模運(yùn)算)全面講解

    Java%(取模運(yùn)算)全面講解

    這篇文章主要介紹了Java%(取模運(yùn)算)全面講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java 全面系統(tǒng)介紹反射的運(yùn)用

    Java 全面系統(tǒng)介紹反射的運(yùn)用

    準(zhǔn)備入手學(xué)習(xí)java的安全了,感覺這也是一個(gè)大的趨勢(shì),想著盡早進(jìn)入到j(luò)ava安全的探索中,在反序列化鏈的學(xué)習(xí)之前,需要先學(xué)習(xí)反射,不多說(shuō)了,開干吧
    2022-03-03
  • 詳細(xì)介紹Java中的各種鎖

    詳細(xì)介紹Java中的各種鎖

    不少同學(xué)開始慢慢接觸多線時(shí)候,對(duì)線程之間為了保障數(shù)據(jù)安全性,一致性有所了解,本文詳細(xì)解介紹java中的21種鎖 ,需要的朋友可以參考下
    2021-06-06
  • MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn)

    MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn)

    在開發(fā)中經(jīng)常遇到多個(gè)實(shí)體類有共同的屬性字段,這些字段屬于公共字段,本文主要介紹了MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • SpringCloud Hystrix熔斷器使用方法介紹

    SpringCloud Hystrix熔斷器使用方法介紹

    通過(guò)hystrix可以解決雪崩效應(yīng)問(wèn)題,它提供了資源隔離、降級(jí)機(jī)制、融斷、緩存等功能。接下來(lái)通過(guò)本文給大家分享SpringCloud集成Hystrix熔斷,感興趣的朋友一起看看吧
    2023-03-03

最新評(píng)論

柘荣县| 张家口市| 阿勒泰市| 徐闻县| 东乌珠穆沁旗| 东城区| 景宁| 宣威市| 罗定市| 天等县| 耒阳市| 丽水市| 南京市| 秦安县| 咸丰县| 库伦旗| 东安县| 顺义区| 惠水县| 烟台市| 静海县| 泗水县| 平顺县| 保靖县| 中西区| 个旧市| 河南省| 萨嘎县| 宽甸| 常山县| 哈巴河县| 古蔺县| 外汇| 南昌县| 乐亭县| 夹江县| 竹北市| 洞口县| 秦安县| 即墨市| 兴仁县|