I want to execute a specific method which contains a service call. As it includes a service call , it will take some time for execution. I want to add a timer which will keep program in wait till that method completes its executiuon. Any work around for this?
3 Answers
You can organize an asynchroneous method execution with a timeout with java.util.concurrent package
ExecutorService executorService = ...
Object res = executorService.submit(new Callable<Object>() {
public Object call() throws Exception {
... your logic
}
}).get(timeout, TimeUnit.MILLISECONDS);
3 Comments
fge
Could be specified that
.submit() actually returns a Future and that there is a .get() without a timeout tooi was trying this solution and even these classes (ExecutorService , futute etc) are not allowed on client side of GWT. There are restrictions on the use of classes on client side. It gives error - No source code is available for type java.net.URL; did you forget to inherit a required module?
Evgeniy Dorofeev
Well, ExecutorService and others are part of standard JRE since 1.5
You can use a separate thread to call that service and using join() method of Thread class, you can force main program to wait until that thread finishes the execution.
.get(). For further information, look at the javadoc ofFutureandFutureTask.