में किसी कार्य का समय निर्धारित करना मैं काफी जावा के लिए नया हूँ और मैं 5 से 10 के बीच के क्षेत्र में किसी भी अंतराल पर, कि हर 5 से 10 सेकंड चलेंगे एक कार्य उत्पन्न करने के लिए कोशिश कर रहा हूँ तो, 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();
}
}
"कार्य" एक टाइमरटास्क है। क्या मैं मिलता है:
Exception in thread "Thread-4" java.lang.IllegalStateException: Task already scheduled or cancelled
मैं here कि एक TimerTask पुन: उपयोग नहीं किया जा सकता से समझते हैं, लेकिन मुझे यकीन है कि यह कैसे तय करने के लिए नहीं कर रहा हूँ। जिस तरह से मेरा टाइमरटास्क काफी विस्तृत है और कम से कम 1,5 सेकंड तक रहता है।
किसी भी मदद वास्तव में सराहना की जाएगी, धन्यवाद!
काम करने लगता है, धन्यवाद! – menackin