-1

私は2つのスレッドを並行して実行しています.1つだけが実行されています:

    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");
    }
}
4

3 に答える 3

0
 if (nb%1000==0) :

上記の行が真でない場合、リストの長さが 1000 の倍数でない場合に可能性があります。以下のステートメントは実行されません。

  System.out.println("Thread "+v+ " running"); 

このステートメントは、コンストラクターで実行され、おそらく nb の初期値である nb=0 のときに実行されるため、すべてのスレッドに対して 2 回実行されました

于 2016-09-10T11:11:17.147 に答える