zoukankan      html  css  js  c++  java
  • 如何接收线程池执行后的返回值

    时间紧迫,先简单记一下

    首先定义一个返回值的接受类

    package com.lz.car.facade.dto;
    
    import java.io.Serializable;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    public class MessageDto implements Serializable{
    
        /**
         * 接口返回内容
         */
        private static final long serialVersionUID = 8475031290992534279L;
        /**
         * 返回码
         */
        private String code;
        /**
         * 返回内容
         */
        private String message;
        /**
         * 报文体
         */
        private Object data;
        
        public MessageDto(String code, String message) {
            super();
            this.code = code;
            this.message = message;
        }
        
        public MessageDto() {
            super();
        }
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public Object getData() {
            return data;
        }
        public void setData(Object data) {
            this.data = data;
        }
    
    }

    然后我们定义一个Thread类

        public class ThreadTest extends Thread {
            MessageDto messageDto;
    
            public ThreadTest(MessageDto messageDto) {
                super();
                this.messageDto = messageDto;
            }
            
            public void run() {
                System.out.println(messageDto.getCode());
                try {
                    //随机休眠
                    long num = (long) (Math.random() * 10);
                    System.out.println(num);
                    Thread.sleep(num * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                messageDto.setData(System.currentTimeMillis());
            }
        }

    然后定义一个测试类执行测试

    package com.lz.car.apply.enums;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.ScheduledExecutorService;
    
    /**
     */
    public enum ThreadPoolTest {
    
        public static List<Future> test() {
            ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
            MessageDto messageDto1 = new MessageDto();
            MessageDto messageDto2 = new MessageDto();
            MessageDto messageDto3 = new MessageDto();
            List<Future> futureList = new ArrayList<>();
            
            Future one = executorService.submit(new ThreadTest(messageDto1), messageDto1);
            Future two = executorService.submit(new ThreadTest(messageDto2), messageDto2);
            Future three = executorService.submit(new ThreadTest(messageDto3), messageDto3);
            executorService.shutdown();
            
            futureList.add(one);
            futureList.add(two);
            futureList.add(three);
            
            return futureList;
        }
        
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            List<Future> list = test();
            for(Future future : list) {
                System.out.println(JacksonUtil.toJson(future.get()));
            }
            
        }
    }

    运行main方法得到返回:

    null
    null
    null
    1
    6
    1
    {"data":1552555559590}
    {"data":1552555559590}
    {"data":1552555564590}
  • 相关阅读:
    Vector 、ArrayList、LinkedList比较
    MySQL主从复制
    多浏览器兼容flv视频播放HTML
    [转]javascript对联广告、漂浮广告封装类,多浏览器兼容
    ASP获取json天气信息
    IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书
    Web Service初探
    算法之逆序对
    算法之数组和问题
    重读算法导论之算法基础
  • 原文地址:https://www.cnblogs.com/tranquillity/p/10531765.html
Copyright © 2011-2022 走看看