Start Thread with a given execution time

青春壹個敷衍的年華 提交于 2019-12-01 21:04:27

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.

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

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. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!