I have code in which the 'sleep' method appears in two places. I would like to replace both sleep's with code that starts a new thread when the thread from a prior task completes.
As suggested on this forum, this was successfuly done with the first 'sleep' (line 6) by commenting it out and adding lines 3 and 4. This code is now functional..
(1) Runnable runnable = new BasicThread2(); (2) Thread thread = new Thread(runnable); (3) thread.start(); (4) String batCommand = batPathBufferB.toString(); (5) thread.join(); (6) //thread.sleep(12000); COMMENTED OUT (7) Process batChild = Runtime.getRuntime().exec(batCommand); __________________________
I then tried removing the second sleep (line 12) and adding lines 8,9, and 11. However, the code fails with an unavailable resource error. (A file that's generated by line 10 is unvailable further along in the code.) In other words, when the sleep at line 12 remains, the code runs, but when the sleep is replaced with lines 8, 9, and 11, an error results.
I'm interested in why this error occurs since the code change made below seems similar to the one made above.
(8) Thread thread2 = new Thread(runnable); (9) thread2.start(); (10) Process batChild = Runtime.getRuntime().exec(batCommand); (11) thread2.join(); (12)// thread.sleep(12000); COMMENTED OUT
[ January 23, 2007: Message edited by: Michael Scott ] [ January 23, 2007: Message edited by: Michael Scott ]
effectively defeats the use of threads. It starts one and then waits for it to finish. If you really don't do anything else in between (maybe you cut something out to shorted the post?) it would be equivalent to just say
Processes you start through runtime exec always run in new threads. If you want to wait until that one is done, look on the Process class for waitFor. You might also have to read the outputs of the process to make it run correctly.
Did either of those answer the right question?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 20, 2003
Hi Stan. Please note that there is code between the start and join methods as follows:
This was actually suggested by you in response to a previous post I made. (http://www.coderanch.com/t/381873/java/java/Stopping-Starting-Threads) I opened a new post because the scenario changed somewhat and appending to the original post would have caused more confusion. Anyway, the above solution works as it ensures that the variable created on line 4 exists before it's referenced later in the code. Without the start and join (or the sleep which existed previously), the code fails because the variable is unavailable when needed.
What's not apparent to me though is why this same solution does not also work for the following code to ensure that a file created on line 10 exists before it's referenced later in the code.
(8) Thread thread2 = new Thread(runnable); (9) thread2.start(); (10) Process batChild = Runtime.getRuntime().exec(batCommand); (11) thread2.join();
Thanks. [ January 24, 2007: Message edited by: Michael Scott ]
Joined: Jan 29, 2003
Ah, now you're getting through to me. I think the problem is because the runtime exec process runs in its own thread. You don't wait for it to finish creating the file before you go on to the next line. The process.waitFor() ought to help with that.
Joined: Jan 20, 2003
Thanks Stan. Both of your suggested solutions - the thread.join() from my previous post and the process.waitFor() are functioning exactly as intended.