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

Why my threads are still active after my attempt to shut them down?

我的class有这些函数:https://codeshare.io/kvze0

public void startFtp(String address, int port, String username, String password, String filepath, String file)
{
    //...parsing parameter to class variables
    try {
        while (download) {
            downloadAndBroadcast();
        }
    } catch (Exception e){
        Log.d(TAG, "startFtp disconnected or fails");
        e.printStackTrace();
    }
}

private void downloadAndBroadcast() {
    beginTime = System.currentTimeMillis();
    try {
        URL url = new URL("http://" + serverAddress + "/" + serverFile);
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();
        inputStream = new BufferedInputStream(url.openStream());
        long difference;
        byte data[] = new byte[4094];
        int count;
        while ((count = inputStream.read(data)) != -1 && download) {
            downloadCount += count;
            long stoptime = System.currentTimeMillis();
            difference = stoptime - beginTime;
            if (difference > 1000 && download) {
                currentSpeed = downloadCount / (difference / 1000L);
                averageSpeed = (averageSpeed + currentSpeed) / 2;
                broadcastSpeed();
                downloadCount = 0; 
                beginTime = stoptime;
            }
        }
        clearInputStream();
    } catch (Exception e) {
        Log.d(TAG, "FAIL");
        e.printStackTrace();
    } finally {
        clearInputStream();
        Log.d(TAG, "downloadAndBroadcast: finally");
    }
}


private void broadcastSpeed() {
    Log.d(TAG, "broadcastSpeed: ");
    toMainActivityIntent = new Intent(Constants.BROADCAST_SPEED)
            .addCategory(Intent.CATEGORY_DEFAULT)
            .putExtra("speed", averageSpeed)
            .putExtra("thread", String.valueOf(Thread.currentThread().getId()));

    downloadService.sendBroadcast(toMainActivityIntent); //send to main activity, in main activity, there is a listener that analyzes Broadcast and Intent
}
private void clearInputStream() {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void stopDownload() {
    Log.d(TAG, "stopDownload: ");
    download = false;
}

我调用 stopDownload() 来停止线程。之后,我登录 ThreadPoolExecutor 以查看线程的状态:

[Shutting down, pool size = 4, active threads = 4, queued tasks = 0, completed tasks = 0]

是什么让我的线程处于活动状态?提前致谢。

我如何开始一个话题:

    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();       
    }
}

请尝试以下步骤:

删除这些行:

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;
}