码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现多线程并发

时间:2015-01-09 19:18:46      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

Java实现多线程并发

标签:

原文地址:http://www.cnblogs.com/liu-ke/p/4213993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!