首页 > 代码库 > Java实现多线程并发

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 }

 

Java实现多线程并发