Java 8 定时器重复一个功能
Java 8 Timer to repeat one function
如何在 Java 8 中实现计时器?为此,我更喜欢一种简单的方法。我想每 15 分钟或 30 分钟做一些事情。有什么想法吗?
你可以使用
Thread.sleep(milliseconds)
调用你想要的函数并将其放入 Runnable 中。
示例:
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000); // 3
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//your Function
}
});
}
}).start();
或
Thread t = new Thread(new Runnable() {
public void run() {
// stuff here
}});
t.start();
如何在 Java 8 中实现计时器?为此,我更喜欢一种简单的方法。我想每 15 分钟或 30 分钟做一些事情。有什么想法吗?
你可以使用
Thread.sleep(milliseconds)
调用你想要的函数并将其放入 Runnable 中。
示例:
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000); // 3
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//your Function
}
});
}
}).start();
或
Thread t = new Thread(new Runnable() {
public void run() {
// stuff here
}});
t.start();