简单 带返回值的线程 callable
来源:互联网 发布:淘宝迷你变色龙活体 编辑:程序博客网 时间:2024/05/17 02:01
测试带返回值的线程,要实现callable接口,重写call方法,在返回值的时候用get方法获取到返回值。
1.内部类实现接口
import java.util.ArrayList;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class TestCallableAndFuture {TestCallableAndFuture() {}public static void main(String[] args) {ExecutorService pool = Executors.newScheduledThreadPool(3);List<Future<String>> futureList = new ArrayList<>();TestCallableAndFuture t = new TestCallableAndFuture();for(int i = 0; i < 5; i++) {int leng = i * 1000;futureList.add(pool.submit(t.new myCallable(leng)));}pool.shutdown();for(int i = 0; i < 5; i++) {try {System.out.println(futureList.get(i).get());} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ExecutionException e) {// TODO Auto-generated catch blocke.printStackTrace();};}}class myCallable implements Callable {private int length;myCallable(int length) {this.length = length;}@Overridepublic String call() throws Exception {// 线程调用call()方法for(int i = 0; i < length; i++) {System.out.println("线程" + length + ", i = " + i);}return "线程" + length;}}}代码中实现多线程输出。
这是截图输出,可以看见,2,、3、4分别运行。
2.直接实现callable接口
注意会报Cannot refer to a non-final variable j inside an inner class defined in a different method这个错误,因为callable要求内部的数据为final类型。如果,两个final指针指向同一个对象地址,会在线程执行过程中改变值,发生错误。要保证对象不变形。可用clone来复制对象。
final int[] leng = {10000,20000,30000,40000};for(int j = 0; j < 4; j++) {futureList.add(pool.submit(new Callable() {@Overridepublic String call() throws Exception {for(int i = 0; i < leng.length; i++) {System.out.println("线程" + leng[j] + ", i = " + i);}return "线程" + leng[j];}}));}
这样会报错,因为j不是final
3.用futuretask接收执行的callable方法。
与future不同的是futuretask实现了Runnable接口,可以对线程的状态进行查询。在我的使用中不常见。
下面的例子执行和1的结果类似
import java.util.ArrayList;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.FutureTask;public class TestCallableAndFuture {TestCallableAndFuture() {}public static void main(String[] args) {ExecutorService pool = Executors.newScheduledThreadPool(3);List<FutureTask<String>> futuretaskList = new ArrayList<>();TestCallableAndFuture t = new TestCallableAndFuture();for(int i = 0; i < 6; i++) {int leng = i * 1000 * 100;FutureTask<String> thisFutureTask = new FutureTask<String>(t.new myCallable(leng));futuretaskList.add(thisFutureTask);pool.execute(thisFutureTask);//futureList.add(pool.submit(t.new myCallable(leng)));}pool.shutdown();for(int i = 0; i < 6; i++) {try {System.out.println(futuretaskList.get(i).get());} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ExecutionException e) {// TODO Auto-generated catch blocke.printStackTrace();};}}class myCallable implements Callable {private int length;myCallable(int length) {this.length = length;}@Overridepublic String call() throws Exception {// 线程调用call()方法for(int i = 0; i < length; i++) {System.out.println("线程" + length + ", i = " + i);}return "线程" + length;}}}
0 0
- 简单 带返回值的线程 callable
- 带返回值的线程:Callable、Future和FutureTask
- java 带返回值线程 Callable
- 带返回值的多线程callable
- Callable有返回值的线程
- java5 带有返回值的线程 callable
- java Callable有返回值的线程
- 有返回值的线程 -- Callable
- Callable实现带有返回值的线程
- java线程:关于Callable和Future的简单使用:有返回值的线程
- Callable 获取线程返回值
- Callable实现线程返回值
- Java线程中带有返回值的线程Callable
- Java多线程研究06-带返回值的线程定义接口Callable以及Future,FutureTask的使用
- java带返回值的线程示例,简单方便
- 带返回值的线程
- 带返回值的线程
- java 线程池 执行callable 的返回值问题
- ath9k usb wifi 网卡驱动浅析
- 简单html
- java的(PO,VO,TO,BO,DAO,POJO)解释
- 4种控件自绘技术类型分析
- 《深入理解Java虚拟机》类文件结构
- 简单 带返回值的线程 callable
- VC设置软件开机自启
- 初次的接触
- 最近在整linux
- linux下如何反汇编
- 简单HTML
- JavaScript高级程序设计9--DOM扩展
- Algorithm-Percolation-code
- java集合类学习心得