Example usage for java.util.concurrent FutureTask get

List of usage examples for java.util.concurrent FutureTask get

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask get.

Prototype

public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 

Source Link

Usage

From source file:org.apache.hadoop.hbase.regionserver.HRegion.java

private void doProcessRowWithTimeout(final RowProcessor<?, ?> processor, final long now, final HRegion region,
        final List<KeyValue> mutations, final WALEdit walEdit, final long timeout) throws IOException {
    // Short circuit the no time bound case.
    if (timeout < 0) {
        try {/* w w  w . j a  va2 s.  c  om*/
            processor.process(now, region, mutations, walEdit);
        } catch (IOException e) {
            LOG.warn("RowProcessor:" + processor.getClass().getName() + " throws Exception on row(s):"
                    + Bytes.toStringBinary(processor.getRowsToLock().iterator().next()) + "...", e);
            throw e;
        }
        return;
    }

    // Case with time bound
    FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
        @Override
        public Void call() throws IOException {
            try {
                processor.process(now, region, mutations, walEdit);
                return null;
            } catch (IOException e) {
                LOG.warn("RowProcessor:" + processor.getClass().getName() + " throws Exception on row(s):"
                        + Bytes.toStringBinary(processor.getRowsToLock().iterator().next()) + "...", e);
                throw e;
            }
        }
    });
    rowProcessorExecutor.execute(task);
    try {
        task.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException te) {
        LOG.error("RowProcessor timeout:" + timeout + " ms on row(s):"
                + Bytes.toStringBinary(processor.getRowsToLock().iterator().next()) + "...");
        throw new IOException(te);
    } catch (Exception e) {
        throw new IOException(e);
    }
}