簡體   English   中英

Java中的線程池

[英]Thread pool in Java

我使用ThreadPool在Java中編寫應用程序。 首先我創建新的ThreadPool:

private ExecutorService threadExecutor = Executors.newFixedThreadPool( 20 );

然后我創建了一些Runnable對象。 之后,我不時地執行我的ThreadPool傳遞相同的Runnable對象:

threadExecutor.execute(serverRunnable);

我每20秒執行一次這個ThreadPool。 我的問題是threadExecutor停止工作大約5分鍾。 它只是不執行Runnable對象。 我注意到當我增加參數:

Executors.newFixedThreadPool( 20 );

從20到100個ThreadPool將工作更長時間。 任何人都可以解釋為什么ThreadPool停止工作?

PS。 我在Android中編寫此代碼

首先,如果您希望每隔20秒安排執行任務,請嘗試使用ScheduledThreadPoolExecutor: http//docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

看來你的runnable沒有終止 - 這樣它會在一段固定的時間后超過20個線程。 如果您的runnable正常終止 - 您將能夠無限期地使用您的執行程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM