您的位置:首页 > 其它

Thead线程篇之-----多线程 实现 有返回值的功能

2014-07-09 14:58 281 查看
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallBack implements Callable<String>{
private String threadName;
public MyCallBack() {
}
public MyCallBack(String theadName) {
this.threadName = theadName;
}

public String call() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.println(threadName+"===>\t"+i);
}
return threadName+"\t is over";
}
public static void main(String[] args) {
MyCallBack callBack = new MyCallBack("thread0");
FutureTask<String> taskList = new FutureTask<String>(callBack);
Thread t = new Thread(taskList);
t.start();
try {
for (int i = 0; i < 100; i++) {
System.out.println("thread1===>\t"+i);
}
while(!taskList.isDone()){
System.out.println(taskList.get());
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: