//package com.spy;自己的报名
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author Administrator
*利用java线程池技术测试并发
*
*/
public class RuntimeTest {
public static void main(String[] args) {
int count = 10;//设置并发数量
//线程池准备
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
ExecutorService executorService = Executors.newFixedThreadPool(count);
long now = System.currentTimeMillis();//开始时间
for (int i = 0; i < count; i++)
executorService.execute(new RuntimeTest().new Task(cyclicBarrier,i));
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();//结束时间
System.out.println("总共耗时!---------"+(end-now));//总共耗时
}
public class Task implements Runnable {
private CyclicBarrier cyclicBarrier;
private int count;
public Task(CyclicBarrier cyclicBarrier,int count) {
this.cyclicBarrier = cyclicBarrier;
this.count = count;
}
@Override
public void run() {
try {
// 等待所有任务准备就绪
cyclicBarrier.await();
//测试路径
String href = "http://localhost:8080/App/Se?id="+count;
System.err.println(count);
URL url = new URL(href);
InputStream is = url.openStream();
} catch (Exception e) {
System.out.println("出现超时的线程"+count);
e.printStackTrace();
}
}
}
}