I have a situation to run thread1 every minute and thread2 every hour. How can i do this.
currently I have a working code to run thread1 after every minute.
main method
static void main(string args[]){
orderListner thread1 = new orderListner();
thread1.start();
}
thread1
public static void orderListner extends thread{
public void run(){
while(true){
process();
thread.sleep(60000);
}
}
}
Now I need to start new thread for results which runs after every hour. how I can implement this simultaneously(thread1 will continously run thread2 should start every hour)
A better way to use to the thread class is to make an object that implements Runnable instead of extending thread. If you don’t want to make another file, simply make an anonymous class. (It’s a good habit to avoid using inheritance whenever possible.)
That being said, just make a new thread that runs on the hour and start it. I will use your method of threads instead.
thread 1 is the same and thread 2 is