我想一个接一个地运行两个runnables线程。
这意味着当第一个可运行线程完成第二个线程时,
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();
它仍在一起运行。
请帮助我。
答案 0 :(得分:1)
使用单线程执行程序,不要再生成Thread
s:
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();