1. Download our Official Android App: Forums for Android!

Apps Threading programme

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

  1. purnachandrarao

    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Jul 20, 2010

    Jul 20, 2010
    8
    0
    25
    programmer
    india
    hi
    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
     

    Advertisement

  2. Arvesh

    Arvesh Lurker
    Rank:
    None
    Points:
    25
    Posts:
    6
    Joined:
    Jan 11, 2012

    Jan 11, 2012
    6
    0
    25
    Mauritius
    no answers ! thats sad
     
  3. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    Arvesh, why don't you volunteer one then?
     
  4. jaeela

    jaeela Lurker
    Rank:
    None
    Points:
    6
    Posts:
    9
    Joined:
    Jan 15, 2012

    Jan 15, 2012
    9
    1
    6
    Male
    Indiana
    Thread t = new Thread(new Runnable() {
    public void run() {
    Thread.sleep(1000);
    //Do operations here
    }
    }
    t.start();

    Is this what you want?
     
  5. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia

    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
    Rank:
    None
    Points:
    36
    Posts:
    26
    Joined:
    Feb 23, 2011

    Check out Timer and Timertask also.
     
  7. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    As Lemoncog said , use Timer and Timertask
    here's an example

    Code (Text):
    1.  
    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. }
    13.  
    I used this for a cool down in my game tutorial, check it out
    https://jimmaru.wordpress.com/2011/12/13/simple-andengine-game-v2-0-more-awesomeness/
     

Share This Page

Loading...