如何接收线程池执行后的返回值

时间紧迫,先简单记一下

 

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

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}

 

posted @ 2019-03-14 17:36  漂泊之枫  阅读(2285)  评论(0编辑  收藏  举报