Saturday, July 23, 2011

check long running task status using callable, future, ExecutorService in java


public boolean checkInBackground() {
ExecutorService pool = Executors.newFixedThreadPool(1);
Callable callable = new Callable() {
@Override
public Boolean call() throws Exception {
check();
return Boolean.TRUE;
}
};
final Future future = pool.submit(callable);
pool.shutdown();
Boolean bool = Boolean.FALSE;
try {
bool = future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
future.cancel(true);
bool = Boolean.FALSE;
}
return bool;
}
public boolean check() {
//long running task
return false;
}

No comments:

Post a Comment