多线程方法将对象添加到同步的 ArrayList

MultiThread method adding Object to synchronized ArrayList

我的程序有问题。在循环中,我在多线程(这是我练习的前提)中创建一个新的雇员(通过从假名生成器网站自动下载数据),如果线程完成创建雇员对象,应该将其添加到同步列表但出现问题并列出是空的。我卡住了。有人知道怎么回事吗?

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) throws Exception {
        final String[][] e = new String[1][1];
        EmployeesGenerator a=new EmployeesGenerator();
        List<Employees> list= Collections.synchronizedList(new ArrayList<>());
        ExecutorService executorService= Executors.newFixedThreadPool(10);

       for(int x=0; x<10;x++){
           executorService.submit(()->{
                       try {
                           e[0] =a.getEmployees();
                       } catch (Exception ex) {
                           ex.printStackTrace();
                       }
                       list.add(new Employees(e[0][0], e[0][1], e[0][2], e[0][3], e[0][4]));
                        }
                   );
       }
       executorService.shutdown();

        System.out.println(list.size());
    }
}

您应该在 executorService 关闭后等待线程终止:

try {
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
    }
    System.out.println(list.size());