【发布时间】:2014-05-15 08:48:38
【问题描述】:
我正在尝试计算我的程序创建的所有线程的总数,但我的代码仅计算调用时间。
我确定是因为在输出中我看到在所有线程完成之前打印出差异
下面是我的主程序 RequestProcessor 这里是我的线程类
int i = 0;
List<Customer>customers = customer.getCustomers();
long startTimestamp = System.currentTimeMillis();
for (Customer c: customers){
new RequestProcessor(c, i).start();
i++;
}
long endTimestamp = System.currentTimeMillis();
System.out.println("difference = "+(endTimestamp - startTimestamp));
下面是请求处理器类
public RequestProcessor( Customer customer , int threadSeq ) {
super();
this.customer = customer;
this.threadSeq = threadSeq;
}
@Override
public void run() {
// TODO Auto-generated method stub
processRequest();
super.run();
}
public void processRequest(){
//performing execution on customer here
System.out.println("processing "+ customer.getName()+" thread seq "+threadSeq);
}
下面是我得到的输出
processing customer9 thread seq 9
processing customer7 thread seq 7
difference = 4
processing customer3 thread seq 3
processing customer2 thread seq 2
我尝试使用 join 但对我不起作用
【问题讨论】:
标签: java multithreading oop