1. Are you ready for the Galaxy S20? Here is everything we know so far!

Threading programme

Discussion in 'Android Development' started by purnachandrarao, Jul 29, 2010.

  1. purnachandrarao

    Thread Starter

    i want to perform some task for every second of intervals.
    but i am not getting the result for using the Thread.sleep(1000).

    please suggest me how to write programme to full fill this task


  2. Arvesh

    Arvesh Lurker

    no answers ! thats sad
  3. jiminaus

    jiminaus Well-Known Member

    Arvesh, why don't you volunteer one then?
  4. jaeela

    jaeela Lurker

    Thread t = new Thread(new Runnable() {
    public void run() {
    //Do operations here

    Is this what you want?
  5. jiminaus

    jiminaus Well-Known Member

    In may or may not be important to your application, but be aware that this code suffers from drift. Thread.sleep(1000) will sleep for at least 1 second. It may actually be more by time the thread is actually scheduled for execution.

    And you'll need to catch the InterruptedException thrown by Thread.sleep.

    Another way that doesn't use threads is to use android.os.Handler's postAtTime or postDelayed methods.
  6. Lemoncog

    Lemoncog Newbie

    Check out Timer and Timertask also.
  7. JiMMaR

    JiMMaR Android Enthusiast

    As Lemoncog said , use Timer and Timertask
    here's an example

    Code (Text):
    2. private Timer timer;
    3. private long delay = 600;
    4. public void start(){
    5. timer.schedule(new Task(), delay);
    6. }
    7. class Task extends TimerTask {
    8.         public void run() {
    9. //your code goes here
    10. timer.schedule(new Task(), delay);
    11.         }
    12. }
    I used this for a cool down in my game tutorial, check it out

Share This Page