Reputation: 81
For every 30 seconds the record is store in to the SQLite DB.
I want to execute a thread within 30 seconds to post the number of records in to our server.
If the thread exceed 30 seconds then kill the thread by itself.
Upvotes: 0
Views: 3049
Reputation: 1020
Take a look at Handlers and specifically the postDelayed(Runnable, long)
method.
This is a lightweight method of specifying some code to run in the future, in your case you could set it to 30 seconds
and check if the offending code is still running.
(e.g. by setting a boolean to true when the code is running) and kill it by whatever means available to you.
Upvotes: 1
Reputation: 7646
thread.sleep(30000);
to sleep the thread for 30 Seconds. thread.interrupt();
Note: Dont use thread.stop();
as it is the deprecated method.
Upvotes: 1
Reputation: 18509
use timer as follows:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() { //timer
System.out.println("done");
this.cancel();
}
}, 30000L);
Upvotes: 3