每30 秒,记录就会存储到SQLite DB中。
我想在30 秒内执行一个线程,将记录数发布到我们的服务器。
如果线程超过30 秒,则自行终止线程。
每30 秒,记录就会存储到SQLite DB中。
我想在30 秒内执行一个线程,将记录数发布到我们的服务器。
如果线程超过30 秒,则自行终止线程。
使用定时器如下:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() { //timer
System.out.println("done");
this.cancel();
}
}, 30000L);
看看Handlers,特别是postDelayed(Runnable, long)
方法。
这是一种指定将来运行的代码的轻量级方法,在您的情况下,您可以将其设置为30 seconds
并检查有问题的代码是否仍在运行。
(例如,通过在代码运行时将布尔值设置为 true)并通过您可用的任何方式终止它。
thread.sleep(30000);
使线程休眠 30 秒。thread.interrupt();
注意:不要使用thread.stop();
,因为它是不推荐使用的方法。