Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Rep Power

    Exclamation 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

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    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.
    isRunning = false;

  3. #3
    Join Date
    Sep 2010
    Rep Power

    Exclamation 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

  1. mailing concept
    By vikram.jamakhandi in forum AWT / Swing
    Replies: 3
    Last Post: 03-03-2010, 01:43 PM
  2. Thread concept
    By javahelp00 in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-01-2009, 06:15 PM
  3. What is Seeding Concept?
    By bhupal4all in forum New To Java
    Replies: 1
    Last Post: 09-03-2008, 05:47 AM
  4. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:30 PM
  5. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts