Example usage for com.google.common.util.concurrent Futures getUnchecked

List of usage examples for com.google.common.util.concurrent Futures getUnchecked

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures getUnchecked.

Prototype

@GwtIncompatible("TODO")
public static <V> V getUnchecked(Future<V> future) 

Source Link

Document

Returns the result of calling Future#get() uninterruptibly on a task known not to throw a checked exception.

Usage

From source file:org.robotninjas.barge.rpc.netty.RpcChannelFactory.java

@Override
public boolean validateObject(Object key, ListenableFuture<NettyRpcChannel> obj) {
    return !obj.isDone() || (obj.isDone() && Futures.getUnchecked(obj).isOpen());
}

From source file:com.continuuity.weave.internal.AbstractExecutionServiceController.java

@Override
public final State startAndWait() {
    return Futures.getUnchecked(start());
}

From source file:com.google.devcoin.utils.Threading.java

/**
 * Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
 * tasks submitted before this point are now completed. Usually you won't want to use this method - it's a
 * convenience primarily used in unit testing. If you want to wait for an event to be called the right thing
 * to do is usually to create a {@link com.google.common.util.concurrent.SettableFuture} and then call set
 * on it. You can then either block on that future, compose it, add listeners to it and so on.
 *///from  w w  w  . j  a v a 2  s.c o m
public static void waitForUserCode() {
    // If this assert fires it means you have a bug in your code - you can't call this method inside your own
    // event handlers because it would never return. If you aren't calling this method explicitly, then that
    // means there's a bug in bitcoinj.
    if (vUserThread != null) {
        checkState(vUserThread.get() != null && vUserThread.get() != Thread.currentThread(),
                "waitForUserCode() run on user code thread would deadlock.");
    }
    Futures.getUnchecked(USER_THREAD.submit(Callables.returning(null)));
}

From source file:com.continuuity.loom.common.zookeeper.lib.ZKInterProcessReentrantLock.java

public void acquire() {
    if (isOwnerOfLock()) {
        return;//from  w w  w  .  j ava 2  s  . co  m
    }

    // The algo is the following:
    // 1) we add sequential ephemeral node
    // 2a) if added node is the first one in the list, we acquired the lock. Finish
    // 2b) if added node is not the first one, then add watch to the one before it to re-acquire when it is deleted.

    lockNode = Futures.getUnchecked(zkClient.create(lockPath, null, CreateMode.EPHEMERAL_SEQUENTIAL, true));
    NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(path));
    List<String> children = nodeChildren.getChildren();
    Collections.sort(children);
    if (lockNode.equals(path + "/" + children.get(0))) {
        // we are the first to acquire the lock
        return;
    }

    final SettableFuture<Object> future = SettableFuture.create();
    boolean setWatcher = false;
    // add watch to the previous node
    Collections.reverse(children);
    for (String child : children) {
        child = path + "/" + child;
        if (child.compareTo(lockNode) < 0) {
            OperationFuture<Stat> exists = zkClient.exists(child, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    if (event.getType() == Event.EventType.NodeDeleted) {
                        future.set(new Object());
                    }
                }
            });
            // if it was deleted before we managed to add watcher, we need to add watcher to the current previous, hence
            // continue looping
            if (Futures.getUnchecked(exists) != null) {
                setWatcher = true;
                break;
            }
        }
    }

    if (!setWatcher) {
        // we are owners of a lock, just return
        return;
    }

    // wait for lock to be released by previous owner
    Futures.getUnchecked(future);
}

From source file:com.google.logicoin.utils.Threading.java

/**
 * Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
 * tasks submitted before this point are now completed. Usually you won't want to use this method - it's a
 * convenience primarily used in unit testing. If you want to wait for an event to be called the right thing
 * to do is usually to create a {@link com.google.common.util.concurrent.SettableFuture} and then call set
 * on it. You can then either block on that future, compose it, add listeners to it and so on.
 *///from  w  ww .j  a v  a  2 s .com
public static void waitForUserCode() {
    // If this assert fires it means you have a bug in your code - you can't call this method inside your own
    // event handlers because it would never return. If you aren't calling this method explicitly, then that
    // means there's a bug in logicoinj.
    if (vUserThread != null) {
        checkState(vUserThread.get() != null && vUserThread.get() != Thread.currentThread(),
                "waitForUserCode() run on user code thread would deadlock.");
    }
    Futures.getUnchecked(USER_THREAD.submit(Callables.returning(null)));
}

From source file:com.google.digitalcoin.utils.Threading.java

/**
 * Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
 * tasks submitted before this point are now completed. Usually you won't want to use this method - it's a
 * convenience primarily used in unit testing. If you want to wait for an event to be called the right thing
 * to do is usually to create a {@link com.google.common.util.concurrent.SettableFuture} and then call set
 * on it. You can then either block on that future, compose it, add listeners to it and so on.
 *//*from  w w w. jav  a 2 s.  c  o m*/
public static void waitForUserCode() {
    // If this assert fires it means you have a bug in your code - you can't call this method inside your own
    // event handlers because it would never return. If you aren't calling this method explicitly, then that
    // means there's a bug in digitalcoinj.
    if (vUserThread != null) {
        checkState(vUserThread.get() != null && vUserThread.get() != Thread.currentThread(),
                "waitForUserCode() run on user code thread would deadlock.");
    }
    Futures.getUnchecked(USER_THREAD.submit(Callables.returning(null)));
}

From source file:com.continuuity.weave.internal.AbstractExecutionServiceController.java

@Override
public final State stopAndWait() {
    return Futures.getUnchecked(stop());
}

From source file:com.google.litecoin.utils.Threading.java

/**
 * Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
 * tasks submitted before this point are now completed. Usually you won't want to use this method - it's a
 * convenience primarily used in unit testing. If you want to wait for an event to be called the right thing
 * to do is usually to create a {@link com.google.common.util.concurrent.SettableFuture} and then call set
 * on it. You can then either block on that future, compose it, add listeners to it and so on.
 *//*from  w  w w  .  j  av  a  2s.  c  o m*/
public static void waitForUserCode() {
    // If this assert fires it means you have a bug in your code - you can't call this method inside your own
    // event handlers because it would never return. If you aren't calling this method explicitly, then that
    // means there's a bug in bitcoinj.
    if (vUserThread != null) {
        checkState(vUserThread.get() != null && vUserThread.get() != Thread.currentThread(),
                "waitForUserCode() run on user code thread would deadlock.");
    }
    Futures.getUnchecked(SINGLE_THREADED_EXECUTOR.submit(Callables.returning(null)));
}

From source file:org.atlasapi.model.translators.UserModelTranslator.java

public Set<String> transformApplicationIds(Set<Id> input) {
    ListenableFuture<Resolved<Application>> resolved = store.resolveIds(input);
    return ImmutableSet.copyOf(
            Futures.getUnchecked(resolved).getResources().transform(new Function<Application, String>() {
                @Override// w  w  w .j a v  a2 s. c om
                public String apply(Application input) {
                    return input.getSlug();
                }
            }));
}

From source file:com.continuuity.loom.common.queue.internal.ZKElementsTracking.java

public ZKElementsTracking(final ZKClient zkClient, final String basePath) {
    String queuePath = basePath + "/queue";
    Futures.getUnchecked(ZKClientExt.ensureExists(zkClient, queuePath));
    this.queueElements = new SynchronizedZKMap<Entry>(zkClient, queuePath + "/map", ENTRY_SERIALIZER);

    this.globalLock = new ThreadLocal<ZKInterProcessReentrantLock>() {
        @Override/*from ww w  . j  a  v  a  2  s  .c  om*/
        protected ZKInterProcessReentrantLock initialValue() {
            return new ZKInterProcessReentrantLock(zkClient, basePath);
        }
    };
}