gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:56:59 25 4
gpt4 key购买 nike

我的类(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;
}

关于java - 为什么在我尝试关闭它们后我的线程仍然处于 Activity 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38697010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com