callable接口的多线程实现方式

package com.cxy.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class  MyThread implements Callable<Integer> {


    @Override
    public Integer call() throws Exception {
        return 1024;
    }
}
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask =new FutureTask(new MyThread());

        new Thread(futureTask,"a").start();

        Integer reslut =futureTask.get();
        System.out.println(reslut);
    }
}

 

上一篇:Java实现多线程的四种方式


下一篇:线程