views:

437

answers:

3

My main process calls an external library method. This method sometimes hangs. I can not fix the external library because another company is responsible for it.

I want to use a Thread for the library calls with a defined execution timer. When the method call takes to long, the Thread with the Runnable in which the method call is placed should stop and the main process should go forward.

  1. Main Thread wait
  2. Execute Thread
    • start Start timer Thread
    • When timer thread is finished kill Execute Thread
  3. Execute Thread stop Main
  4. thread resume

Does anybody have some code this logic is for, a design pattern i can use or a internet page i can read some informations?

Thanks

+1  A: 

This will wait up to 30 seconds for the thread to finish.

Thread t = new Thread() {
  public void run() {
    badExternalMethod();
    synchronized (this) {
      notifyAll();
    }
  }
};

synchronized (t) {
  t.start();
  try {
    t.wait(30000); // 30 secs
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

// t has completed or timed out

I think you mean t.join(30000) instead of t.wait(30000).
Andrzej Doyle
Thanks, i will test the code and see what happens to the external library and which resources are not dispose
Markus Lausberg
+1  A: 

You won't be able to magically make the running thread stop after 30 seconds; what you would need to do is make that thread check some state after the time is up, notice it's taken too long and return from its run method.

There are roughly two approaches to doing this:

  1. Have the library call thread perform its operation in "chunks" that take small amounts of time each, and check the status between each chunk. If you're processing hundreds of thousands of records, for example, you might process a few hundred in one batch then check the flag. Or if you're making potentially blocking calls with a timeout available, set the timeout to 500ms and check the flag between each call.
  2. Have the main thread interrupt the library call thread when the time is up, and ensure that the library thread handles interrupts correctly and checks the flag when this is the case. This would require the library call thread performs an interruptable method, though any potentially long-running method should allow for interrupts.

In both cases, you'd need to have one of the threads (probably the main thread as it has nothing else to do) set the "stop" flag to true once the time is up (and this variable should be declared volatile so updates are seen by all threads). Likewise, when the library call thread sees this flag as true, it should stop what it's doing, generally by returning from the run() method.

You shouldn't really kill the library thread from the main thread; see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated for the reasons, and an alternative that looks very much like what I've described above. :-)

Andrzej Doyle
I think i will terminate the main process gracefully, when the external call hangs. I dont know what the external library is doing, so i can not say what is happening when i kill the thread.
Markus Lausberg
+2  A: 

Take a look at the java.lang.concurrent package in Java 5 and later, in particular the CompletionService interface and the classes that implement it.

This interface includes calls that allow you to submit a task and either wait for it to complete, or else continue after a timeout.

Bill Michell