LatchTest:

    1. package com.company;
    2. import java.util.concurrent.CountDownLatch;
    3. import java.util.concurrent.TimeUnit;
    4. /**
    5. * @Author: GP3
    6. * @Description: 并发请求测试
    7. * @Date: Created in 10:16 2020/4/17
    8. * @Modified By:
    9. */
    10. public class LatchTest {
    11. private static final int NUM = 2000; //并发数
    12. public static void main(String[] args) throws InterruptedException {
    13. Runnable taskTemp = new Runnable() {
    14. // 注意,此处是非线程安全的,留坑
    15. private int iCounter;
    16. @Override
    17. public void run() {
    18. // for(int i = 0; i < 10; i++) {
    19. // 发起请求
    20. HttpUtils.sendPost("http://4uu1193612.51vip.biz:23759",
    21. "userName=gaopeng&pageNum=1&pageSize=15");
    22. // TT.Test();
    23. iCounter++;
    24. System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
    25. try {
    26. Thread.sleep(5);
    27. } catch (InterruptedException e) {
    28. e.printStackTrace();
    29. }
    30. // }
    31. }
    32. };
    33. LatchTest latchTest = new LatchTest();
    34. latchTest.startTaskAllInOnce(NUM, taskTemp);
    35. }
    36. public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
    37. final CountDownLatch startGate = new CountDownLatch(1);
    38. final CountDownLatch endGate = new CountDownLatch(threadNums);
    39. for (int i = 0; i < threadNums; i++) {
    40. Thread t = new Thread() {
    41. public void run() {
    42. try {
    43. // 使线程在此等待,当开始门打开时,一起涌入门中
    44. startGate.await();
    45. try {
    46. task.run();
    47. } finally {
    48. // 将结束门减1,减到0时,就可以开启结束门了
    49. endGate.countDown();
    50. }
    51. } catch (InterruptedException ie) {
    52. ie.printStackTrace();
    53. }
    54. }
    55. };
    56. t.start();
    57. }
    58. long startTime = System.nanoTime();
    59. System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
    60. // 因开启门只需一个开关,所以立马就开启开始门
    61. startGate.countDown();
    62. // 等等结束门开启
    63. endGate.await();
    64. long endTime = System.nanoTime();
    65. System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
    66. System.err.println("=====执行时间:" + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + "豪秒");
    67. return endTime - startTime;
    68. }
    69. }

    HttpUtils:

    1. package com.company;
    2. import java.io.*;
    3. import java.net.URL;
    4. import java.net.URLConnection;
    5. import java.util.Iterator;
    6. import java.util.Map;
    7. /**
    8. * @Author: GP3
    9. * @Description: http请求
    10. * @Date: Created in 10:10 2020/4/17
    11. * @Modified By:
    12. */
    13. public class HttpUtils {
    14. public HttpUtils() {
    15. }
    16. /**
    17. * 向指定URL发送POST请求
    18. *
    19. * @param url
    20. * @param paramMap
    21. * @return 响应结果
    22. */
    23. public static String sendPost(String url, Map<String, String> paramMap) {
    24. PrintWriter out = null;
    25. BufferedReader in = null;
    26. String result = "";
    27. try {
    28. URL realUrl = new URL(url);
    29. // 打开和URL之间的连接
    30. URLConnection conn = realUrl.openConnection();
    31. // 设置通用的请求属性
    32. conn.setRequestProperty("accept", "*/*");
    33. conn.setRequestProperty("connection", "Keep-Alive");
    34. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    35. // conn.setRequestProperty("Charset", "UTF-8");
    36. // 发送POST请求必须设置如下两行
    37. conn.setDoOutput(true);
    38. conn.setDoInput(true);
    39. // 获取URLConnection对象对应的输出流
    40. out = new PrintWriter(conn.getOutputStream());
    41. // 设置请求属性
    42. String param = "";
    43. if (paramMap != null && paramMap.size() > 0) {
    44. Iterator<String> ite = paramMap.keySet().iterator();
    45. while (ite.hasNext()) {
    46. String key = ite.next();// key
    47. String value = paramMap.get(key);
    48. param += key + "=" + value + "&";
    49. }
    50. param = param.substring(0, param.length() - 1);
    51. }
    52. // 发送请求参数
    53. out.print(param);
    54. // flush输出流的缓冲
    55. out.flush();
    56. // 定义BufferedReader输入流来读取URL的响应
    57. in = new BufferedReader(
    58. new InputStreamReader(conn.getInputStream()));
    59. String line;
    60. while ((line = in.readLine()) != null) {
    61. result += line;
    62. }
    63. } catch (Exception e) {
    64. System.err.println("发送 POST 请求出现异常!" + e);
    65. e.printStackTrace();
    66. }
    67. // 使用finally块来关闭输出流、输入流
    68. finally {
    69. try {
    70. if (out != null) {
    71. out.close();
    72. }
    73. if (in != null) {
    74. in.close();
    75. }
    76. } catch (IOException ex) {
    77. ex.printStackTrace();
    78. }
    79. }
    80. return result;
    81. }
    82. /**
    83. * 向指定 URL 发送POST方法的请求
    84. *
    85. * @param url 发送请求的 URL
    86. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
    87. * @return 所代表远程资源的响应结果
    88. */
    89. public static String sendPost(String url, String param) {
    90. PrintWriter out = null;
    91. BufferedReader in = null;
    92. String result = "";
    93. try {
    94. URL realUrl = new URL(url);
    95. // 打开和URL之间的连接
    96. URLConnection conn = realUrl.openConnection();
    97. // 设置通用的请求属性
    98. // conn.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
    99. // conn.setRequestProperty("Content-Type", "application/json");
    100. conn.setRequestProperty("accept", "*/*");
    101. // conn.setRequestProperty("connection", "Keep-Alive");
    102. // conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0;
    103. // Windows NT 5.1;SV1)");
    104. // 发送POST请求必须设置如下两行
    105. conn.setDoOutput(true);
    106. conn.setDoInput(true);
    107. // 获取URLConnection对象对应的输出流
    108. out = new PrintWriter(conn.getOutputStream());
    109. // 发送请求参数
    110. out.print(param);
    111. // flush输出流的缓冲
    112. out.flush();
    113. // 定义BufferedReader输入流来读取URL的响应
    114. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    115. String line;
    116. while ((line = in.readLine()) != null) {
    117. result += line;
    118. }
    119. } catch (Exception e) {
    120. System.out.println("发送 POST 请求出现异常!" + e);
    121. e.printStackTrace();
    122. }
    123. // 使用finally块来关闭输出流、输入流
    124. finally {
    125. try {
    126. if (out != null) {
    127. out.close();
    128. }
    129. if (in != null) {
    130. in.close();
    131. }
    132. } catch (IOException ex) {
    133. ex.printStackTrace();
    134. }
    135. }
    136. return result;
    137. }
    138. }