I'm trying to implement a separate Thread in my project. Every hour a scheduler starts a thread to get files from a server and store them in my database.
My code works as it should. Since the scheduler starts a new Thread every hour, I don't know if I need to stop the Thread manually or not?
try {
Runnable task = new MyClass();
Thread thread = new Thread(task);
thread.start();
} catch (Exception ex) {
System.err.println("Exception-Thread: " + ex.getMessage());
}
My Class (runnable)
public class MyClass implements Runnable {
@Override
public void run() {
try {
//Do stuff...
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
If you do not have a permanent loop or something similar inside the Runnable, then no you don't need to.
As long as the thread has not chance of doing infinite loop
Such as using while loop. Thread will stop once all the tasks in Runnable are done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With