Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > waiting for external process to finish (using Runtime and Thread classes)

Reply
Thread Tools

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

 
 
brownjenkn@aol.com
Guest
Posts: n/a
 
      04-22-2005
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

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      04-22-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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/j...229-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.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
waiting for multiple child processes to finish Martin DeMello Ruby 1 11-13-2006 02:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 2 09-12-2005 11:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 0 09-12-2005 06:09 PM
Waiting for a site to finish loading Brett Javascript 3 05-28-2005 04:54 AM
Waiting for processes to finish under Solaris Behrang Dadsetan Python 1 07-15-2003 07:56 PM



Advertisments