获取线程返回值

主要方法:

1.  主线程等待法;

2. 通过调用Thread类的join方法阻塞当前线程以等待子线程处理完毕;

3. 通过Callable接口实现:FutureTask / 线程池获取; (重点)

1.  主线程等待法;

public class MainThreadCycleWait implements Runnable {
    private String value;

    @Override
    public void run() {
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        value = " finished ";
    }

    // 方法一 : 主线程循环等待
    public static void main(String[] args) throws InterruptedException {
        MainThreadCycleWait runnable = new MainThreadCycleWait();
        Thread thread = new Thread(runnable);
        thread.start();
        while (runnable.value == null) {
            Thread.currentThread().sleep(100);
        }
        System.out.println("value = " + runnable.value);
    }
}

2. 通过调用Thread类的join方法阻塞当前线程以等待子线程处理完毕;

public class MainThreadCycleWait implements Runnable {
    private String value;

    @Override
    public void run() {
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        value = " finished ";
    }

    public static void main(String[] args) throws InterruptedException {
        MainThreadCycleWait runnable = new MainThreadCycleWait();
        Thread thread = new Thread(runnable);
        thread.start();
        thread.join(); // 方法二 : 使用join() 需要调用join的线程执行完成,当前的线程(主线程)才能继续执行 
        System.out.println("value = " + runnable.value);
    }
}

3. 通过Callable接口实现

定义MyCallable实现Callable接口

public class MyCallable implements Callable {
    @Override
    public String call() throws Exception {
        String value = "result ";
        System.out.println(" ready to work ");
        Thread.currentThread().sleep(1000);
        System.out.println(" finished ");
        return value;
    }
}

3.1 jdk5版本之后,实现Callable接口之后,可以获取Future对象,通过调用Future对象的get方法

public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
//        if (!futureTask.isDone()){
//            System.out.println(" task has not finished ..... ");
//        }
        String result = futureTask.get();
        System.out.println(result);
    }
}

3.2 通过线程池 

public class ThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        Future future = cachedThreadPool.submit(new MyCallable());
        try {
            System.out.println("result =====>>>>> " + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            cachedThreadPool.shutdown();
        }
    }
}

FutureTask源码追踪

public class FutureTask implements RunnableFuture 
public interface RunnableFuture extends Runnable, Future

 获取线程返回值_第1张图片

 ....

你可能感兴趣的:(JDK,java)