共计 6583 个字符,预计需要花费 17 分钟才能阅读完成。
作者:等你归去来
链接:www.cnblogs.com/yougewe
有时须要测试一下某个性能的并发性能,又不要想借助于其余工具,索性就本人的开发语言,来一个并发申请就最不便了。
java 中模仿并发申请,天然是很不便的,只有多开几个线程,发动申请就好了。然而,这种申请,个别会存在启动的先后顺序了,算不得真正的同时并发!
怎么样能力做到真正的同时并发呢?
是本文想说的点,java 中提供了闭锁 CountDownLatch, 刚好就用来做这种事就最合适了。
只须要:
- 开启 n 个线程,加一个闭锁,开启所有线程;
- 待所有线程都筹备好后,按下开启按钮,就能够真正的发动并发申请了。
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.baidu.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 工具类,能够应用 成熟的工具包,也能够本人写一个简要的拜访办法,参考如下:
class HttpClientOp {public static String doGet(String httpurl) { | |
HttpURLConnection connection = null; | |
InputStream is = null; | |
BufferedReader br = null; | |
String result = null;// 返回后果字符串 | |
try { | |
// 创立近程 url 连贯对象 | |
URL url = new URL(httpurl); | |
// 通过近程 url 连贯对象关上一个连贯,强转成 httpURLConnection 类 | |
connection = (HttpURLConnection) url.openConnection(); | |
// 设置连贯形式:get | |
connection.setRequestMethod("GET"); | |
// 设置连贯主机服务器的超时工夫:15000 毫秒 | |
connection.setConnectTimeout(15000); | |
// 设置读取近程返回的数据工夫:60000 毫秒 | |
connection.setReadTimeout(60000); | |
// 发送申请 | |
connection.connect(); | |
// 通过 connection 连贯,获取输出流 | |
if (connection.getResponseCode() == 200) {is = connection.getInputStream(); | |
// 封装输出流 is,并指定字符集 | |
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |
// 存放数据 | |
StringBuffer sbf = new StringBuffer(); | |
String temp = null; | |
while ((temp = br.readLine()) != null) {sbf.append(temp); | |
sbf.append("\r\n"); | |
} | |
result = sbf.toString();} | |
} catch (MalformedURLException e) {e.printStackTrace(); | |
} catch (IOException e) {e.printStackTrace(); | |
} finally { | |
// 敞开资源 | |
if (null != br) { | |
try {br.close(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
if (null != is) { | |
try {is.close(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
connection.disconnect();// 敞开近程连贯} | |
return result; | |
} | |
public static String doPost(String httpUrl, String param) { | |
HttpURLConnection connection = null; | |
InputStream is = null; | |
OutputStream os = null; | |
BufferedReader br = null; | |
String result = null; | |
try {URL url = new URL(httpUrl); | |
// 通过近程 url 连贯对象关上连贯 | |
connection = (HttpURLConnection) url.openConnection(); | |
// 设置连贯申请形式 | |
connection.setRequestMethod("POST"); | |
// 设置连贯主机服务器超时工夫:15000 毫秒 | |
connection.setConnectTimeout(15000); | |
// 设置读取主机服务器返回数据超时工夫:60000 毫秒 | |
connection.setReadTimeout(60000); | |
// 默认值为:false,当向近程服务器传送数据 / 写数据时,须要设置为 true | |
connection.setDoOutput(true); | |
// 默认值为:true,以后向近程服务读取数据时,设置为 true,该参数可有可无 | |
connection.setDoInput(true); | |
// 设置传入参数的格局: 申请参数应该是 name1=value1&name2=value2 的模式。connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
// 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 | |
connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); | |
// 通过连贯对象获取一个输入流 | |
os = connection.getOutputStream(); | |
// 通过输入流对象将参数写出去 / 传输进来, 它是通过字节数组写出的 | |
os.write(param.getBytes()); | |
// 通过连贯对象获取一个输出流,向近程读取 | |
if (connection.getResponseCode() == 200) {is = connection.getInputStream(); | |
// 对输出流对象进行包装:charset 依据工作项目组的要求来设置 | |
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |
StringBuffer sbf = new StringBuffer(); | |
String temp = null; | |
// 循环遍历一行一行读取数据 | |
while ((temp = br.readLine()) != null) {sbf.append(temp); | |
sbf.append("\r\n"); | |
} | |
result = sbf.toString();} | |
} catch (MalformedURLException e) {e.printStackTrace(); | |
} catch (IOException e) {e.printStackTrace(); | |
} finally { | |
// 敞开资源 | |
if (null != br) { | |
try {br.close(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
if (null != os) { | |
try {os.close(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
if (null != is) { | |
try {is.close(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
// 断开与近程地址 url 的连贯 | |
connection.disconnect();} | |
return result; | |
} | |
} |
如上,就能够发动真正的并发申请了。
并发申请操作流程示意图如下:
此处设置了一道门,以保障所有线程能够同时失效。然而,此处的同时启动,也只是语言层面的货色,也并非相对的同时并发。具体的调用还要依赖于 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.baidu.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..."); | |
} | |
} |
其运行后果如下图:
各有其利用场景吧,关键在于需要。就本文示例的需要来说,集体更违心用闭锁一点,因为更可控了。然而代码却是多了,所以看你喜爱吧!
近期热文举荐:
1.1,000+ 道 Java 面试题及答案整顿 (2021 最新版)
2. 别在再满屏的 if/ else 了,试试策略模式,真香!!
3. 卧槽!Java 中的 xx ≠ null 是什么新语法?
4.Spring Boot 2.5 重磅公布,光明模式太炸了!
5.《Java 开发手册(嵩山版)》最新公布,速速下载!
感觉不错,别忘了顺手点赞 + 转发哦!