• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timer Rescheduling problem

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a problem in using Timer and TimerTask.

Problem:
I have a arraylist of say 100 codes. I have to loop through the arraylist and start individual process for each code. Then after eaach individual code is done, reschedule it back in 1 hour.

I am able to do the first part, but struck in the second part.

Code snippet:



Can somebody tell me now how to schedule each code again after 1 hour?

Thanks to all in advance.

Thanks,
Tom


[HW: Added code tags]
[ September 16, 2006: Message edited by: Henry Wong ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Side question... Why are you calling the cancel() method on the timertask upon completion? If the task is *not* repetitive, it won't do anything as the current one is already running. But that leads to...

Repetitive tasks is exactly what you want. You can configure the timer to call the task every hour -- provided that you don't cancel it. See the JavaDoc for the overloaded schedule() methods.

Henry
 
Tom Keith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried another way of doing this and the following is the code:

import java.util.Date;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import java.util.ArrayList;

public class TimerTest {

String arr[]={"element1", "element2"};
public static RunTask task = null;
public static int RETRY_COUNT = 4;
ArrayList arraylist = new ArrayList();
Date today, future;

public static void main(String[] args) {
new TimerTest();
}

public TimerTest() {
//Different ways to Add Elements to the arraylist
for(int i=0;i<arr.length;i++) {
arraylist.add(i,arr[i]);
}

String strCode = null;
try {
for(int i = 0;i< arraylist.size();i++) {
strCode = (String)arraylist.get(i);
task = new RunTask(strCode, RETRY_COUNT);
task.start();
System.out.println( "\n["+ Thread.currentThread().getName()+"] Started the thread ->" + task.getName());
System.out.println("Sleeping for 2 seconds...");
Thread.sleep(2000L);
}
}
catch(Exception e) {
}
}
}

class RunTask extends Thread {
String strCode = null;
RunTask task;
int iRetryCount = 0;

public RunTask(String code, int iCount) {
strCode = code;
iRetryCount = iCount;
}

public RunTask(String code, int iCount, RunTask rt) {
strCode = code;
task = rt;
iRetryCount = iCount;
}

public void run() {
try {
iRetryCount--;

if(task != null) {
System.out.println("Sleeping for 30 seconds...");
Thread.sleep(30000);
if(! task.isAlive()) {
System.out.println(Thread.currentThread().getName() + " is dead...");
}
}
// do someting

if(iRetryCount != 0) {
task = new RunTask(strCode, iRetryCount, this);
task.start();

System.out.println( "\n["+ Thread.currentThread().getName()+"] Stopped & terminated." +
"Starting thread ->" + task.getName());
}

Thread.sleep(1000L);
System.out.println(Thread.currentThread().getName() + " sleeping for 1 second...");

}
catch (ThreadDeath e) {
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

But, I have a question. I am killing the current Thread and creating another Thread and ask it to sleep for 30 seconds. This is just a simulation. My actual program will wait for 45 minutes.

My questionis, is there any other better way of doing it?

Thanks,
Tom
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic