zoukankan      html  css  js  c++  java
  • Java多线程-处理线程的返回值

    一、主线程等待法:优点:实现简单,缺点:代码冗余

    package com.test.thread;
    
    public class CycleWait implements Runnable {
        private String value;
    
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            value = "we have data now";
        }
    
        public static void main(String[] args) throws InterruptedException {
            CycleWait cycleWait = new CycleWait();
            Thread t = new Thread(cycleWait);
            t.start();
            while (cycleWait.value == null) {
                Thread.sleep(100);
            }
            System.out.println(cycleWait.value);
        }
    }
    

    运行结果:

    we have data now

    二、使用Thread类的join()阻塞当前线程,以等待子线程处理完毕。优点:比“主线程等待法”更简单 缺点:粒度不够细

    package com.test.thread;
    
    public class CycleWait implements Runnable {
        private String value;
    
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            value = "we have data now";
        }
    
        public static void main(String[] args) throws InterruptedException {
            CycleWait cycleWait = new CycleWait();
            Thread t = new Thread(cycleWait);
            t.start();
           // join方法,在start后
            t.join();
            System.out.println(cycleWait.value);
        }
    }
        

    三、通过Callable接口实现:通过FutureTask 或者 线程池获取

     1、future task

    MyCallable.class:
    
    package com.test.thread;
    
    import java.util.concurrent.Callable;
    // 实现callable接口
    public class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            String value = "test";
            System.out.println("ready to work");
            Thread.sleep(5000);
            System.out.println("task done");
            return value;
        }
    }
    
    FutureTaskDemo.class:
    package com.test.thread;
    
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    //future task
    public class FutureTaskDemo {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            FutureTask<String> task = new FutureTask<String>(new MyCallable());
            new Thread(task).start();
            if (!task.isDone()) {
                System.out.println("task has not finished, please wait!");
            }
            System.out.println("task return:" + task.get());
        }
    }

    2、线程池

    TreadPoolDemo.class:
    package com.test.thread;
    
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class TreadPoolDemo {
        public static void main(String[] args) {
            // 创建线程池
            ExecutorService executorService =
                    Executors.newCachedThreadPool();
            // 向线程池中提交任务
            Future<String> future = executorService.submit(new MyCallable());
            // 判断任务是否完成
            if (!future.isDone()) {
                System.out.println("task not finished, please wait~~");
            }
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } finally {
                // 将线程池关闭
                executorService.shutdown();
            }
        }
    }
    

      

  • 相关阅读:
    legend3---图片裁剪实例代码
    JPG 和 JPEG 格式有什么区别
    jpg与png图片的优缺点
    心得体悟帖---200320(legend3它的真实意义,在于资质提升,一举多得)
    心得体悟帖---200320(觉得录课录不下去了,可以去招聘市场找找激励,这个真的特别有效)
    心得体悟帖---200320(我必不甘于人之下,所以还是得自己干,自己好好录课)
    ES6参考---Promise对象
    范仁义js课程---70、sort方法
    JS实现异步编程的4种方法(总结)
    JS执行机制详解
  • 原文地址:https://www.cnblogs.com/starstarstar/p/11222223.html
Copyright © 2011-2022 走看看