CallableTest.java 735 字节
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1 2 3 4 5 6 7
package org.hongxi.java.util.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
武汉红喜's avatar
java  
武汉红喜 已提交
8
 * @author shenhongxi 2019/8/8
武汉红喜's avatar
武汉红喜 已提交
9 10 11 12 13
 */
public class CallableTest {

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
武汉红喜's avatar
java  
武汉红喜 已提交
14
        // submit(Callable<T>)
武汉红喜's avatar
武汉红喜 已提交
15 16 17 18 19 20 21
        Future<String> future = executorService.submit(() -> {
           Thread.sleep(2000);
           return "hello";
        });
        long begin = System.currentTimeMillis();
        System.out.println(future.get());
        System.out.println(System.currentTimeMillis() - begin);
武汉红喜's avatar
武汉红喜 已提交
22
        executorService.shutdown();
武汉红喜's avatar
武汉红喜 已提交
23 24
    }
}