• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping and Starting Threads  RSS feed

 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan. Both of your suggested solutions - the thread.join() from my previous post and the process.waitFor() are functioning exactly as intended.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!