0

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?

5
  • What do you mean by "keep the program in wait"? What should happen if the timer expires? Commented Jul 10, 2013 at 5:21
  • further program execution depends on the result of service call.. so i want to wait for that result before further execution. Commented Jul 10, 2013 at 5:27
  • Well, why a timer then? What if the call takes too long? Commented Jul 10, 2013 at 5:29
  • Agree.. any other work around for this then? Commented Jul 10, 2013 at 5:31
  • The accepted answer will work for you with a timeout. If you don't want a timeout, just call .get(). For further information, look at the javadoc of Future and FutureTask. Commented Jul 10, 2013 at 5:33

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Could be specified that .submit() actually returns a Future and that there is a .get() without a timeout too
i 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?
Well, ExecutorService and others are part of standard JRE since 1.5
1

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.

1 Comment

I guess there are some limitations on the use of classes on client side in gwt and thread class is one of those (if i am not wrong). It gives error - No source code is available for type java.net.URL; did you forget to inherit a required module?.
0

Sheduler

Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand(){

@Override

public void execute() {

// code here

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.