Java Code Examples for java.util.concurrent.atomic.AtomicReference#wait()

The following examples show how to use java.util.concurrent.atomic.AtomicReference#wait() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlfrescoImapServer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void checkForOpeningExceptions(AtomicReference<Exception> serverOpeningExceptionRef)
{
    synchronized (serverOpeningExceptionRef) 
    {
        try 
        {
            //wait for openServerSocket() method to finish 
            serverOpeningExceptionRef.wait();
            if (serverOpeningExceptionRef.get() != null)
            {
                throw new RuntimeException(serverOpeningExceptionRef.get());
            }
        } catch (InterruptedException e) {
            if (logger.isDebugEnabled())
            {
                logger.debug(e.getMessage(), e);
            }
        }
    }
}
 
Example 2
Source File: Events.java    From HolandaCatalinaFw with Apache License 2.0 6 votes vote down vote up
public static <E extends Event> E waitForEvent(Class<E> eventClass, long timeout) throws InterruptedException {
    AtomicReference<E> result = new AtomicReference<>();
    addEventListener(new EventListener<E>(){
        @Override
        public void onEventReceived(E event) {
            result.set(event);
            synchronized (result) {
                result.notifyAll();
            }
        }
        @Override
        public Class<E> getEventType() {
            return eventClass;
        }
    });
    synchronized (result) {
        result.wait(timeout);
    }
    return result.get();
}
 
Example 3
Source File: LocalEntitiesTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testEffectorEmitsTransientSensor() throws Exception {
    HelloEntity h = app.createAndManageChild(EntitySpec.create(HelloEntity.class));
    app.start(ImmutableList.of(loc));
    
    final AtomicReference<SensorEvent<?>> evt = new AtomicReference<SensorEvent<?>>();
    app.subscriptions().subscribe(h, HelloEntity.ITS_MY_BIRTHDAY, new SensorEventListener<Object>() {
        @Override public void onEvent(SensorEvent<Object> event) {
            evt.set(event);
            synchronized (evt) {
                evt.notifyAll();
            }
        }});
    
    long startTime = System.currentTimeMillis();
    synchronized (evt) {
        h.setAge(5);
        evt.wait(5000);
    }
    assertNotNull(evt.get());
    assertEquals(HelloEntity.ITS_MY_BIRTHDAY, evt.get().getSensor());
    assertEquals(h, evt.get().getSource());
    assertNull(evt.get().getValue());
    assertTrue(System.currentTimeMillis() - startTime < 5000);  //shouldn't have blocked for all 5s
}
 
Example 4
Source File: Aria2Tester.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private <O> Object runRequest(@NonNull AbstractClient.AriaRequestWithResult<O> request, @NonNull Aria2Helper helper) {
    final AtomicReference<Object> lock = new AtomicReference<>(null);
    helper.request(request, new AbstractClient.OnResult<O>() {
        @Override
        public void onResult(@NonNull O result) {
            synchronized (lock) {
                lock.set(result);
                lock.notify();
            }
        }

        @Override
        public void onException(@NonNull Exception ex) {
            synchronized (lock) {
                lock.set(ex);
                lock.notify();
            }
        }
    });

    synchronized (lock) {
        try {
            lock.wait(5000);
        } catch (InterruptedException ignored) {
        }

        return lock.get();
    }
}
 
Example 5
Source File: TestWatchesBuilder.java    From curator with Apache License 2.0 5 votes vote down vote up
private boolean blockUntilDesiredConnectionState(AtomicReference<ConnectionState> stateRef, Timing timing, final ConnectionState desiredState)
{
    if(stateRef.get() == desiredState)
    {
        return true;
    }

    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized(stateRef)
    {
        if(stateRef.get() == desiredState)
        {
            return true;
        }
        
        try
        {
            stateRef.wait(timing.milliseconds());
            return stateRef.get() == desiredState;
        }
        catch(InterruptedException e)
        {
            Thread.currentThread().interrupt();
            return false;
        }
    }
}
 
Example 6
Source File: E04_Unsubscribe.java    From api with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // Create an await for the API
    //Promise<ApiPromise> ready = ApiPromise.create();
    initEndPoint(args);

    WsProvider wsProvider = new WsProvider(endPoint);

    Promise<ApiPromise> ready = ApiPromise.create(wsProvider);

    AtomicReference<IRpcFunction.Unsubscribe> unsubscribe = new AtomicReference<>();
    ready.then(api -> {
        Promise<IRpcFunction.Unsubscribe<Promise>> invoke = api.rpc().chain().function("subscribeNewHead").invoke(
                (IRpcFunction.SubscribeCallback<Header>) (Header header) ->
                {
                    //System.out.println("Chain is at block: " + JSON.toJSONString(header));
                    System.out.println("Chain is at block: " + header.getBlockNumber());
                });
        return invoke;
    }).then((IRpcFunction.Unsubscribe<Promise> result) -> {
        unsubscribe.set(result);
        synchronized (unsubscribe) {
            unsubscribe.notify();
        }
        System.out.println(" init unsubscribe ");
        return null;
    })._catch((err) -> {
        err.printStackTrace();
        return Promise.value(err);
    });


    synchronized (unsubscribe) {
        unsubscribe.wait();
    }
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("do unsubscribe");
    if (unsubscribe.get() != null) {
        unsubscribe.get().unsubscribe();
    }
}
 
Example 7
Source File: E04_Unsubscribe.java    From api with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // Create an await for the API
    //Promise<ApiPromise> ready = ApiPromise.create();
    initEndPoint(args);

    WsProvider wsProvider = new WsProvider(endPoint);

    Promise<ApiPromise> ready = ApiPromise.create(wsProvider);

    AtomicReference<IRpcFunction.Unsubscribe> unsubscribe = new AtomicReference<>();
    ready.then(api -> {
        Promise<IRpcFunction.Unsubscribe<Promise>> invoke = api.rpc().chain().function("subscribeNewHead").invoke(
                (IRpcFunction.SubscribeCallback<Header>) (Header header) ->
                {
                    //System.out.println("Chain is at block: " + JSON.toJSONString(header));
                    System.out.println("Chain is at block: " + header.getBlockNumber());
                });
        return invoke;
    }).then((IRpcFunction.Unsubscribe<Promise> result) -> {
        unsubscribe.set(result);
        synchronized (unsubscribe) {
            unsubscribe.notify();
        }
        System.out.println(" init unsubscribe ");
        return null;
    })._catch((err) -> {
        err.printStackTrace();
        return Promise.value(err);
    });


    synchronized (unsubscribe) {
        unsubscribe.wait();
    }
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("do unsubscribe");
    if (unsubscribe.get() != null) {
        unsubscribe.get().unsubscribe();
    }
}