2016-12-03 49 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