我怀疑我在循环中创建了几个线程,它将调用我已经实现的run方法。我怀疑虽然我已经编写了t.join
而未完成其run方法,但主线程正在向前推进并执行后续步骤。怎么阻止这个?
for(int j=1;j<=iteration;j++){
Thread t=null;
System.out.println("Starting Iteration-"+j);
for(int i=1;i<=totalNumberOfUsers;i++) {
t=new Thread(new CasLoadTest());
t.setName("User"+i);
t.start();
Thread.sleep((long) (delayPeriod*1000));
}
t.join();
for(Map.Entry<String,Long> map:latencyMap.entrySet()){
System.out.println(map.getKey()+"-"+map.getValue());
}
System.out.println("Iteration"+j+"-->Max:"+Collections.max(latencyMap.values()));
System.out.println("Iteration"+j+"-->Min:"+Collections.min(latencyMap.values()));
double temp=(double)(lastUserLoggedIn-firstUserLoggedIn)/1000;
System.out.println("Iteration"+j+"-->Total Number of Users Logged In (Users\\Sec)--"+totalNumberOfUsers/temp);
latencyMap.clear();
threadCount = totalNumberOfUsers;
count = 0;
Thread.sleep(30000);
}
答案 0 :(得分:0)
收集您在列表中创建的所有主题。然后你可以在继续使用main方法之前等待所有人退出他们的操作。
ArrayList<Thread> allThreads = new ArrayList<>(); // create a list of all Threads
for(int i=1;i<=totalNumberOfUsers;i++) {
t=new Thread(new CasLoadTest());
allThreads.add(t); // add new Thread to list
...
}
...
// wait for all Threads to end before executing next code
for(Thread thread : allThreads)
thread.join();