Simple question, is this code valid, and will not leave a resource leak of any kind:
// code...
final int delaySecs = 60;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(delaySecs * 1000);
// code to do whatever delayed single-shot action
} catch (InterruptedException ex) { /* skip action */ }
}
}).start();
// more code...
If it is not valid, should I use a Thread subclass like this to enable setDaemon(true) call:
class DaemonThread extends Thread {
public DaemonThread(Runnable target) {
super(target);
setDaemon(true);
}
}
Or something else?
Java has offered specific support for your use case since 1.5, so you are best advised to use it:
This will both achieve the proper delay and take care of cleanup afterwards.