java使用多线程插入数据到数据库

等待线程结束方法

public static void joinThread(List<Thread> workers) {
        //等待线程结束
        workers.forEach(e -> {
            try {
                e.join();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                e.interrupt();
            }
        });
    }

业务

//线程
        List<Thread> workers = new ArrayList<>();
        //线程1
        Thread worker1 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                PerformanceCheck a = new PerformanceCheck();
                a.setCheckEntCount(getRandom());
                performanceTestService.save(a);
            }
        });
        //线程2
        Thread worker2 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                PerformanceCheck a = new PerformanceCheck();
                a.setRegionCode(Func.toLong(getRandom(regionArr)));
                performanceTestService.save(a);
            }
        });
         worker1.start();
        worker2.start();
        //关闭线程
        joinThread(workers);

你可能感兴趣的:(springboot,java,java,算法,开发语言,多线程)