私はJavaにまったく慣れておらず、5〜10秒ごとに実行されるタスクを生成しようとしているので、10を含む5〜10の領域の任意の間隔で実行します。
私はいくつかのことを試しましたが、今のところ何も機能していません。私の最新の取り組みは以下のとおりです。
timer= new Timer();
Random generator = new Random();
int interval;
//The task will run after 10 seconds for the first time:
timer.schedule(task, 10000);
//Wait for the first execution of the task to finish:
try {
sleep(10000);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
//Afterwards, run it every 5 to 10 seconds, until a condition becomes true:
while(!some_condition)){
interval = (generator.nextInt(6)+5)*1000;
timer.schedule(task,interval);
try {
sleep(interval);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
「タスク」はTimerTaskです。私が得るものは:
Exception in thread "Thread-4" java.lang.IllegalStateException: Task already scheduled or cancelled
ここから、TimerTaskは再利用できないことは理解していますが、修正方法がわかりません。ちなみに、私のTimerTaskは非常に精巧で、少なくとも1.5秒続きます。
どんな助けでも本当にありがたいです、ありがとう!