带返回值的线程

来源:互联网 发布:淘宝开店去哪里找货源 编辑:程序博客网 时间:2024/06/10 07:25
public class PostThread implements Callable {private String s;public PostThread (String str) {this.s = h;}public Object call() throws Exception {return this.s;}}

使用的时候:

ExecutorService pool = Executors.newFixedThreadPool(2); Callable c1 = new PostThread("hello");Future f1 = pool.submit(c1);
               String str = (String)f1.get();