waiting for external process to finish (using Runtime and Thread classes)

Discussion in 'Java' started by brownjenkn@aol.com, Apr 22, 2005.

  1. Guest

    Hi all,

    I was hoping to get some advice/strategy on how best to do this. I
    want to write a form to update a user's password on DB2 via a servlet.
    The code below runs okay, and successfully updates a user password, but
    I'd also like to verify it updated okay with a little dummy query at
    the end. Having virtually no experience with threading, or the Runtime
    class, etc. I think I need some help...

    The idea behind the code below is to have the current thread sleep 5sec
    which will give the external db2 process time to run and update the
    password.


    HERE'S MY CODE!

    //----------------------------------------------------------
    Runtime rt = Runtime.getRuntime();
    rt.exec("db2cmd /c db2 connect to {someDatabase} user {someUser} using
    {somePassword} new {newPassword} confirm {newPassword}")

    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    /*

    - code to connect to {someDatabase} and run a query to verify if update
    was successful using whatever {newPassword} is

    - if query runs okay, redirect to a "success" page, else display an
    error message

    */

    //----------------------------------------------------------

    Question 1- is putting the current thread to sleep a good way to
    handle this?

    Question 2- should I use Thread.currentThread().sleep(5000) instead?
    (ie. do it "non-statically")

    Question 3- should I consider:

    Process p = Runtime.getRuntime().exec("db2cmd /c db2 connect to
    {someDatabase} user {someUser} using {somePassword} new {newPassword}
    confirm {newPassword}");

    p.waitFor();

    instead of dealing with the Thread class? According to the API
    waitFor() "causes the current thread to wait, if necessary, until the
    process represented by this Process object has terminated."

    Question 4- is there a totally better way to do this?

    Any advice is much appreciated.

    Thanks, Marc
    , Apr 22, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hi all,
    >
    > I was hoping to get some advice/strategy on how best to do this. I
    > want to write a form to update a user's password on DB2 via a servlet.
    > The code below runs okay, and successfully updates a user password, but
    > I'd also like to verify it updated okay with a little dummy query at
    > the end. Having virtually no experience with threading, or the Runtime
    > class, etc. I think I need some help...
    >
    > The idea behind the code below is to have the current thread sleep 5sec
    > which will give the external db2 process time to run and update the
    > password.
    >
    >
    > HERE'S MY CODE!
    >
    > //----------------------------------------------------------
    > Runtime rt = Runtime.getRuntime();
    > rt.exec("db2cmd /c db2 connect to {someDatabase} user {someUser} using
    > {somePassword} new {newPassword} confirm {newPassword}")
    >
    > try {
    > Thread.sleep(5000);
    > } catch (InterruptedException e) {
    > e.printStackTrace();
    > }
    >
    > /*
    >
    > - code to connect to {someDatabase} and run a query to verify if update
    > was successful using whatever {newPassword} is
    >
    > - if query runs okay, redirect to a "success" page, else display an
    > error message
    >
    > */
    >
    > //----------------------------------------------------------
    >
    > Question 1- is putting the current thread to sleep a good way to
    > handle this?


    I think you're better off waiting for the external process to finish,
    although you may want to consider a watchdog timer in case it takes too
    long.

    > Question 2- should I use Thread.currentThread().sleep(5000) instead?
    > (ie. do it "non-statically")


    sleep is a static method of Thread. Invoking via currentThread() often
    confuses the issue, especially when people start calling sleep on other
    threads, e.g. otherThread.sleep(2000) is a valid statement, but actually
    makes the current thread sleep.

    >
    > Question 3- should I consider:
    >
    > Process p = Runtime.getRuntime().exec("db2cmd /c db2 connect to
    > {someDatabase} user {someUser} using {somePassword} new {newPassword}
    > confirm {newPassword}");
    >
    > p.waitFor();
    >
    > instead of dealing with the Thread class? According to the API
    > waitFor() "causes the current thread to wait, if necessary, until the
    > process represented by this Process object has terminated."


    Yes, this is better.

    >
    > Question 4- is there a totally better way to do this?


    Yes. You need to ensure that your exec target either does not produce any
    output at all or that you properly consume that output in two separate
    threads (one for the exec's standard output and one for standard error.)
    See this: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    The reason is that if the exec produces output and you don't consume it, the
    exec will block waiting for you to read it. Meanwhile you are waiting for
    the process to finish, so voila deadlock.

    >
    > Any advice is much appreciated.


    Why don't you connect to the database directly via JDBC?

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Apr 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Behrang Dadsetan

    Waiting for processes to finish under Solaris

    Behrang Dadsetan, Jul 15, 2003, in forum: Python
    Replies:
    1
    Views:
    528
    Behrang Dadsetan
    Jul 15, 2003
  2. Martin DeMello
    Replies:
    1
    Views:
    187
    Farrel Lifson
    Nov 13, 2006
  3. Brett

    Waiting for a site to finish loading

    Brett, May 28, 2005, in forum: Javascript
    Replies:
    3
    Views:
    117
    Randy Webb
    May 28, 2005
  4. matej
    Replies:
    1
    Views:
    110
    Bjoern Hoehrmann
    Jun 25, 2008
  5. Roedy Green
    Replies:
    31
    Views:
    600
    Robert Klemme
    Jan 26, 2013
Loading...

Share This Page