user1296107
user1296107

Reputation:

How to add a timer for a specific method in java?

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?

Upvotes: 0

Views: 105

Answers (3)

swamy
swamy

Reputation: 1210

Sheduler

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

@Override

public void execute() {

// code here

}

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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);

Upvotes: 1

Darshan Mehta
Darshan Mehta

Reputation: 30809

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.

Upvotes: 1

Related Questions