欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Java線程池的幾種實(shí)現(xiàn)方法和區(qū)別介紹實(shí)例詳解-創(chuàng)新互聯(lián)

下面通過(guò)實(shí)例代碼為大家介紹Java線程池的幾種實(shí)現(xiàn)方法和區(qū)別:

網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)介紹好的網(wǎng)站是理念、設(shè)計(jì)和技術(shù)的結(jié)合。成都創(chuàng)新互聯(lián)公司擁有的網(wǎng)站設(shè)計(jì)理念、多方位的設(shè)計(jì)風(fēng)格、經(jīng)驗(yàn)豐富的設(shè)計(jì)團(tuán)隊(duì)。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營(yíng)銷(xiāo)思維進(jìn)行網(wǎng)站設(shè)計(jì)、采用先進(jìn)技術(shù)開(kāi)源代碼、注重用戶體驗(yàn)與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺(jué)化效果。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestThreadPool {
 // -newFixedThreadPool與cacheThreadPool差不多,也是能reuse就用,但不能隨時(shí)建新的線程
 // -其獨(dú)特之處:任意時(shí)間點(diǎn),最多只能有固定數(shù)目的活動(dòng)線程存在,此時(shí)如果有新的線程要建立,只能放在另外的隊(duì)列中等待,直到當(dāng)前的線程中某個(gè)線程終止直接被移出池子
 // -和cacheThreadPool不同,F(xiàn)ixedThreadPool沒(méi)有IDLE機(jī)制(可能也有,但既然文檔沒(méi)提,肯定非常長(zhǎng),類(lèi)似依賴(lài)上層的TCP或UDP
 // IDLE機(jī)制之類(lèi)的),所以FixedThreadPool多數(shù)針對(duì)一些很穩(wěn)定很固定的正規(guī)并發(fā)線程,多用于服務(wù)器
 // -從方法的源代碼看,cache池和fixed 池調(diào)用的是同一個(gè)底層池,只不過(guò)參數(shù)不同:
 // fixed池線程數(shù)固定,并且是0秒IDLE(無(wú)IDLE)
 // cache池線程數(shù)支持0-Integer.MAX_VALUE(顯然完全沒(méi)考慮主機(jī)的資源承受能力),60秒IDLE
 private static ExecutorService fixedService = Executors.newFixedThreadPool(6);
 // -緩存型池子,先查看池中有沒(méi)有以前建立的線程,如果有,就reuse.如果沒(méi)有,就建一個(gè)新的線程加入池中
 // -緩存型池子通常用于執(zhí)行一些生存期很短的異步型任務(wù)
 // 因此在一些面向連接的daemon型SERVER中用得不多。
 // -能reuse的線程,必須是timeout IDLE內(nèi)的池中線程,缺省timeout是60s,超過(guò)這個(gè)IDLE時(shí)長(zhǎng),線程實(shí)例將被終止及移出池。
 // 注意,放入CachedThreadPool的線程不必?fù)?dān)心其結(jié)束,超過(guò)TIMEOUT不活動(dòng),其會(huì)自動(dòng)被終止。
 private static ExecutorService cacheService = Executors.newCachedThreadPool();
 // -單例線程,任意時(shí)間池中只能有一個(gè)線程
 // -用的是和cache池和fixed池相同的底層池,但線程數(shù)目是1-1,0秒IDLE(無(wú)IDLE)
 private static ExecutorService singleService = Executors.newSingleThreadExecutor();
 // -調(diào)度型線程池
 // -這個(gè)池子里的線程可以按schedule依次delay執(zhí)行,或周期執(zhí)行
 private static ExecutorService scheduledService = Executors.newScheduledThreadPool(10);
 public static void main(String[] args) {
 DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 List<Integer> customerList = new ArrayList<Integer>();
 System.out.println(format.format(new Date()));
 testFixedThreadPool(fixedService, customerList);
 System.out.println("--------------------------");
 testFixedThreadPool(fixedService, customerList);
 fixedService.shutdown();
 System.out.println(fixedService.isShutdown());
 System.out.println("----------------------------------------------------");
 testCacheThreadPool(cacheService, customerList);
 System.out.println("----------------------------------------------------");
 testCacheThreadPool(cacheService, customerList);
 cacheService.shutdownNow();
 System.out.println("----------------------------------------------------");
 testSingleServiceThreadPool(singleService, customerList);
 testSingleServiceThreadPool(singleService, customerList);
 singleService.shutdown();
 System.out.println("----------------------------------------------------");
 testScheduledServiceThreadPool(scheduledService, customerList);
 testScheduledServiceThreadPool(scheduledService, customerList);
 scheduledService.shutdown();
 } 
 public static void testScheduledServiceThreadPool(ExecutorService service, List<Integer> customerList) {
 List<Callable<Integer>> listCallable = new ArrayList<Callable<Integer>>();
 for (int i = 0; i < 10; i++) {
  Callable<Integer> callable = new Callable<Integer>() {
  @Override
  public Integer call() throws Exception {
   return new Random().nextInt(10);
  }
  };
  listCallable.add(callable);
 }
 try {
  List<Future<Integer>> listFuture = service.invokeAll(listCallable);
  for (Future<Integer> future : listFuture) {
  Integer id = future.get();
  customerList.add(id);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println(customerList.toString());
 }
 public static void testSingleServiceThreadPool(ExecutorService service, List<Integer> customerList) {
 List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>>();
 for (int i = 0; i < 10; i++) {
  Callable<List<Integer>> callable = new Callable<List<Integer>>() {
  @Override
  public List<Integer> call() throws Exception {
   List<Integer> list = getList(new Random().nextInt(10));
   boolean isStop = false;
   while (list.size() > 0 && !isStop) {
   System.out.println(Thread.currentThread().getId() + " -- sleep:1000");
   isStop = true;
   }
   return list;
  }
  };
  listCallable.add(callable);
 }
 try {
  List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable);
  for (Future<List<Integer>> future : listFuture) {
  List<Integer> list = future.get();
  customerList.addAll(list);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println(customerList.toString());
 }
 public static void testCacheThreadPool(ExecutorService service, List<Integer> customerList) {
 List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>>();
 for (int i = 0; i < 10; i++) {
  Callable<List<Integer>> callable = new Callable<List<Integer>>() {
  @Override
  public List<Integer> call() throws Exception {
   List<Integer> list = getList(new Random().nextInt(10));
   boolean isStop = false;
   while (list.size() > 0 && !isStop) {
   System.out.println(Thread.currentThread().getId() + " -- sleep:1000");
   isStop = true;
   }
   return list;
  }
  };
  listCallable.add(callable);
 }
 try {
  List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable);
  for (Future<List<Integer>> future : listFuture) {
  List<Integer> list = future.get();
  customerList.addAll(list);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println(customerList.toString());
 }
 public static void testFixedThreadPool(ExecutorService service, List<Integer> customerList) {
 List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>>();
 for (int i = 0; i < 10; i++) {
  Callable<List<Integer>> callable = new Callable<List<Integer>>() {
  @Override
  public List<Integer> call() throws Exception {
   List<Integer> list = getList(new Random().nextInt(10));
   boolean isStop = false;
   while (list.size() > 0 && !isStop) {
   System.out.println(Thread.currentThread().getId() + " -- sleep:1000");
   isStop = true;
   }
   return list;
  }
  };
  listCallable.add(callable);
 }
 try {
  List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable);
  for (Future<List<Integer>> future : listFuture) {
  List<Integer> list = future.get();
  customerList.addAll(list);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println(customerList.toString());
 }
 public static List<Integer> getList(int x) {
 List<Integer> list = new ArrayList<Integer>();
 list.add(x);
 list.add(x * x);
 return list;
 }
}

當(dāng)前名稱(chēng):Java線程池的幾種實(shí)現(xiàn)方法和區(qū)別介紹實(shí)例詳解-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://aaarwkj.com/article4/gcdie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)企業(yè)建站、Google、網(wǎng)站設(shè)計(jì)公司動(dòng)態(tài)網(wǎng)站、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
日本经典三级在线视频| 91精品国产色综合久久不| 亚洲一区二区三区色偷偷| 日韩欧美另类精品在线| 男女搞j视频网站免费观看| 91成年精品一区在线观看| 亚洲精品熟女国产国产老熟女| 日本大片在线一区二区三区| 91久久福利国产成人精品| 欧美另类精品一区二区| 成人18禁视频免费看| 熟妞人妻精品一区二区视频| 欧美国产日韩激情在线| 日本理伦片一区二区| 欧亚日韩精品一区二区在线| 粉嫩av蜜臀一区二区三区| 亚洲精品国产精品乱码不卞| 国语对白自拍视频在线播放| 亚洲夫妻性生活免费视频| 91麻豆精品一区二区三区| 不卡视频一区中文字幕| 一区二区中文字幕精品| 亚洲日本香蕉视频观看视频| 在线视频一区二区三区精品观看| 久久成人综合亚洲精品欧美| 国内精品亚洲成av人片麻豆| 人妻中文字幕视频在线| 欧美日韩一区二区激情在线| 国产精品久久久av大片| 国产成人久久精品二区三区| 欧美一区二区国产日韩在线| 亚洲美女国产精选999| 可以免费看黄的网久久| 色悠悠粉嫩一区二区三区| 国产一区二区不卡在线播放| 午夜激情视频在线网站| 尤物视频精品在线观看| 91成年精品一区在线观看| av在线中文字幕剧情| 日韩av一区二区免费在线观看| 香蕉夜夜草草久久亚洲香蕉|