这里演示了普通线程池以及带有返回值的线程池的使用方式
- package
com.jadyer.thread.pool; -
- import
java.util.Random; - import
java.util.concurrent.Callable; - import
java.util.concurrent.CompletionService; - import
java.util.concurrent.ExecutionException; - import
java.util.concurrent.ExecutorCompletionServic e; - import
java.util.concurrent.ExecutorService; - import
java.util.concurrent.Executors; - import
java.util.concurrent.Future; - import
java.util.concurrent.TimeUnit; -
-
- public
class ThreadPoolTest { -
public static void main(String[] args) { -
new ThreadPoolTest().threadPoolTest(); -
new ThreadPoolTest().threadPoolScheduledTest(); -
new ThreadPoolTest().threadPoolCallbaleAndFut ureSignTest(); -
new ThreadPoolTest().threadPoolCallbaleAndFut ureMoreTest(); -
} -
-
-
public void threadPoolTest(){ -
//newSingleThreadExecutor()的好处就是,若池中的线程死了,它会把一个"替补的线程"扶上位,即它会保证池中始终有一个线程存在 -
ExecutorService threadPool = Executors.newSingleThreadExecutor(); -
for(int i=1; i<=10; i++) { -
final int task = i; -
threadPool.execute(new MyThread(task)); //注意execute()的返回值是void -
} -
System.out.println("all of 10 tasks have committed......"); -
-
//线程池中的任务均执行完毕后,关闭线程池 -
threadPool.shutdown(); -
} -
-
-
public void threadPoolScheduledTest(){ -
//10秒后自动执行一次 -
//Executors.newScheduledThreadPool(3).schedule(new MyScheduledThread(), 10, TimeUnit.SECONDS); -
//6秒后首次执行,之后每2秒均自动执行一次 -
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new MyScheduledThread(), 6, 2, TimeUnit.SECONDS); -
} -
-
-
public void threadPoolCallbaleAndFut ureSignTest(){ -
ExecutorService threadPool = Executors.newSingleThreadExecutor(); -
Future<String> future = threadPool.submit( -
new Callable<String>() { -
@Override -
public String call() throws Exception { -
Thread.sleep(2000); -
return "张起灵"; -
}; -
} -
); -
System.out.println("等待结果"); -
try { -
System.out.println("拿到结果:" + future.get()); //future.get(4, TimeUnit.SECONDS) -
} catch (InterruptedException e) { -
e.printStackTrace(); -
} catch (ExecutionException e) { -
e.printStackTrace(); -
} -
} -
-
-
public void threadPoolCallbaleAndFut ureMoreTest(){ -
ExecutorService threadPool = Executors.newFixedThreadPool(5); -
CompletionService<Integer> completionService = new ExecutorCompletionServic e<Integer>(threadPool); -
for(int i=1; i<=5; i++){ -
final int taskCode = i; -
completionService.submit( -
new Callable<Integer>(){ -
@Override -
public Integer call() throws Exception { -
Thread.sleep(new Random().nextInt(5000)); -
return taskCode; -
} -
} -
); -
} -
for(int i=0; i<5; i++){ -
try { -
System.out.println(completionService.take().get()); -
} catch (InterruptedException e) { -
e.printStackTrace(); -
} catch (ExecutionException e) { -
e.printStackTrace(); -
} -
} -
} - }
-
-
- class
MyThread implements Runnable{ -
private Integer task; -
public MyThread(Integer task){ -
this.task = task; -
} -
@Override -
public void run() { -
//这里不需要写成j,因为它和threadPoolTest()里面的for()循环中的i并不是同一个方法中的变量,故其不会冲突 -
for(int i=1; i<=10; i++) { -
try { -
Thread.sleep(20); -
} catch (InterruptedException e) { -
e.printStackTrace(); -
} -
System.out.println(Thread.currentThread().getName() + " is looping of " + i + " for task of " + task); -
} -
} - }
-
-
- class
MyScheduledThread implements Runnable{ -
@Override -
public void run() { -
System.out.println("bombing......"); -
} - }
原文链接: http://blog.youkuaiyun.com/hopezhangbo/article/details/7383791