Java之使用Callable创建线程

在这里插入图片描述
示例:

package Multithreading;

import java.util.concurrent.*;

public class TestCallable implements Callable {
    
    
    @Override
    public Object call() throws Exception {
    
    
        return "Callable";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
    
        TestCallable callable = new TestCallable();
//        创建服务执行
        ExecutorService service = Executors.newFixedThreadPool(1);
//        提交执行
        Future<String> s1 = service.submit(callable);
//        获取结果
        String str01 = s1.get();
//        关闭服务
        service.shutdown();

        System.out.println(str01);
    }
}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/I_r_o_n_M_a_n/article/details/113919983