File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stopping and Starting Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Stopping and Starting Threads" Watch "Stopping and Starting Threads" New topic
Author

Stopping and Starting Threads

Michael Scott
Ranch Hand

Joined: Jan 20, 2003
Posts: 57
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


Thanks.

[ January 23, 2007: Message edited by: Michael Scott ]
[ January 23, 2007: Message edited by: Michael Scott ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

We can't tell what you are trying to do... could you post the *before* code, which works, along with the *after* code with all the changes... preferably as test code that can be compiled.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Michael Scott
Ranch Hand

Joined: Jan 20, 2003
Posts: 57
Please note that there is too much code to post here and that these snippets are from a Domino application. Thanks.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The little snippet you have

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
Michael Scott
Ranch Hand

Joined: Jan 20, 2003
Posts: 57
Hi Stan. Please note that there is code between the start and join methods as follows:

(3) thread.start();
(4) String batCommand = batPathBufferB.toString();
(5) thread.join();

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 ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
Michael Scott
Ranch Hand

Joined: Jan 20, 2003
Posts: 57
Thanks Stan. Both of your suggested solutions - the thread.join() from my previous post and the process.waitFor() are functioning exactly as intended.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Stopping and Starting Threads
 
Similar Threads
Running a .bat file with Java code
Stopping and Starting Threads
File Not Found Error
Error While Running a Shell Script
Stopping and Starting Threads