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

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();
        }
    }
    
}

 

posted @ 2014-07-09 14:58  mr.g.  阅读(289)  评论(0编辑  收藏  举报