Java 在一个类中创建DTO对象,并使用ExecuterService在另一个对象中处理DTO对象

Java 在一个类中创建DTO对象,并使用ExecuterService在另一个对象中处理DTO对象,java,multithreading,Java,Multithreading,我试图在一个类中创建一个DTO类,并在另一个线程中处理该DTO类。但是我在下面创建的编码创建了所有的DTO,然后只有我能够得到作为一个整体的列表,而不是作为一个DTO public class Executer { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService service = Executors.newF

我试图在一个类中创建一个DTO类,并在另一个线程中处理该DTO类。但是我在下面创建的编码创建了所有的DTO,然后只有我能够得到作为一个整体的
列表
,而不是作为一个DTO

public class Executer {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService service = Executors.newFixedThreadPool(5);

        Set<Callable<List<EmpDTO>>> callables = new HashSet<Callable<List<EmpDTO>>>();

        callables.add(new Callable<List<EmpDTO>>() {
            public List<EmpDTO> call() throws Exception {
                List<EmpDTO> empDTO=new ArrayList<EmpDTO>();
                for(int i=1;i<=100;i++){
                    EmpDTO emp = new EmpDTO();
                    emp.name ="name";
                    emp.employeeId=i;
                    emp.age="27";
                    empDTO.add(emp);
                    System.out.println("added "+i);
                }

                return empDTO;
            }
        });

        List<Future<List<EmpDTO>>> futures = service.invokeAll(callables);

        for(Future<List<EmpDTO>> future : futures){
            System.out.println("future.get = " + future.get());
        }





    }

}
公共类执行器{
公共静态void main(字符串[]args)引发InterruptedException、ExecutionException{
ExecutorService=Executors.newFixedThreadPool(5);
Set callables=newhashset();
add(newcallable()){
公共列表调用()引发异常{
List empDTO=new ArrayList();

对于(int i=1;我真的不清楚您想要实现什么。如果这是基本的顺序代码,那么整个代码将更加简单和快速。在内存中创建100个对象比创建线程池更快。我只做了一个示例,但实时场景将是成千上万,DTO对象也将是大的。所以只想o使用简单逻辑检查它即使这样,使用顺序代码可能会更快(更简单)。但无论如何,这并不能回答我的问题:你想要实现什么?你想要在主线程中创建什么DTO(你现在没有在主线程中创建任何DTO),以及应该在另一个线程中执行什么处理?将for循环上移一级,以便创建多个EmpDto可调用项,而不是一个EmpDto列表可调用项。此外,您可能希望使用CompletionService按照对象的构造顺序接收对象。