处理实现线程获取值

方式一:主线程等待法

如果未获取到值,则主线程等待,一直到获取值为止。

    package mrzhangxd.xyz.thread;
    
    /**
     * CopyRight (C),2019
     *
     * @ClassName: ThreadWait
     * @Auther: zhangxiaodong3
     * @Created: 2019/12/2015:11
     * @Description: 处理实现线程获取值
     */
    
    /**
      * 如果未获取到值,则主线程等待,一直到获取值为止。
      */
    public class ThreadWait implements Runnable{
    
        private String value;
    
        public static void main(String[] args){
    
            ThreadWait tw = new ThreadWait();
            Thread t = new Thread(tw);
            t.start();
    
            //TODO:如果未获取到值,则主线程等待,一直获取到值为止
            while (tw.value == null){
    
                try {
                    //主线程等待法
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("Value:"+tw.value);
            //输出结果:Value:My name is MrZhangxd!!!
        }
        @Override
        public void run() {
    
    
            try {
    
                Thread.sleep(5000);
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            value = "My name is MrZhangxd!!!";
    
        }
    }

方式二:join()

使用 Thread 的 join() 阻塞当前线程以等待子线程处理完毕。

package mrzhangxd.xyz.thread;

/**
 * CopyRight (C),2019
 *
 * @ClassName: ThreadWaitJoin
 * @Auther: zhangxiaodong3
 * @Created: 2019/12/2015:26
 * @Description: 处理实现线程获取值
 */

//使用 Thread 的 join() 阻塞当前线程以等待子线程处理完毕。

public class ThreadWaitJoin implements Runnable {

    private  String value;

    public static void main(String[] args) throws InterruptedException {

        ThreadWaitJoin twj = new ThreadWaitJoin();
        Thread t = new Thread(twj);
        t.start();

        //TODO:使用 Thread 的 join 方法阻塞当前线程,等待子线程执行完毕
        t.join();//阻塞当前主线程,直到 t 线程执行完毕
        System.out.println("Value:" + twj.value);

        //输出结果:
        //Value:My name is MrZhangxd!!!
    }

    @Override
    public void run() {


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        value = "My name is MrZhangxd!!!";

    }
}

方式三:Callable + FutureTask

实现 Callablee 接口,使用 FutureTask 接收 call() 方法返回的数据。

package mrzhangxd.xyz.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * CopyRight (C),2019
 *
 * @ClassName: ThreadWaitFutureTask
 * @Auther: zhangxiaodong3
 * @Created: 2019/12/2015:45
 * @Description: 处理实现线程获取值
 */

//实现 Callablee 接口,使用 FutureTask 接收 call() 方法返回的数据。
public class ThreadWaitFutureTask implements Callable<String> {

    private  String value;

    public static void main(String[] args)throws InterruptedException, ExecutionException{

        ThreadWaitFutureTask twft = new ThreadWaitFutureTask();

        FutureTask ft = new FutureTask(twft);

        Thread t = new Thread(ft);

        t.start();


        while (!ft.isDone()){
                // FutureTask 的 get() 方法会一直阻塞,一直会等待任务执行完毕,才返回。
                Thread.sleep(1000);

        }

            System.out.println("Vaule:"+ft.get());

        //输出结果:
        //Vaule:My  name is MrZhangxd!!!

    }
    @Override
    public String call() throws Exception {

        Thread.sleep(5000);
        value = "My  name is MrZhangxd!!!";
        return value;
    }

}

方式四:线程池 + Future

线程池执行任务,返回的结果使用 Future 接收。

package mrzhangxd.xyz.thread;

import java.util.concurrent.*;

/**
 * CopyRight (C),2019
 *
 * @ClassName: ThreadWaitFuture
 * @Auther: zhangxiaodong3
 * @Created: 2019/12/20 15:58
 * @Description: 处理实现线程获取值
 */

//线程池执行任务,返回的结果使用 Future 接收。

public class ThreadWaitFuture implements Callable<String> {

    private String vaule;

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService service = Executors.newCachedThreadPool();
        ThreadWaitFuture twf = new ThreadWaitFuture();
        //将任务提交给线程池处理
        Future<String>  future = service.submit(twf);

        while (!future.isDone()){

            Thread.sleep(1000);

        }

        System.out.println("Value:"+future.get());
        //关闭线程池
        service.shutdown();

        //运行结果:
        //Vaule:My name is MrZhangxd!!!

    }

    @Override
    public String call() throws Exception {

        Thread.sleep(5000);
        vaule = "My name is MrZhangxd!!!";
        return vaule;
    }
}
posted @ 2019-12-20 16:43  猿校长  阅读(403)  评论(0编辑  收藏  举报