待一切线程皆筹办好后,按下开启按钮,就能够真实的倡议并收恳求了。
-
- package com.test;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.concurrent.CountDownLatch;
-
- public class LatchTest {
-
- public static void main(String[] args) throws InterruptedException {
- Runnable taskTemp = new Runnable() {
-
- // 留意,此处长短线程宁静的,留坑
- private int iCounter;
-
- @Override
- public void run() {
- for(int i = 0; i < 10; i++) {
- // 倡议恳求
- // HttpClientOp.doGet("https://www.百度.com/");
- iCounter++;
- System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
-
- LatchTest latchTest = new LatchTest();
- latchTest.startTaskAllInOnce(5, taskTemp);
- }
-
- public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
- final CountDownLatch startGate = new CountDownLatch(1);
- final CountDownLatch endGate = new CountDownLatch(threadNums);
- for(int i = 0; i < threadNums; i++) {
- Thread t = new Thread() {
- public void run() {
- try {
- // 使线程正在吹廊待,当开端门翻开时,一同涌进梅市
- startGate.await();
- try {
- task.run();
- } finally {
- // 将完毕闷骢1,加到0时,就能够开启完毕门了
- endGate.countDown();
- }
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- }
- };
- t.start();
- }
- long startTime = System.nanoTime();
- System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
- // 果开启梅驶需一个开闭,以是坐马便开启开端门
- startGate.countDown();
- // 等等完毕门开启
- endGate.await();
- long endTime = System.nanoTime();
- System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
- return endTime - startTime;
- }
- }
赶钙代码
其施行结果以下图所示: httpClientOp 东西类,可使用 成生的东西包,也能够本人写一个扼要的会见办法,参考以下:赶钙代码
如上,就能够倡议真实的并收恳求了。
并收恳求操纵流程表示图以下: 此处设置了一讲门,以包管一切线潮以同时见效。可是,此处的同时启动,也执偾言语层里的工具,也并不是尽对的同时并收。详细的挪用借要依靠于CPU个数,线程数及操纵体系当边程调理功用等,不外我们也无需纠结于那些了,重面正在于了解道理!取 CountDownLatch 有相似功用的,另有个东西栅栏 CyclicBarrier, 也是供给一个等候一切线程抵达某一面后,再一同开端某个行动,结果分歧,不外栅栏的目标的确比力地道,便是等候一切线程抵达,而前里道的闭锁 CountDownLatch 固然完成的也是一切线程抵达后再开端,可是他的触收面实际上是 最初那一个开闭,以是偏重面是纷歧样的。
简朴看一下栅揽嗲怎样完成实正同时并收呢?示比方下:
- // 取 闭锁 构造分歧
- public class LatchTest {
-
- public static void main(String[] args) throws InterruptedException {
-
- Runnable taskTemp = new Runnable() {
-
- private int iCounter;
-
- @Override
- public void run() {
- // 倡议恳求
- // HttpClientOp.doGet("https://www.百度.com/");
- iCounter++;
- System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
- }
- };
-
- LatchTest latchTest = new LatchTest();
- // latchTest.startTaskAllInOnce(5, taskTemp);
- latchTest.startNThreadsByBarrier(5, taskTemp);
- }
-
- public void startNThreadsByBarrier(int threadNums, Runnable finishTask) throws InterruptedException {
- // 设置栅栏消除时的行动,好比初初化钠舂值
- CyclicBarrier barrier = new CyclicBarrier(threadNums, finishTask);
- // 启动 n 个线程,取栅栏阀值分歧,即当线程筹办数到达请求时,栅琅好开启,从而到达同一掌握结果
- for (int i = 0; i < threadNums; i++) {
- Thread.sleep(100);
- new Thread(new CounterTask(barrier)).start();
- }
- System.out.println(Thread.currentThread().getName() + " out over...");
- }
- }
-
-
- class CounterTask implements Runnable {
-
- // 传进栅阑霈普通思索更文雅方法
- private CyclicBarrier barrier;
-
- public CounterTask(final CyclicBarrier barrier) {
- this.barrier = barrier;
- }
-
- public void run() {
- System.out.println(Thread.currentThread().getName() + " - " + System.currentTimeMillis() + " is ready...");
- try {
- // 设置栅阑霈使正在吹廊待,抵达地位当边程到达请求便可开启年夜门
- barrier.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (BrokenBarrierException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + " - " + System.currentTimeMillis() + " started...");
- }
- }
赶钙代码
其运转成果以下图: