我正在并行运行两个线程,只有一个正在执行:
countDown = new CountDownLatch(2); //tasks number
tasks = new ArrayList<Callable<Object>>();
//tasks.add(Executors.callable(new runCode(countDown, timelineTime2)));
tasks.add(Executors.callable(new runCode(countDown, timelineTime_sublist1,1)));
tasks.add(Executors.callable(new runCode(countDown, timelineTime_sublist2,2)));
executor.invokeAll(tasks);
在输出中,它以:
Thread 1 running,
Thread 2 running,
Thread 1 running,
Thread 2 running,
但在那之后只有线程 1 运行。
runCode 的代码是:
public static class runCode implements Runnable {
private final CountDownLatch countDown;
private final List<Long> timelineTimeI;
private final int v;
runCode (CountDownLatch countDown, List<Long> timelineTimeI, int v) {
this.countDown = countDown;
this.timelineTimeI = timelineTimeI;
this.v = v;
System.out.println("Thread "+v+ " running");
}
public void run1 (List<Long> timelineTimeI) throws IOException, InterruptedException {
//do work
for (Long l : timelineTimeI) {
//if (v==2) thread.notifyAll();
if (nb%1000==0) {
System.out.println("Thread "+v+ " running");
System.out.println(nb + " / ~ 80000");
System.out.println("Number of active threads: "+ Thread.activeCount());
//Print CPU Usage
//OperatingSystemMXBean opBean = ManagementFactory.getOperatingSystemMXBean();
System.out.println("Total Usage: "+thread.getTotalUsage());
System.out.println("CPU Usage by Server: "+(thread.getTotalUsage()-thread.getUsageByThread(Thread.currentThread()))+"%");
System.out.println("CPU Usage by Client: "+thread.getUsageByThread(Thread.currentThread())+"%");
totalUsage += thread.getTotalUsage();
totalClientUsage += thread.getUsageByThread(Thread.currentThread());
count++;
//System.out.println("CPU Usage: "+ opBean.getSystemLoadAverage());
}
candidateSetSize = 0;
counter = t.get(l).size();
//System.out.println("Counter: "+counter+" l: "+l+" t.get(l): "+t.get(l));
for (Rating rate : t.get(l)) {
//System.out.println("Rate: "+rate+" "+rate.getRate()+" "+rate.getRid()+" "+rate.getUid());
lastOnline.put(rate.getUid(), l);
//liked by user, so send like notification
if (rate.getRate() > averageRating.get(rate.getUid())) {
peers.get(rate.getUid()).sendLikeNotification(rate.getRid());
} else {
//disliked by user, so send dislike notification
peers.get(rate.getUid()).sendDisLikeNotification(rate.getRid());
}
//in the test data set so when a user sends a notification, it implies that user is online
if (l>tborne) {
candidateSetSize += peers.get(rate.getUid()).sendOnlineNotification(true);
} else {
candidateSetSize += peers.get(rate.getUid()).sendOnlineNotification(false);
} nb++;
}
// additional online status
for (Integer uid : userList) {
// if user has been online later than slot then send online
if (lastOnline.containsKey(uid)) {
if(l - lastOnline.get(uid) > SLOT) {
if (l>tborne) {
candidateSetSize += peers.get(uid).sendOnlineNotification(true);
} else {
candidateSetSize += peers.get(uid).sendOnlineNotification(false);
}
if(counterAddOnline.containsKey(uid)) {
counterAddOnline.put(uid, counterAddOnline.get(uid)+1);
} else {
counterAddOnline.put(uid, 1);
}
lastOnline.put(uid, l);
counter++;
}
}
}
candidateSetSizeOverTime.add(candidateSetSize/counter);
}
}
public void run() {
try {
run1(timelineTimeI);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countDown.countDown();
System.out.println("Thread "+v+" finished");
}
}