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

 

Thead线程篇之-----多线程 实现 有返回值的功能,布布扣,bubuko.com

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

上一篇:Python 爬虫—— requests BeautifulSoup


下一篇:C++ 调节图像饱和度