java多线程处理

int threadNum = 10;
final int requestNum = 100;
Thread[] threads = new Thread[threadNum];

long startTime = System.currentTimeMillis();
//benchmark for sync call
for(int i = 0; i < threadNum; ++i){
System.out.println("i = " + i+Thread.currentThread());
threads[i] = new Thread(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < requestNum; i++) {
System.out.println("error = " + i+Thread.currentThread());
}
}
});
threads[i].start();
}
for(int i=0; i<threads.length;i++){
threads[i].join(); //不进行join,主进程直接执行完毕
}
long timeCost = (System.currentTimeMillis() - startTime);
String msg = String.format("Sync call total-time-cost:%sms, req/s=%s",timeCost,((double)(requestNum * threadNum)) / timeCost * 1000);
System.out.println(msg);

posted @ 2017-07-07 17:19  W&L  阅读(95)  评论(0编辑  收藏  举报