线程池与Callable更配哦
来源:互联网 发布:新闻表数据库设计 编辑:程序博客网 时间:2024/05/20 23:02
概述
Callable介绍见:http://blog.csdn.net/zengmingen/article/details/53288119
多线程介绍见:http://blog.csdn.net/zengmingen/article/details/53284999
代码
TaskCallable.java
package multithreading.pool;import java.util.concurrent.Callable;public class TaskCallable implements Callable<String>{/**线程编号*/private int tNo;public TaskCallable(int tNo){this.tNo=tNo;}@Overridepublic String call() throws Exception {String tName=Thread.currentThread().getName();long currentTimeMillis = System.currentTimeMillis();System.out.println(tNo+"-"+tName+" start time is:"+currentTimeMillis/1000);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(tNo+"-"+tName+ " is working...");return "the thread is "+tNo;}}
TestPool.java
package multithreading.pool;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class TestPool {public static void main(String[] args) throws Exception, ExecutionException {List<Future<String>> futures = new ArrayList<Future<String>>();ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);for (int i = 0; i < 5; i++) {Future<String> future = newFixedThreadPool.submit(new TaskCallable(i));futures.add(future);}// 打印结果for (Future<String> f : futures) {boolean done = f.isDone(); // 从结果的打印顺序可以看到,即使未完成,也会阻塞等待System.out.println(done ? "已完成" : "未完成");//从Future中get结果,这个方法是会被阻塞的,一直要等到线程任务返回结果System.out.println("已完成线程返回future结果: " + f.get());}newFixedThreadPool.shutdown();}}
运行结果
0-pool-1-thread-1 start time is:1479808023
未完成
1-pool-1-thread-2 start time is:1479808023
0-pool-1-thread-1 is working...
1-pool-1-thread-2 is working...
已完成线程返回future结果: the thread is 0
已完成
已完成线程返回future结果: the thread is 1
未完成
2-pool-1-thread-1 start time is:1479808024
3-pool-1-thread-2 start time is:1479808024
3-pool-1-thread-2 is working...
2-pool-1-thread-1 is working...
4-pool-1-thread-2 start time is:1479808026
已完成线程返回future结果: the thread is 2
已完成
已完成线程返回future结果: the thread is 3
未完成
4-pool-1-thread-2 is working...
已完成线程返回future结果: the thread is 4
0 0
- 线程池与Callable更配哦
- 线程学习二:线程池执行Runnable与Callable
- 线程池-callable任务
- Callable与Futrue创建线程
- 线程池Executors、Callable与Future的应用
- 线程池 Callable实现多线程
- 线程池中使用Callable
- java5线程 Callable与Future的应用
- java线程中Callable与Future
- Java Callable与Future线程详解
- Android线程—Callable与Runnable
- java之多线程(3)--线程池&Callable
- Java 多线程线程池 - Callable和Future
- JAVA 线程池 ExecutorService Callable Future
- 再说Runnable、Callable、Future、线程池
- (8)java5线程 Callable与Future的应用
- (8)java5线程 Callable与Future的应用
- Java 线程与并发研究系列二(实现Callable接口)
- 恢復UFT12版本試用的方法
- java 操作redis 数据库一:基本操作
- 如何使用阿里百川hotfix热修复功能(三)
- 管道的读写操作
- Android View不为人知的各种小属性和一些有用的小方法
- 线程池与Callable更配哦
- JNI中jstring 和char* 之间的转换方法
- 在vspackage中强制更新command的ui状态(Forcing Command UI refresh)
- Spring+Mybatis+SpringMVC+Maven+MySql搭建样例
- Oracle分析函数总结
- [python]python验证码识别
- 编程学习之道
- 链表学习总结
- Android制作和使用Nine-Patch图片