Results 1 to 19 of 19
Thread: Running thread a second time
- 11-11-2009, 03:47 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
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(); }Why won't the thread be run a second time?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(); }
- 11-11-2009, 03:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 11-11-2009, 06:43 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
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?
- 11-11-2009, 07:01 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-11-2009, 07:02 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 11-11-2009, 07:39 PM #6
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
Thanks Jos,
Looks like I'm going to be busy this week-end :)
- 11-11-2009, 07:58 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-12-2009, 07:08 PM #8
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
Okay, confused again: if the thread started and then finished, I can't start it again, you said?
- 11-12-2009, 08:02 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 11-13-2009, 02:38 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
:sigh: IOW you create and start a new one.
- 11-13-2009, 04:16 PM #11
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
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?
-
Last edited by Fubarable; 11-13-2009 at 06:29 PM.
- 11-13-2009, 06:42 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
A Thread doesn't care about its name anyway so why not synthesize a name for it?
kind regards,Java Code:private static int count= 0; ... public String getName() { return "name"+count++; } ...
Jos
- 11-13-2009, 08:47 PM #14
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
@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.
-
No. As Jos states, the name doesn't matter.
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.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?).
- 11-14-2009, 12:27 AM #16
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Fleur, I think that your problem is that you cannot run:
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:Thread 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.Java Code:threadProgStop = new Thread(ExtProgramStop, "threadProgStop"); threadProgStop.start();
- 11-14-2009, 03:09 PM #17
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
- 11-14-2009, 07:17 PM #18
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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?
- 11-17-2009, 12:57 PM #19
Member
- Join Date
- Apr 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
need info on running thread during a particular time interval alone
By karthikeyan_raju in forum Threads and SynchronizationReplies: 2Last Post: 10-06-2009, 02:40 AM -
Improve the slow running time
By coolFrenzi in forum Advanced JavaReplies: 1Last Post: 04-12-2009, 12:15 PM -
Calculating the Running time
By JordashTalon in forum New To JavaReplies: 2Last Post: 02-13-2009, 10:32 PM -
Getting FileNotFoundException in my java program after running for some time
By satya_vanimireddy in forum New To JavaReplies: 1Last Post: 01-12-2009, 08:59 AM -
Running a thread on another appilcation
By Charlie in forum Advanced JavaReplies: 2Last Post: 08-12-2008, 12:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks