• java多线程并发


        @Test
        public void testRunBatchClient() {
            long start = System.currentTimeMillis();
            runBatchClient(30);
            long end = System.currentTimeMillis();
            System.out.println("子线程执行时长:" + (end - start));
    
        }
    
        private void runBatchClient(int clients){
            CountDownLatch countDownLatch = new CountDownLatch(clients);
            ClientMock clientMock = new ClientMock(countDownLatch);
            for (int i = 0; i < clients; i++) {
                Thread thread = new Thread(clientMock);
                thread.start();
            }
            try {
                // 阻塞当前线程,直到倒数计数器倒数到0
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
        class ClientMock implements Runnable {
            private CountDownLatch countDownLatch;
    
            public ClientMock(CountDownLatch countDownLatch) {
                this.countDownLatch = countDownLatch;
            }
    
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "子线程开始");
                long start = System.currentTimeMillis();
                try {
    
                    UploadZIP();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }
                long end = System.currentTimeMillis();
                System.out.println(Thread.currentThread().getName() + "子线程结束,时长:" + (end - start));
            }
        }
  • 相关阅读:
    如何对一台服务器进行免密操作
    MySQL 中的自增主键
    mysql 添加多版本数据库
    Group by 优化
    join 查询优化
    CCF2020-09-Python题解
    CCF2019-09-Python题解
    CCF2019-12-Python题解
    CCF202006-Python题解
    差分约束
  • 原文地址:https://www.cnblogs.com/puqiuxiaomao/p/3898810.html
Copyright © 2020-2023  润新知