gpt4 book ai didi

android运行多个runnable

转载 作者:行者123 更新时间:2023-11-30 01:02:01 25 4
gpt4 key购买 nike

我想一个接一个地运行两个可运行线程。

这意味着当第一个可运行线程完成时,第二个线程开始。

DownSpeedTester speedTester = new DownSpeedTester();
speedTester.setDownUrl(String.valueOf(urlDownload));
new Thread(speedTester).start();

UploadSpeedTester uploadSpeedTester = new UploadSpeedTester();
uploadSpeedTester.setUploadUrl(String.valueOf(urlUpload));
new Thread(uploadSpeedTester).start();

尝试使用 newSingleThreadExecutor 但没有成功。

ExecutorService es = Executors.newSingleThreadExecutor();
Future< Boolean> result = es.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
//Download
DownSpeedTester speedTester = new DownSpeedTester();
speedTester.setDownUrl(String.valueOf(urlDownload));
new Thread(speedTester).start();
return true;
}
});
try {
boolean res = result.get();
if (res == true){
//Upload
Log.d("mceeli","UploadSpeedTester");
UploadSpeedTester uploadSpeedTester = new UploadSpeedTester();
uploadSpeedTester.setUploadUrl(String.valueOf(urlUpload));
new Thread(uploadSpeedTester).start();
}
} catch (Exception e) {

}
es.shutdown();

它仍然一起运行。

请帮助我。

最佳答案

使用单线程执行器,不再生成任何Thread:

ExecutorService es = Executors.newSingleThreadExecutor();
DownSpeedTester speedTester = new DownSpeedTester();
speedTester.setDownUrl(String.valueOf(urlDownload));
es.submit(speedTester);

UploadSpeedTester uploadSpeedTester = new UploadSpeedTester();
uploadSpeedTester.setUploadUrl(String.valueOf(urlUpload));
es.submit(uploadSpeedTester);

es.shutdown();

关于android运行多个runnable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270714/

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