package com.yh.proxy.pay.util;
import com.yh.proxy.basic.utils.RandomDataUtils;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import java.util.concurrent.CountDownLatch;
public class LatchTest {
public static void main(String[] args) throws InterruptedException {
OkHttpClient client = MyThread.okHttpClient();
String url = "https://api.live.bilibili.com/client/v1/Ip/getInfoNew";
Runnable taskTemp = new Runnable() {
@SneakyThrows
@Override
public void run() {
Integer randomData = RandomDataUtils.getRandomData(1, 500);
String authUser = "huangkun1zz-" + randomData;//死循环随机子账号
// String authUser = "huangkun1zz-" + i;//for循环子账号
String authPassword = "52B4EG20";
String proxyHost = "gate.hide-proxy.pro";
Integer port = 1234;
ProxyConnect1.test(authUser, authPassword, proxyHost, port, url, null, client);
Thread.sleep(3000);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
LatchTest latchTest = new LatchTest();
latchTest.startTaskAllInOnce(10000, 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;
}
}