简单的问题,这段代码是否有效,并且不会留下任何类型的资源泄漏:
// 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...
如果它无效,我应该使用这样的Thread
子类来启用setDaemon(true)
调用:
class DaemonThread extends Thread {
public DaemonThread(Runnable target) {
super(target);
setDaemon(true);
}
}
或者是其他东西?