1. 程式人生 > >用Java程式碼簡單模擬併發訪問

用Java程式碼簡單模擬併發訪問

思路主要是通過模擬多個執行緒同時發起http請求。

public class TestBingfa {
        //傳送請求的url地址
	private final String url = "http://localhost:8085/bda-search";
	//模擬的併發量
	private static final int BINGFA = 199;
	
	private static CountDownLatch cdl = new CountDownLatch(BINGFA);
	
	public static void main(String[] args) {
		for (int i = 0; i < BINGFA; i++) {
			new Thread(new UserRequest()).start();
			cdl.countDown();
		}
	}
	
	public static class UserRequest implements Runnable{
		@Override
		public void run() {
			try {
				cdl.await();
			} catch (Exception e) {
				e.printStackTrace();
			}
	        //使用工具類傳送http請求
                String json2 = HttpClientUtil.sendHttpPostJson(url, getJson());
	        System.out.println(new Date().getTime()+"::"+json2);
		}
		
	}
	
	//傳送的請求引數
	public static String getJson(){
           return null;
	}
}