×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    differents between timer, timertask and thread??

    hi people

    i have a doubt, i need to do a process to get a gps position every five minutes, but i´m no t sure how do this, if one timer and timer task, or with a thread, why is the best way?

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: differents between timer, timertask and thread??

    Timer and TimerTask can be troublesome. I'd suggest using a Thread.

    Use sleep() in small amounts (say, 500 milliseconds). Check System.currentTimeMillis() to see if it's time to do something. You'll need to do this in a loop, and be checking some "volatile boolean" member variable to see if the loop should end. On some devices, you could stop the application from terminating properly if you don't make sure that the thread completes. The thread only ends when the run() method returns. If you sleep() for five minutes, you could block termination of the application for five minutes!

    (If you don't sleep, and just keep checking the time in a loop, other threads may get starved of processor time, the application will become unresponsive, and your application may drain the battery faster.)

    Graham.

  3. #3
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: differents between timer, timertask and thread??

    are you gonna use JSR179 ?
    if so use LocationListener interface for that and set the interval on 5 minutes
    So every 5 minutes (if available) you will get your location through the LocationListener interface
    no extra threads nor timers, just plain and simple

  4. #4
    Super Contributor
    Join Date
    Jun 2007
    Location
    Mumbai
    Posts
    539

    Re: differents between timer, timertask and thread??

    Quote Originally Posted by -Icaro- View Post
    hi people

    i have a doubt, i need to do a process to get a gps position every five minutes, but i´m no t sure how do this, if one timer and timer task, or with a thread, why is the best way?
    Well i have not seen details of JSR 179, but if wat Tiger79 says is correct then thats the best way. Else there is no difference in using Thread and TimerTask both use Runnable its just that if u use Thread u need to handle wait() process ur self and Timer and TimerTask do this for u. I have used both and not much difference it makes.
    au revoir
    Prakash Raman

  5. #5
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: differents between timer, timertask and thread??

    Quote Originally Posted by prakash.raman View Post
    but if wat Tiger79 says is correct then thats the best way
    obviously its correct, otherwise I wouldn't advise it :P
    seriously, I do have some knowledge about that part (JSR179), not to mention it's a pretty short document to read (maybe thats why I can actually remember some of it ), so I'd advise you to read the JSR179 documentation and specifically about the PositionListener interface, how to set that up etc...

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: differents between timer, timertask and thread??

    Go with Tiger's suggestion. It is always better to avoid using a multi-threaded solution if you can.

    Graham.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    Talking Re: differents between timer, timertask and thread??

    mm yes , i´m go with the tiger's suggestion, i think it's the better way, thanks a lot for all the replays , i dont know that do without forum nokia thx a lot!

Similar Threads

  1. TimerTask
    By mshita in forum Mobile Java General
    Replies: 2
    Last Post: 2004-06-06, 10:42
  2. 6600 & instantiating Alert dialogs from within Timer's thread context
    By random_john_doe in forum Mobile Java General
    Replies: 0
    Last Post: 2004-01-15, 17:20
  3. Thread freezes any suggestion
    By shahzad73 in forum Mobile Java General
    Replies: 0
    Last Post: 2003-02-10, 17:23
  4. How to recover from a thread in J2ME
    By shahzad73 in forum Mobile Java General
    Replies: 0
    Last Post: 2003-02-08, 09:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×