xiaowei6176

黑马程序员_java线程池

java中构建一个新的线程是有一定代价的,因为涉及到与操作系统的交互,如果程序中创建了大量的生命期很短的线程,应该使用线程池(thread pool),一个线程池中包含许多准备运行的空闲线程,将Runnable对象交给线程池,就会有一个线程调用run方法,当run方法退出时,线程不会死亡,香港虚拟主机,香港服务器,而是在池中准备为下一个请求提供服务。

另一个使用线程池的理由是减少并发线程的数目,创建大量线程会大大降低性能甚至使虚拟机崩溃,如果有一个会创建许多线程的算法,应该使用一个线程数“固定的”线程池以限制并发线程的总数。

执行器(Executor)类有许多静态工厂方法用了构建线程池,主要有以下:

newCachedThreadPool 必要时创建线程,空闲线程会被保留60秒;

newFixedThreadPool 该池保护固定数量的线程,空闲线程会一直被保留

newSingleThreadExecutor 只有一个线程的池,该线程顺序执行每一个提交的任务,美国服务器,类似与Swing事件分配线程

newScheduledThreadPool 用于预定执行而构建的固定线程池,替代java.util.Timer

newSingleThreadScheduledExecutor 用于预定执行而构建的单线程池

以下程序将遍历一个目录下的所有文件并且找出包含指定关键字的文件数目,最后将打印出执行池中的最大的线程数,我们不能通过ExecutorService这个接口得到最大的线程数,必须将该Pool对象转化为ThreadPoolExecutor类对象

1 package cn.andy.java.thread; 23 import java.io.File; 4 import java.io.FileInputStream; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 import java.util.concurrent.Callable; 8 import java.util.concurrent.ExecutorService; 9 import java.util.concurrent.Executors; 10 import java.util.concurrent.Future; 11 import java.util.concurrent.ThreadPoolExecutor; 12 public class ThreadPoolTest { 13 1415public static void main(String[] args) { 16Scanner in=new Scanner(System.in); 17System.out.println(“输入目录(e.g. G:/JavaDoc): “); 18String directory=in.nextLine(); 19System.out.println(“输入查找的关键字(e.g. VoidType): “); 20String keyword=in.nextLine(); 2122ExecutorService pool=Executors.newCachedThreadPool(); 2324 25MatchCounter counter=new MatchCounter(new File(directory), keyword, pool); 26Future<Integer> result=pool.submit(counter); 27try { 28System.out.println(“得到”+result.get()+” 个匹配文件” ); 29} catch (Exception e) { 30e.printStackTrace(); 31} 32pool.shutdown(); 33int largestPoolSize=((ThreadPoolExecutor)pool).getLargestPoolSize(); 34System.out.println(“池中的最大线程数是= “+largestPoolSize); 35} 36/** 37* 得到一个目录总包含关键字的文件及其子文件的数目 38* @author xiaowei 39* 40*/ 41static class MatchCounter implements Callable<Integer>{ 42private File directory; 43private String keyword; 44private ExecutorService pool; 45private int count; 4647public MatchCounter(File directory, String keyword, ExecutorService pool) { 48super(); 49this.directory = directory; 50this.keyword = keyword; 51this.pool = pool; 52} 53/** 54* 用关键来查找文件 55* @param file 要查找的文件目录 56* @return true 如果某个文件中包含关键字了返回true 57*/ 58public boolean serach(File file){ 59try { 60Scanner in=new Scanner(new FileInputStream(file)); 61boolean found=false; 62while(!found&&in.hasNext()){ 63String line=in.nextLine(); 64if(line.contains(keyword)){ 65found=true; 66} 67} 68in.close(); 69return found; 70} catch (Exception e) { 71return false; 72} 73} 74@Override 75public Integer call() throws Exception { 76count=0; 77try { 78File[] files=directory.listFiles(); 79ArrayList<Future<Integer>> results=new ArrayList<Future<Integer>>(); 80for(File file:files){ 81if(file.isDirectory()){ 82MatchCounter counter=new MatchCounter(file, keyword, pool); 83Future<Integer> result=pool.submit(counter); 84results.add(result); 85}else{ 86if(serach(file)) count++; 87} 88for(Future<Integer> result:results){ 89try { 90count+=result.get(); 91} catch (Exception e) { 92e.printStackTrace(); 93} 94} 95 96} 97} catch (Exception e) { 9899}100return count;101}102103}104 105 }

posted on

Copyright ©2012 xiaowei6176 Powered by: 博客园 模板提供:沪江博客

来说是非者,便是是非人。

xiaowei6176

相关文章:

你感兴趣的文章:

标签云: