我可以在 Java 中做这样的事情吗:
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();
}
上述过程安全吗?