啵溜啵溜neal 2019-07-16 18:03 采纳率: 0%
浏览 176

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

在我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;

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

    求大佬指点迷津,发现自己多线程都没搞明白,心态有点崩
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器