This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Both work, except when the first one doesn't. Why does it fail? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Both work, except when the first one doesn Watch "Both work, except when the first one doesn New topic

Both work, except when the first one doesn't. Why does it fail?

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
The first one uses Future objects. This allows me to place a timeout on each object with the 'Future.get' function. This is nice but adds a bit of variability in the code. I don't know if it will take seconds to run the request or hours or days. It really doesn't matter because after 1000 objects are created, it begins to get flaky. About 10% of the time Apache kills the parent process (a Perl CGI script), and thus killing the child process (the forked Java process).On the other hand, I can control the timeout of the entire executor and force it to timeout before Apache's hard timeout of 10 minutes (set in the Apache config). This way I can ensure proper cleanup before Apache gives it the axe. I would love to incorporate both timeouts. However, the bit of code above dies after 2-3 minutes (~1000 callableObjects) 10% of the time and cannot be trusted.
Apache Errors for the first bit of code:
[error] [client <IP Address>] \tat java.util.concurrent.FutureTask$Sync.innerGet(, referer:
[error] [client <IP Address>] \tat java.util.concurrent.FutureTask.get(, referer:
[error] [client <IP Address>] \tat Thread_Handler.setupThreads(, referer:
[error] [client <IP Address>] \tat <Java Main>.main(<Java Main>.java:72), referer:

Question 1: What is wrong with the first bit of code to cause it to be unstable? Is this a bug in Java or my ignorance?
Question 2: Why is the second bit of code stable? I really see no difference as long as the total time to run is under 10 minutes.
Question 3: Is there a better way to do this whole idea?

Thank you for any help you can provide.
Consider Paul's rocket mass heater.
subject: Both work, except when the first one doesn't. Why does it fail?
Similar Threads
How to exit from Java method without waiting for control to retrun back
Number of working threads using java executor framework
ExecutorService is forcing me to cast to (Callable<T>)
Java program for fibanocci using Java threads
Question involving InterruptedException and sleeping in a thread