java中能用多個線程池嗎,Java 多線程,線程池,

 2023-12-25 阅读 27 评论 0

摘要:1. 創建線程池的方法之三: //對于每個任務,如果有空閑的線程可用,立即讓他執行任務,//沒有空閑的線程則創建一個線程。ExecutorService pool = Executors.newCachedThreadPool();//固定大小的線程池,任務數 > 空閑線程數,得

1. 創建線程池的方法之三:

    //對于每個任務,如果有空閑的線程可用,立即讓他執行任務,//沒有空閑的線程則創建一個線程。ExecutorService pool = Executors.newCachedThreadPool();//固定大小的線程池,任務數 > 空閑線程數,得不到服務的任務置于隊列ExecutorService pool = Executors.newFixedThreadPool;//退化的大小為1的線程池,由一個線程逐個執行提交的任務。ExecutorService pool = Executors.newSingleThreadPool();

java中能用多個線程池嗎、2. 把任務交給線程池:

    //返回的對象可以調用isDone(),cancel(),isCancelledFuture<?> submit(Runnable task);//get() 返回指定的result對象Future<T> submit(Runnable task,T result);//返回的對象將在計算結果準備好的時候得到它。Future<T> submit(Callable<T> task);

3.用完一個線程池的時候,調用shutdown() 啟動線程池的關閉序列。被關閉的執行器不再接受新的任務,當任務都結束后,線程池中的線程死亡。

4. 案例:給定一個目錄,查找目錄中文本文檔內容包含指定關鍵字的文檔的數量。

  條件:目錄、關鍵字

  4.1 任務類(線程類)

   /** 線程任務:計算目錄中所有包含給定關鍵字的文件的數量。*/class MatchCounter implements Callable<Integer>{private File directory;private String keyword;private ExecutorService pool;private int count;/*** Constructs a MatchCounter.* @param directory 給定的目錄* @param keyword 關鍵字* @param pool 用來執行任務的線程池*/public MatchCounter(File directory, String keyword, ExecutorService pool){this.directory = directory;this.keyword = keyword;this.pool = pool;}@Overridepublic Integer call(){count = 0;try{File[] files = directory.listFiles();//線程執行結果集合List<Future<Integer>> results = new ArrayList<>();for (File file : files)//遍歷給定目錄中的所有文件//如果是文件夾if (file.isDirectory()){//遞歸MatchCounter counter = new MatchCounter(file, keyword, pool);Future<Integer> result = pool.submit(counter);results.add(result);}//如果是文件,則調用search()方法 看是否包含關鍵字。else{if (search(file)) count++;}for (Future<Integer> result : results)try{int a = result.get();count += result.get();}catch (ExecutionException e){e.printStackTrace();}}catch (InterruptedException e){}return count;}/*** Searches a file for a given keyword.* @param file the file to search* @return true if the keyword is contained in the file*/public boolean search(File file){try{try (Scanner in = new Scanner(file, "UTF-8")){boolean found = false;while (!found && in.hasNextLine()){String line = in.nextLine();if (line.contains(keyword)) found = true;}return found;}}catch (IOException e){return false;}}}

  4.2 主程序

  

    public class ThreadPoolTest{public static void main(String[] args) throws Exception{try (Scanner in = new Scanner(System.in)){System.out.print("請輸入要查找的目錄:");String directory = in.nextLine();System.out.print("請輸入要查找的關鍵字:");String keyword = in.nextLine();ExecutorService pool = Executors.newCachedThreadPool();MatchCounter counter = new MatchCounter(new File(directory), keyword, pool);Future<Integer> result = pool.submit(counter);try{System.out.println(result.get() + " 匹配的文件");}catch (ExecutionException e){e.printStackTrace();}catch (InterruptedException e){}pool.shutdown();int largestPoolSize = ((ThreadPoolExecutor) pool).getLargestPoolSize();System.out.println("線程池最大數量 =" + largestPoolSize);}}}

  4.3 運行結果:

  

?

轉載于:https://www.cnblogs.com/lovleo/p/11320901.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/194853.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息