Results 1 to 11 of 11
  1. #1
    TurtleRock is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Re-iterate Thread execution after its execution finishes.

    Hi,

    Is there any way to restart a particular thread after its execution is over.

    Eg : Suppose a thread A, after the execution of A completes, can any how we repeat its execution

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TurtleRock View Post
    Hi,

    Is there any way to restart a particular thread after its execution is over.

    Eg : Suppose a thread A, after the execution of A completes, can any how we repeat its execution
    You can't; as the API documentation states: a Thread that has finished the run() method is dead and can't be restarted again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TurtleRock is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks a lot. :)

  4. #4
    shivapvr is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Try This

    Hi,
    Actually u can. Try this out

    public class Threads{
    public static void main(String[] args){
    Thread th = new Thread();
    System.out.println("Numbers are printing line by line after 5 seconds : ");
    try{
    for(int i = 1;i <= 10;i++)
    {
    System.out.println(i);
    th.sleep(5000);
    }
    }
    catch(InterruptedException e){
    System.out.println("Thread interrupted!");
    e.printStackTrace();
    }
    }
    }

    Regards
    Shiva

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by shivapvr View Post
    Actually u can. Try this out
    Better not because that example doesn't even start the Thread (and the example is faulty anyway).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    shivapvr is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Well. It works for me.:rolleyes:
    And it shows, u need to spend some time in Java Basics,;)

    Regards
    Shiva

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by shivapvr View Post
    Well. It works for me.:rolleyes:
    And it shows, u need to spend some time in Java Basics,;)
    If you have done so you most certainly have notices that the sleep( ... ) method is static. Oh, and b.t.w. your Thread wasn't even started.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by TurtleRock View Post
    Hi,

    Is there any way to restart a particular thread after its execution is over.

    Eg : Suppose a thread A, after the execution of A completes, can any how we repeat its execution
    Ok sorry for bumping in....:P

    Lets make things much simpler I totally agree with JosAH on "a Thread that has finished the run() method is dead ".

    Now the feasibility depends upon how you are using thread A. If you had intialised thread A(class extending thread) like
    A a = new A(); or even
    Thread A = new Thread(){ public void run() {}};
    you can execute it again and again as far as your instance 'a'/A holds valid(i.e your main class is still running ) coz even though the thread is not alive but due to the fact that 'a'/A has the reference you can keep calling a.run();/A.run(); as many times as u want.

    Well just make sure that your thread is not alive before calling it again ....

    Kindly rectify me if my understanding is wrong .... :)




    warm regards
    Vinod M
    Last edited by Vinod Mukundan; 11-29-2010 at 01:10 PM.
    _______________________________________________
    give me beans .........

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    To put an end to this discussion; from the Thread.start() API documentation:

    It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Ooops sorry for the post............ I got it now JosAH .... I missed the point regarding the difference in implementation of thread.start() and thread.run()... did some reading and now have much better idea.. thnx for your reply and of-course your patience ..:)
    _______________________________________________
    give me beans .........

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    Ooops sorry for the post............ I got it now JosAH .... I missed the point regarding the difference in implementation of thread.start() and thread.run()... did some reading and now have much better idea.. thnx for your reply and of-course your patience ..:)
    No need to apologize; I was more replying to shivapr (reply #4 and reply #6). The examples were dead wrong.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. thread execution one after another
    By turanan in forum New To Java
    Replies: 16
    Last Post: 05-08-2012, 12:11 PM
  2. Help in execution of a Timer
    By Alice.B in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-27-2010, 06:36 AM
  3. .BAT execution error
    By hunterbdb in forum Advanced Java
    Replies: 5
    Last Post: 02-23-2009, 05:41 AM
  4. JAR execution
    By patoh in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 09:09 AM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 03:52 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
  •