Java实现多线程并发

 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 import java.util.concurrent.Semaphore;
 4 
 5 public class ThreadTest {
 6     private static int thread_num = 500;
 7     private static int client_num = 5000;
 8 
 9     public static void main(String[] args,String no) {
10         ExecutorService exec = Executors.newCachedThreadPool();
11 
12         final Semaphore semp = new Semaphore(thread_num);
13 
14         for (int index = 0; index < client_num; index++) {
15 
16             final int NO = index;
17 
18             Runnable run = new Runnable() {
19                 public void run() {
20                     try {
21                         semp.acquire();
22                         //HttpClientTest.postLogin();
23                         System.out.println("Thread:" + NO);
24                         semp.release();
25                     } catch (Exception e) {
26                         e.printStackTrace();
27                     }
28                 }
29             };
30             exec.execute(run);
31         }
32         exec.shutdown();
33     }
34 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券