Senthil Kumar
Senthil Kumar

Reputation: 81

Thread to execute for specified amount of time.

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

Answers (3)

Cathal Comerford
Cathal Comerford

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

Avadhani Y
Avadhani Y

Reputation: 7646

  1. Use thread.sleep(30000); to sleep the thread for 30 Seconds.
  2. Implement your code for storing the data in DB in run() method.
  3. After completion of your storing the recorded data use thread.interrupt();

Note: Dont use thread.stop(); as it is the deprecated method.

Upvotes: 1

Android Killer
Android Killer

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

Related Questions