Results 1 to 6 of 6
  1. #1
    ronyb85 is offline Member
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    4
    Rep Power
    0

    Default Running a thread for a fixed period of time

    Hello,

    I am trying to find a way to run a thread for a fixed period of time and then to stop it.

    For example, if the program will receive an input telling it to run some task for 5 seconds, it will create a new thread (for example containing an endless loop which prints something), and this thread will run for 5 seconds, printing, and then it will stop.

    How can I do this?

    Thanks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    The most obvious way would be to check the elapsed time at the beginning of each loop iteration.

  3. #3
    ronyb85 is offline Member
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your reply.
    What is the method for checking the the elapsed time?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    What did google tell you?

    Hint: System (Java Platform SE 6)

  5. #5
    ronyb85 is offline Member
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    4
    Rep Power
    0

    Default

    Thanks again :)

  6. #6
    ronyb85 is offline Member
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    4
    Rep Power
    0

    Default

    Kevin thanks a lot again. I tried your method and it worked :)

    Another question:
    How can I enable the option to pause the thread, and then to resume it, so once the thread resumes to run that it will have the exact time left to run as it had before it was paused?

Similar Threads

  1. take the total time for particular period
    By baktha.thalapathy in forum New To Java
    Replies: 3
    Last Post: 06-08-2010, 09:42 AM
  2. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  3. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  4. need info on running thread during a particular time interval alone
    By karthikeyan_raju in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2009, 02:40 AM
  5. signing applets for a period of time
    By sandeepk84@gmail.com in forum Java Applets
    Replies: 0
    Last Post: 12-04-2007, 01:34 PM

Posting Permissions

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