[英]A common executorservice(fixed thread pool) for complete web application or a new thread pool using executorService for each and every request?
[英]How to achieve fixed thread pool without using ExecutorService
我有這樣的代碼塊
static ExecutorService executor = Executors.newFixedThreadPool(4);
Runnable worker = new CsvToJavaObject(newFilePath);
executor.execute(worker);
我想將線程數限制為n(4)並且不使用Executor Service。我該怎么辦?
ThreadPoolExecutor呢?
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 5, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
您的核心數是“ 4”嗎? 您是否試圖將並行執行限制為系統中的內核數? 如果是這樣的話:我將只使用TPL(例如“ new Task(Action)”),這已經在考慮您的系統設置了。 如果需要,您甚至可以手動設置線程池提供的線程數,請參閱ThreadPool.SetMinThreads,但是在少數情況下這才有意義。
編輯:我的錯誤,以為是ac#問題...
如果只想按需生成任務,則實際上需要修改的BlockingQueue。
當線程池具有空閑線程時,它將在隊列上執行阻塞的take()。 您修改后的隊列可以按需創建任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.