我可以做这样的事情在Java中:方法返回Runnable对象
protected Runnable getRunnable(final int seconds) {
Runnable runnable = new Runnable() {
public void run() {
sendData();
try {
Thread.sleep(seconds * 1000);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
return runnable;
}
然后:
protected void startTimer(int seconds) throws InterruptedException,NullPointerException {
Thread thread = new Thread(getRunnable(seconds));
thread.start();
}
是上述过程中的安全?
“上述过程是否安全?” =>从哪个角度来看?代码中没有共享状态或变量,因此它是线程安全的。然而'sendData'方法可能不是... – assylias
你试过了吗? –
你想做什么? – Julien