您的位置:首页 > 编程语言 > Java开发

java 多线程( Future Callable)

2016-03-18 19:37 323 查看
import java.util.concurrent.*;
import java.util.*;

public class Threads{
public static void main(String args[])throws InterruptedException,ExecutionException{
int size = 5;
ExecutorService pool = Executors.newFixedThreadPool(size);
List<Future<?>> list = new ArrayList<Future<?>>();
for(int i = 0; i < size ; i++){
Callable<?> c = new MCallable(i+" ");
Future<?> f = pool.submit(c);
list.add(f);
}
pool.shutdown();
for(Future<?> f : list){
System.out.println(">>>" + f.get().toString());
}

}
}
class MCallable implements Callable<Object>{
private String str;
MCallable(String str){
this.str = str;
}
public Object call(){
return str + "completed!";
}
}
root@ubuntu:/home/xiuye/workspace# javac Threads.java
root@ubuntu:/home/xiuye/workspace# java Thread
Error: Could not find or load main class Thread
root@ubuntu:/home/xiuye/workspace# java Threads
>>>0 completed!
>>>1 completed!
>>>2 completed!
>>>3 completed!
>>>4 completed!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: