08-17 18:26:44.540: E/AndroidRuntime(2405): FATAL EXCEPTION: Scheduler
08-17 18:26:44.540: E/AndroidRuntime(2405): java.lang.IllegalThreadStateException: Thread already started.
08-17 18:26:44.540: E/AndroidRuntime(2405): at java.lang.Thread.start(Thread.java:1045)
08-17 18:26:44.540: E/AndroidRuntime(2405): at com.example.simpledownloader.scheduler.Scheduler.run(Scheduler.java:21)
08-17 18:26:44.540: E/AndroidRuntime(2405): at java.util.Timer$TimerImpl.run(Timer.java:284)
所以,我有一个TimerTask
通过蛮力检查所有待处理的下载。
下载全部Thread
存储在Vector
. 一旦TimerTask
看到线程准备就绪,它就会调用start()
它。这是代码:
代码:
package com.example.simpledownloader.scheduler;
import java.util.TimerTask;
import android.util.Log;
import com.example.simpledownloader.sharable.Sharable;
import com.example.simpledownloader.task.Task;
public class Scheduler extends TimerTask {
//------------------------------------------------------------------------------
@Override
public void run() {
if(Sharable.getShouldLook()){ // Should we look for pending tasks ?
for(Task each: Sharable.downloads){ // Yes, then loop
if(each.isAlive()==false){ // Thread is not running ?
Log.v("SCHEDULER", "NOT RUNNING");
if(each.getReadiness() == true){ // Is it ready ?
each.start(); // Start !
break; // stop looping
}
}
}
}
}
//------------------------------------------------------------------------------
}
为什么我不断收到该错误?设置如下
:Timer
Sharable.schedulerTimer.scheduleAtFixedRate(Sharable.schedulerTask, 1000, 1000); // start scheduler