Results 1 to 19 of 19
  1. #1
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default Running thread a second time

    If I click a button, a program is launched and a thread is started that checks if the program is active, so I can perform some tasks when the program is finished.
    This seems to work okay.
    However, if I click the button a second time (after the external program was closed), the thread will no longer run.
    Here is my code:

    Java Code:
        private void btnActionPerformed(java.awt.event.ActionEvent evt) {
            try {
                Runtime.getRuntime().exec( extPath + "ExtProg.exe", null, new File(extPath) );
            } catch (IOException ex) {}        
            WaitExtProgramStop();
        }
    Java Code:
        public void WaitExtProgramStop() {
             Runnable ExtProgramStop = new Runnable () {
                public void run(){
                    while(!ExtProgramStopped){
                        try {
                            System.out.println("sleeping...");
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {}
                        
                        boolean result = VBSUtils.isRunning("ExtProg.exe"); //this checkes if the program is still running
                        if (!result){
                            ExtProgramStopped = true;
                            //Do Stuff
                        }
                    }
                }
             };
          Thread threadProgStop = new Thread(ExtProgramStop, "threadProgStop");
          threadProgStop.start();    
        }
    Why won't the thread be run a second time?

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

    Default

    Quote Originally Posted by Fleur View Post
    Why won't the thread be run a second time?
    You can only start a thread once; if it has stopped you can't start it again. Read the API documentation for the Thread class. You have to start another (new) Thread instead; you may check if the first Thread has stopped already.

    kind regards,

    Jos

  3. #3
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    Thanks for your answer Jos,

    How does this work if I don't know how many times people will click the button that will start a new thread? I'm not supposed to use a counter to keep track on how many threads have been started so far, or am I?

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

    Default

    Quote Originally Posted by Fleur View Post
    Thanks for your answer Jos,

    How does this work if I don't know how many times people will click the button that will start a new thread? I'm not supposed to use a counter to keep track on how many threads have been started so far, or am I?
    I don't know if you're up to it yet but read the API documentation for the AtomicBoolean class. Think of it as a flag you can set if it hasn't been set yet in a synchronized (atomic) way. When you want to create a thread, attempt to set this flag first. If it fails some other thread is still running, otherwise it wasn't set yet so you have now set it.

    Create a new thread that does what it has to do while your original thread simply returns. When that other thread finishes it resets that flag again so new threads can be started again.

    kind regards,

    Jos

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    By creating the thread then and there.

    Edit: Also, if you want the thread to only be started once, disable the button in the action listener.

  6. #6
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    Thanks Jos,

    Looks like I'm going to be busy this week-end :)

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

    Default

    Quote Originally Posted by Fleur View Post
    Thanks Jos,

    Looks like I'm going to be busy this week-end :)
    Mwah, it's lousy weather, so ... also take Masijades tip in consideration: disable the button when you have started your thread and enable it again when the thread finishes (that is the responsibility of the other thread). When disabled the user simply can't start another thread.

    kind regards,

    Jos

  8. #8
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    Okay, confused again: if the thread started and then finished, I can't start it again, you said?

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

    Default

    Quote Originally Posted by Fleur View Post
    Okay, confused again: if the thread started and then finished, I can't start it again, you said?
    Yep, that's what I said and I didn't make that up; read the API documentation for the Thread class.

    kind regards,

    Jos

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    :sigh: IOW you create and start a new one.

  11. #11
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    okay masijade, but how can I do this? Is there a way to generate a new thread with a random name that has not been used before?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fleur View Post
    okay masijade, but how can I do this? Is there a way to generate a new thread with a random name that has not been used before?
    why do you want to do this? What does a thread "name" have to do with this? I'm not even sure what a thread name is. We're discussing specific solutions here when we don't even know what the overall problem is. Perhaps your solution path is incorrect.
    Last edited by Fubarable; 11-13-2009 at 06:29 PM.

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

    Default

    A Thread doesn't care about its name anyway so why not synthesize a name for it?

    Java Code:
    private static int count= 0;
    ...
    public String getName() { return "name"+count++; }
    ...
    kind regards,

    Jos

  14. #14
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    @Fubarable:
    The name of the thread in the example is "threadProgStop". (Wasn't that obvious?)
    If I ran it once I cannot run it again, so I would need to create a new thread (with a different name), no?

    The overall problem is very simple:
    1. I run an externam program
    2. this program generates a file
    3. The external program is closed
    4. my application copies that file to some other location.

    Steps 1 to 4 can be accomplished multiple times one after another without my application closing.

    I don't think I can accomplish this without threads: I tried, but the external application would not be able to close, it remained visible in the list of running processes (anyone know why?).

    Clearly I am open to better ways to solve this issue.

    @Jos: Thanks, this looks like it would do it, I should have thought of it myself :)
    Last edited by Fleur; 11-13-2009 at 08:53 PM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fleur View Post
    @Fubarable:
    The name of the thread in the example is "threadProgStop". (Wasn't that obvious?)
    If I ran it once I cannot run it again, so I would need to create a new thread (with a different name), no?
    No. As Jos states, the name doesn't matter.

    The overall problem is very simple:
    1. I run an externam program
    2. this program generates a file
    3. The external program is closed
    4. my application copies that file to some other location.

    Steps 1 to 4 can be accomplished multiple times one after another without my application closing.

    I don't think I can accomplish this without threads: I tried, but the external application would not be able to close, it remained visible in the list of running processes (anyone know why?).
    I have had no problem with your using threads and agree that it's necessary. Shoot we all agree on this, but worrying about names and such is a bit unusual. And I'm not sure what current problems you may be having and why. I think the details and perhaps your code will be important.

  16. #16
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Fleur, I think that your problem is that you cannot run:
    Java Code:
    Thread threadProgStop = new Thread(ExtProgramStop, "threadProgStop");
    threadProgStop.start();
    More that once because you will get an "Variable already initialized" error. I think that you can just declare: Thread threadProgStop; and the begining, and then call:
    Java Code:
    threadProgStop = new Thread(ExtProgramStop, "threadProgStop");
    threadProgStop.start();
    So that your program will run the Thread more than once. BTW I have no idea if this will work or not, just a thought.

  17. #17
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    what if he makes an object of that thread and puts it in a separate class then he can have new instances of the object

  18. #18
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    That is exactly what is happening when he calls new Thread()
    it makes an object of class Thread. Why would he need to go through ANOTHER class to do this?

  19. #19
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    Okay, I could not solve the issue with the threads. In stead I used jpoller (from sourceforge): this seems to do exactly what I need: highly recommandable

Similar Threads

  1. 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
  2. Improve the slow running time
    By coolFrenzi in forum Advanced Java
    Replies: 1
    Last Post: 04-12-2009, 12:15 PM
  3. Calculating the Running time
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:32 PM
  4. Replies: 1
    Last Post: 01-12-2009, 08:59 AM
  5. Running a thread on another appilcation
    By Charlie in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 12:28 AM

Posting Permissions

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