获取线程返回值

主要方法:

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<String> {
    @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<String> 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<String> 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<V> implements RunnableFuture<V> 
public interface RunnableFuture<V> extends Runnable, Future<V>

 

 ....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值