2013-10-03 26 views
-1

我並行運行兩個線程只有一個執行的是得到執行:兩個線程並行只有一個是越來越在java中

Thread 1 running, 
Thread 2 running, 
Thread 1 running, 
Thread 2 running, 

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); 

在輸出中,它開始

但之後只有線程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"); 
    } 
} 
+1

請提出這樣的問題時,請清除所有不相關的代碼。 –

+1

我已閱讀,但我沒有得到爲什麼後,並行開始,它只使用線程1,然後它結束時,線程1的執行結束和線程2未執行 – user2843283

回答

0
if (nb%1000==0) : 

到在後臺運行的另一個線程,如果上面的線是不正確的是如果列表的長度不是1000的倍數,則可能會發生......下面的語句不會執行

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

,因爲它是在構造函數中執行,執行時NB = 0,這可能是你NB

0

移動countDown.countDown()run()開始的初始值的所有線程執行了兩次這種說法。然後這兩個線程將在約。同一時間。第一個線程等待第二個線程countDown。

相關問題