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)); } }