zoukankan      html  css  js  c++  java
  • 处理实现线程获取值

    方式一:主线程等待法

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

        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;
        }
    }
  • 相关阅读:
    CF500F New Year Shopping [线段树分治,背包]
    P5344 【XR-1】逛森林[倍增优化建图,zkw线段树优化spfa]
    CF452F Permutation [哈希,树状数组]
    [NOI Online #2 提高组]子序列问题
    牛客挑战赛39题解
    #6036. 「雅礼集训 2017 Day4」编码 [前缀优化2sat]
    CF1156E Special Segments of Permutation [分治,set]
    #6198. 谢特 [后缀自动机,01trie合并,启发式合并]
    P4246 [SHOI2008]堵塞的交通 [动态图连通性]
    CF1096G Lucky Tickets [NTT,多项式快速幂]
  • 原文地址:https://www.cnblogs.com/MrZhangxd/p/12073917.html
Copyright © 2011-2022 走看看