分别用多线程处理两段代码,怎样写才能实现

在我controller请求层里面有接收到页面传过来的文档后,分别对每一条数据进行对应的逻辑操作,怎样才能保证for循环里面的两条代码分别用多线程来执行呢?

代码如下:
public Map upload(@RequestParam(value = "path", required = false) String path,
@RequestParam(value = "dns1", required = false) String dns1,
@RequestParam(value = "dns2", required = false) String dns2,
HttpServletResponse response,HttpServletRequest request)throws Exception {
Map map = new HashMap();

    String webPath=request.getServletContext().getRealPath("");
    //读取TXT文件里面的域名信息
    List<String> importTxt = TextUtils.importTxt(new File(webPath + path));
    Dns dns = new Dns();
    List<Dns> original_datas = new ArrayList<>();//dns1数据
    List<Dns> this_datas = new ArrayList<>();//dns2数据
    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (String domain : importTxt) {
        if (domain == null ||domain.isEmpty()){
            dns.setDomain("域名空");
        }else {
            //解析两份数据(这两条代码需要分别用多线程来执行)
            //original_datas.addAll(DnsUtils.getIps(domain, dns1));
            //this_datas.addAll(DnsUtils.getIps(domain, dns2));

            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        original_datas.addAll(DnsUtils.getIps(domain, dns1));
                        System.out.println("线程1="+original_datas.toString());
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        this_datas.addAll(DnsUtils.getIps(domain, dns2));
                        System.out.println("线程2="+this_datas.toString());
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            pool.execute(thread1);
            pool.execute(thread2);
        }
    }
    pool.shutdown();
    Thread.sleep(5000);
    System.out.println("original_datas="+original_datas.toString());
    System.out.println("this_datas="+this_datas.toString());
    //数据对比
    List<String> dnsDuibi = MyUtil.dnsDuibi(original_datas,this_datas);

    dnsService.addList(original_datas);
    dnsService.addList(this_datas);

    map.put("msg", "上传成功");
    map.put("success", true);
    //map.clear();
    //  原数据比  this_data
    map.put("original_datas", original_datas);
    map.put("this_datas", this_datas);
    map.put("dnsDuibi", dnsDuibi);
    return map;

    我上面自己写的那个线程是有问题的,因为不睡眠的话,系统始终比线程块快,会造成外面的输出
    会没有值,而里面的输出有值的情况,而且这样写效率也不行.

    求大佬指点迷津,发现自己多线程都没搞明白,心态有点崩
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问