Results 1 to 3 of 3
Thread: Need help in thread concept
- 11-10-2010, 03:29 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Need help in thread concept
HI ,
My requirement is to execute a shell script from java and write the output to a file using java. If the script execution takes more than 10 mins i should kill that script execution process.
Now writing to a file and monitoring the time should happen simultaneously and once the time reaches ten min , script should be killed. I am not sure how to write this part. Please helop me on this
- 11-11-2010, 03:32 AM #2
To execute the process, we want to use that ProcessBuilder API. We use the ProcessBuilder to create and then execute a Process.
It will give us an "input stream" to the process's output , so we need to read from that. This can be done by a simple thread that does a copy from the process.getInputStream() to an output stream that is your log file- it's confusing how the direction names are reversed, the direction is from the perspective of our Java program that executes this process, its output stream is our input.
And then a second thread that does a sleep for 10 minutes, and tests the process is still running to invokes the process.destroy() method to kill it. This is amusing, because there is no process.isAlive() method. What I do for this is in the main thread that launched the process (or another thread i guess), invoke the process.waitFor() and then set a flag after the process has completed
Java Code:boolean isRunning = false; ProcessBuilder builder = new ProcessBuilder(...); Process process = builder.start(); isRunning = true; // start the thread to copy to output stream here // start the thread that will sleep for 10 minutes and look at iRunning value here. process.waitFor(); isRunning = false;
- 11-11-2010, 06:25 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Thanks a lot but still a small clarification
Hi ,
That was a nice explanation. But in the code we are setting isRunnable=false only after calling process.waitFor() method . In this case it will wait till the process is completed but if my process exceeds 10 mins i want to kill it directly . Also am new to threading let me look into it.
Similar Threads
-
mailing concept
By vikram.jamakhandi in forum AWT / SwingReplies: 3Last Post: 03-03-2010, 12:43 PM -
Thread concept
By javahelp00 in forum Threads and SynchronizationReplies: 2Last Post: 03-01-2009, 05:15 PM -
What is Seeding Concept?
By bhupal4all in forum New To JavaReplies: 1Last Post: 09-03-2008, 05:47 AM -
mail concept
By indirani in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:30 PM -
mail concept
By thamizhisai in forum Advanced JavaReplies: 4Last Post: 04-11-2008, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks