测试带返回值的线程,要实现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 block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
class myCallable implements Callable {
private int length;
myCallable(int length) {
this.length = length;
}
@Override
public 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() {
@Override
public 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 block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
class myCallable implements Callable {
private int length;
myCallable(int length) {
this.length = length;
}
@Override
public String call() throws Exception {// 线程调用call()方法
for(int i = 0; i < length; i++) {
System.out.println("线程" + length + ", i = " + i);
}
return "线程" + length;
}
}
}