1

在我有3個現金櫃臺服務於客戶數量的問題上工作。每個計數器需要1秒鐘處理1個物品,而第三個計數器需要2秒來處理每個物品。 (例如,客戶A在櫃檯1上有5件物品需要5秒鐘才能完成),客戶B在櫃檯C上有3件物品需要6秒鐘。每個客戶都有不同的加入隊列的時間。 我用ScheduledExecutorService來創建相當於現金櫃臺的線程數。Can ScheduledThreadPool可以用來接受不同類型的線程嗎?

ScheduledExecutorService scheduledExecutorService =Executors.newScheduledThreadPool(3); 

現在我的可運行實現檢查項目的數量並相應地運行循環。

我根據客戶數量提交任務的數量。

scheduledExecutorService.schedule(new Runnable(),Timetojoin, TimeUnit.SECONDS); 

如何爲執行程序服務創建的3個線程分配不同的優先級。因爲我的最後一個計數器(線程)需要2秒來處理每個項目。

回答

0

您將不得不提供自定義ThreadFactoryExecutorService。您的ThreadFactory將允許您創建適合您需求的新線程。

ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)

更多信息here,在doc。

+0

感謝您提及,但通過'ThreadFactory'創建的所有線程都是類似的。是否有可能對於單個線程有不同的行爲,而其他人是否有共同的行爲? – Amol 2014-12-02 13:30:29

+0

線程工廠創建新線程。這些線程的參數取決於這種工廠的實現。工廠不必生產相同的線程。 – Antoniossss 2014-12-02 14:07:23

+0

對不起沒有完全理解,是否意味着我們有創建的ScheduledThreadPoolExecutor一個的2與實施A和另一個與實施乙 \t ScheduledExecutorService的池A = Executors.newScheduledThreadPool(1,新RetailThreadFactory(新TrainerRegister())); \t ScheduledExecutorService poolB = Executors.newScheduledThreadPool(2,new RetailThreadFactory(new NormalRegister())); class RetailThreadFactory實現ThreadFactory { \t Runnable r;零售線程工廠(Runnable r){ (Thread)newThread(Runnable r){ } \t} \t public Thread newThread(Runnable r){ \t \t Thread t = new Thread(r); \t \t return t;}} – Amol 2014-12-02 16:57:56

相關問題