2016-12-03 57 views
0

我在Codewars的問題中遇到了一個問題,它要求計算超級市場中自助結帳過程的客戶隊列的總時間,並要求使用線程池。所以,我只是谷歌搜索它,發現一個線程池模式由m個線程組成,它們被創建用於同時執行n個任務。對於目前的問題,我猜測線程將是結賬亭的數量,任務的數量將等於客戶的數量。下面是該問題的說明:計算使用線程池所需的總時間

customers:表示隊列的正整數數組。 每個整數表示一個客戶,其值是他們需要檢出的時間量。 n:一個正整數,結帳的數量。 該函數應該返回一個整數,即所需的總時間。

假設隊列中的前端人員(即陣列/列表中的第一個元素) 一旦變爲空閒狀態就立即進入到隊服。

public static int solveSuperMarketQueue(int[] customers, int n) { 
    return 0; 
} 

我認爲,解決方案需要隨機拆分客戶到自助結帳亭和計算需要多少時間來清理了所有那些使用線程隊列。我發現下面提供了thread pool此示例代碼:

import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

class WorkerThread implements Runnable { 

    private String command; 

    public WorkerThread(String s){ 
     this.command=s; 
    } 

    @Override 
    public void run() { 
     System.out.println(Thread.currentThread().getName()+' Start. Command = '+command); 
     processCommand(); 
     System.out.println(Thread.currentThread().getName()+' End.'); 
    } 

    private void processCommand() { 
     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public String toString(){ 
     return this.command; 
    } 
} 

public class SimpleThreadPool { 

    public static void main(String[] args) { 

     // I think this will be n in the provided method 
     ExecutorService executor = Executors.newFixedThreadPool(5); 

     // loop will iterate till **customers.length** time 
     for (int i = 0; i < 10; i++) { 
      Runnable worker = new WorkerThread('' + i); 
      executor.execute(worker); 
      } 
     executor.shutdown(); 
     while (!executor.isTerminated()) { 
     } 
     System.out.println('Finished all threads'); 
    } 

} 

如何使用上面的代碼來計算的總時間?我也很感激任何其他建議。

+0

我沒有得到它......你想測量用線程池處理客戶數組的時間嗎? –

+0

是的,'index'是顧客的標識符,數組值是他們想要清除'自助結賬亭'的時間(想象他們購買的商品因顧客而異)。例如,如果數組長度爲「m」,則「客戶[m-1]」將是第m客戶清除其總共「n號」展位中的一個展位的時間 – Chak

+0

沒有建議在所有? – Chak

回答

0
import java.util.LinkedList; 
import java.util.Queue; 
import java.util.concurrent.Callable; 
import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

public class Solution { 
    private final static long SCALE = 100L; 
    static class Customer implements Callable<Void> { 

     private final long timeToWait; 
     private final CountDownLatch exitLatch; 

     Customer(int timeToWait, CountDownLatch exitLatch) { 
      this.timeToWait = timeToWait * SCALE; 
      this.exitLatch = exitLatch; 
     } 

     @Override 
     public Void call() throws Exception { 
      Thread.sleep(this.timeToWait); 
      this.exitLatch.countDown(); 
      return null; 
     } 
    } 


    public static int solveSuperMarketQueue(int[] customers, int n) throws InterruptedException { 
     ExecutorService service = Executors.newFixedThreadPool(n); 
     CountDownLatch exitLatch = new CountDownLatch(n); 
     Queue<Customer> queue = new LinkedList<>(); 
     for (int i : customers) { 
      queue.add(new Customer(i, exitLatch)); 
     } 

     long startTime = System.currentTimeMillis(); 
     service.invokeAll(queue); 
     exitLatch.await(); 
     long wholeTime = System.currentTimeMillis() - startTime; 
     service.shutdown(); 

     return (int) (wholeTime/SCALE + (wholeTime % SCALE == 0 ? 0 : 1)); 
    } 
} 
+0

請幫助我,我會接受答案。代碼沒有通過測試並顯示錯誤。我有類 '公共類解決方案{ 公共靜態INT solveSuperMarketQueue(INT []客戶,INT N){ 返回0; } }' 而且,測試用例是 '進口org.junit.runners。JUnit4; 公共類SolutionTest { \t @Test \t公共無效testNormalCondition(){ \t \t的assertEquals(9,Solution.solveSuperMarketQueue(新INT [] {2,2,3,3,4,4},2)) ; \t}}' – Chak

+0

「wholeTime」將包括創建和啓動線程所需的時間,這些線程不應該是「超市排隊時間」的一部分。使用[prestartAllCoreThreads()](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#prestartAllCoreThreads())選項來獲得更清潔的隊列時間。 – vanOekel