Results 1 to 10 of 10
Thread: [SOLVED] error handling
- 07-07-2008, 05:02 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 14
- Rep Power
- 0
[SOLVED] error handling
greetings,
i have a java program calling a .bat file on an XP system.
Runtime.getRuntime().exec("c:\\myBatch.bat");
the batch file does a bunch of stuff, including things that may often incur unwanted popup windows or prompts that I would like automatically bypassed.
i am wondering if i can somehow time how long it has taken for the batch file to finish and return to my java program. if it has taken more than, say, five minutes, then my java program should simply move on. my program runs overnight, and i don't want to come to my pc in the morning and have to click on an "OK" prompt, then wait for it to finish.
any tips, suggestions or questions are welcome, thanks for looking
- 07-07-2008, 05:23 PM #2
Sounds like a job for Threads. Put the batch file caller on one thread and then have another thread that waits some time say 5 minutes and then checks if the batch file thread has finished.
Fancier code could use wait() and notify() and synchronized methods to coordinate the two threads.
- 07-07-2008, 05:53 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Not to mention that you're going to need some (additional) threads to read the output and error streams of the process or it is likely to simply hang anyway.
- 07-07-2008, 05:58 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 14
- Rep Power
- 0
thanks for the tips,
Do either of you have any syntactical help on using one thread to determine if another thread is finished?
- 07-07-2008, 06:23 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
isAlive()
Thread (Java Platform SE 6)
The API docs are your friend
JDK 6 Documentation
- 07-07-2008, 08:03 PM #6
Use a boolean flag. Set true just before the thread exists.
Test if set in the other thread.
To use isAlive() you'd need a reference to the other thread via a variable which cost as much as referencing the boolean flag.
Of course, there are other ways to get references by using names and ThreadGroups.
- 07-07-2008, 08:34 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
For the one thread to check whether the other thread is alive, whether with the isAlive method or with a boolean, a reference to the other thread is still needed, so I don't know what you were getting at with that sentence.
So, why "roll your own" when something finished already exists.
- 07-07-2008, 08:40 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 14
- Rep Power
- 0
I have been using a new thread to make the batch call, and thus have to give it a name. I then use the main thread to check if it is done within 5 minutes. I pretty much have it all figured out now, but I do not know how to terminate the thread if it has failed to return. Java tells me that Thread.stop() is unsafe.
Also, how do i reference the Main thread? Can I reference it from itself?
- 07-07-2008, 10:44 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
You use call interuppt against a thread, and the thread that gets interuppted needs to catch that and clean itself up, then stop (or even continue again, depends on what you're doing and why you interuppted).
- 07-07-2008, 10:46 PM #10
Member
- Join Date
- Jun 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
session handling
By priyanka_t in forum JavaServer Pages (JSP) and JSTLReplies: 10Last Post: 08-11-2009, 12:32 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
SQL error handling example
By Java Tip in forum Java TipReplies: 0Last Post: 02-12-2008, 09:38 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks