• 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 Java code that requires the 'sleep method in two places or it otherwise fails to run to completion. However, the 'sleep' method significantly delays the run time. I'm therefore interested in replacing the 'sleep' method with code that will start and stop threads.

In the code below, without the 'sleep' on line 5, line 7 fails to run, apparently because 'batCommand' is not available. If line 7 has time to run, it generates a file that's referenced later in the code. However, this file is not found unless the code has a 'sleep' at line 8.


(1) Runnable runnable = new BasicThread2
();
(2) Thread thread = new Thread(runnable);
(3) thread.start();
(4) String batCommand = batBuffer.toString();
(5) thread.sleep(12000);
(6) // thread.join(); <=== replaces the preceding sleep()
(7) Process batChild = Runtime.getRuntime().exec(batCommand);
(8) thread.sleep(12000);
(9) // batChild.waitFor(); <=== replaces the preceding sleep();

class BasicThread2 implements Runnable {
public void run() {
}
}


In a prior post, it was suggested that a 'join' replace the 'sleep' at line 5 and that a 'waitFor' replace the 'sleep' at line 8 as shown. However, these changes don't seem to have the intended affect and results are the same as when the two 'sleeps' are not included in the code.

I'd appreciate any other ideas on how the two 'sleep' methods might be bypassed.

Thanks.
[ February 14, 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
This wording sounds very familiar ... is this a repeat of an older post?

Anyhow, what does 'batCommand' is not available mean? batCommand is a String which can hardly be unavailable. Maybe it's a string that says run a batch file and the batch file isn't there yet? If so, we need to know how the batch file is being created.

If you waitFor() the Process and the output file is not yet ready, I'm guessing the command started yet another thread that is off running on its own. If so, we can't get a handle on the thread that's really writing the file so waiting is about all we can do.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan. I just edited this post to clarify it. Yes, there was a prior post on this: http://www.coderanch.com/t/405935/java/java/Stopping-Starting-Threads

If I write the contents of 'batCommand' to a log right after line 4, it has a valid value (the name of a bat file on the hard drive.) However, line 7, which references 'batCommand' will run only if 'sleep' is added (line 5) but not if 'join' is added (line 6). More accurately, adding the 'join' has had inconsistent results.

The 'waitFor' at line 9 does not work if there are a number of concurrent runs of the application.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!