user1595858
user1595858

Reputation: 3890

How to find when the thread completed?

Do I need to call future.get periodically to check whether the task is executed or not?

ExecutorService executor = Executors.newSingleThreadExecutor();

@SuppressWarnings("unchecked")
public void start(final file avo) throws IOException
{
    Future future = executor.submit(new Callable(){

    @Override
    public Object call() throws Exception {
        Condee conv = new Condee();
        return conv.doChange(file);
    });

    LOG.debug("Finished the execution serverion");
    executor.shutdown();


}

Upvotes: 0

Views: 118

Answers (3)

Juned Ahsan
Juned Ahsan

Reputation: 68715

Try to retrieve the value sing the get method. You will either get the result or an exception will be thrown. Here are the four possibilities when you call the get on future:

  1. You get the result value
  2. You get CancellationException - if the computation was cancelled (e.g. Future.cancel(true) is called)
  3. You get ExecutionException - if the computation threw an exception
  4. You get InterruptedException - if the current thread was interrupted while waiting (e.g. executor.shutdownNow() is called)

Upvotes: 0

joaonlima
joaonlima

Reputation: 618

To check completion you can use future.isDone(), this needs to be constantly checked.

But if you use future.get(), this method will wait until finished and then return the result.

Upvotes: 2

robert_difalco
robert_difalco

Reputation: 4914

Add a call to this before executor.shutdown();

future.get();

This method will only return when it has completed.

Upvotes: 1

Related Questions