为什么我的线程在我试图关闭后仍处于活动状态?

时间:2016-08-01 10:54:15

标签: java android multithreading threadpoolexecutor

我的班级有以下功能:https://codeshare.io/kvze0

stopDownload()

我调用[Shutting down, pool size = 4, active threads = 4, queued tasks = 0, completed tasks = 0] 来停止线程。之后我记录我的ThreadPoolExecutor以查看线程的状态:

    int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();

    executor = new ThreadPoolExecutor(
            NUMBER_OF_CORES * 2,
            NUMBER_OF_CORES * 2,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>()
    );
    executor.execute(new Runnable() {
        public void run() {
            FTPDownloader ftpDownloader = new FTPDownloader(downloadService);
            ftpDownloader.startFtp(server.getServer(), server.getPort(), server.getUser(), server.getPass(), server.getPath(), server.getFiledl());
            if (Thread.interrupted()) {
                Log.d(TAG, "run: ");
                ftpDownloader.stopDownload();
            }
        }
    });

是什么让我的线程活跃?提前谢谢。

我如何开始一个帖子:

executor.shutdownNow();

我打电话给private class MainActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { executor.shutdownNow(); } } 关闭我的线程:

.htaccess

1 个答案:

答案 0 :(得分:1)

请尝试以下步骤:

摆脱这些界限:

if (Thread.interrupted()) {
            Log.d(TAG, "run: ");
            ftpDownloader.stopDownload();
        }

downloadAndBroadcast } catch (Exception e) {

之前添加此内容
} catch (InterruptedException ie) {
    // catching IE will reset interrupted status. So just clear the flag.
    download = false;
}