java.util.concurrent.Exchanger Java Examples

The following examples show how to use java.util.concurrent.Exchanger. 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: ConnectorStopDeadlockTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #2
Source File: MultiThreadTest.java    From java-master with Apache License 2.0 6 votes vote down vote up
@Test
public void test11() throws Exception {
    Exchanger<List<String>> exchanger = new Exchanger<>();
    List<String> producerList = new CopyOnWriteArrayList<>();
    List<String> consumerList = new CopyOnWriteArrayList<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.submit(() -> {
        ProducerExchanger producerExchanger = new ProducerExchanger(exchanger, producerList);
        while (true) {
            producerExchanger.produce();
        }
    });
    executorService.submit(() -> {
        ConsumerExchanger consumerExchanger = new ConsumerExchanger(exchanger, consumerList);
        while (true) {
            consumerExchanger.consumer();
        }
    });
    executorService.shutdown();
    TimeUnit.SECONDS.sleep(1000);
}
 
Example #3
Source File: ConnectorStopDeadlockTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #4
Source File: ConnectorStopDeadlockTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #5
Source File: TestReadOnlyZKClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotCloseZkWhenPending() throws Exception {
  ZooKeeper mockedZK = mock(ZooKeeper.class);
  Exchanger<AsyncCallback.DataCallback> exchanger = new Exchanger<>();
  doAnswer(i -> {
    exchanger.exchange(i.getArgument(2));
    return null;
  }).when(mockedZK).getData(anyString(), anyBoolean(),
    any(AsyncCallback.DataCallback.class), any());
  doAnswer(i -> null).when(mockedZK).close();
  when(mockedZK.getState()).thenReturn(ZooKeeper.States.CONNECTED);
  RO_ZK.zookeeper = mockedZK;
  CompletableFuture<byte[]> future = RO_ZK.get(PATH);
  AsyncCallback.DataCallback callback = exchanger.exchange(null);
  // 2 * keep alive time to ensure that we will not close the zk when there are pending requests
  Thread.sleep(6000);
  assertNotNull(RO_ZK.zookeeper);
  verify(mockedZK, never()).close();
  callback.processResult(Code.OK.intValue(), PATH, null, DATA, null);
  assertArrayEquals(DATA, future.get());
  // now we will close the idle connection.
  waitForIdleConnectionClosed();
  verify(mockedZK, times(1)).close();
}
 
Example #6
Source File: ConnectorStopDeadlockTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #7
Source File: ExchangerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * exchange exchanges objects across two threads
 */
public void testExchange() {
    final Exchanger e = new Exchanger();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            assertSame(one, e.exchange(two));
            assertSame(two, e.exchange(one));
        }});
    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            assertSame(two, e.exchange(one));
            assertSame(one, e.exchange(two));
        }});

    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example #8
Source File: ExchangerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed exchange exchanges objects across two threads
 */
public void testTimedExchange() {
    final Exchanger e = new Exchanger();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
            assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
        }});
    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
            assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
        }});

    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example #9
Source File: ExchangeLoops.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void oneRun(int nthreads, int iters) throws Exception {
    LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
    CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
    Exchanger<Int> l = null;
    Exchanger<Int> r = new Exchanger<>();
    for (int i = 0; i < nthreads; ++i) {
        pool.execute(new Stage(l, r, barrier, iters));
        l = r;
        r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
    }
    barrier.await();
    barrier.await();
    long time = timer.getTime();
    if (print)
        System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
}
 
Example #10
Source File: ExchangerUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenThread_WhenExchangedMessage_thenCorrect() throws InterruptedException, ExecutionException {
    Exchanger<String> exchanger = new Exchanger<>();

    Runnable runner = () -> {
        try {
            String message = exchanger.exchange("from runner");
            assertEquals("to runner", message);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }
    };

    CompletableFuture<Void> result = CompletableFuture.runAsync(runner);
    String msg = exchanger.exchange("to runner");
    assertEquals("from runner", msg);
    result.join();
}
 
Example #11
Source File: TestConcurrentQueryIndex.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void testContendedPut() throws Exception
{
    final int max = 1000000;
    CachedQuery orderedData[] = new CachedQuery[max];
    for(int i=0;i<max;i++)
    {
        CachedQuery o = createCachedQuery(i);
        orderedData[i] = o;
    }
    final CachedQuery data[] = shuffle(orderedData);
    final Exchanger exchanger = new Exchanger();
    final NonLruQueryIndex index = new NonLruQueryIndex();
    PutRunnableWithExchange first = new PutRunnableWithExchange(0, max, data, index, exchanger);
    PutRunnableWithExchange second = new PutRunnableWithExchange(0, max, data, index, exchanger);
    ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
    ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
    firstThread.start();
    secondThread.start();
    firstThread.joinWithExceptionHandling();
    secondThread.joinWithExceptionHandling();
    assertEquals(max, index.size());
    assertEquals(max, index.getEntryCount());
    first.verifyExistence();
    second.verifyExistence();
}
 
Example #12
Source File: TestConcurrentStringIndex.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void testContendedGetIfAbsentPut() throws Exception
{
    final int max = 1000000;
    String orderedData[] = new String[max];
    for(int i=0;i<max;i++)
    {
        String o = createData(i);
        orderedData[i] = o;
    }
    final String data[] = shuffle(orderedData);
    final Exchanger exchanger = new Exchanger();
    final StringIndex index = createStringPool();
    GetIfAbsentPutRunnableWithExchange first = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
    GetIfAbsentPutRunnableWithExchange second = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
    ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
    ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
    firstThread.start();
    secondThread.start();
    firstThread.joinWithExceptionHandling();
    secondThread.joinWithExceptionHandling();
    assertEquals(max, index.size());
    first.verifyExistence();
    second.verifyExistence();
}
 
Example #13
Source File: TestConcurrentWeakPool.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void testContendedGetIfAbsentPut() throws Exception
{
    final int max = 1000000;
    String orderedData[] = new String[max];
    for(int i=0;i<max;i++)
    {
        String o = createData(i);
        orderedData[i] = o;
    }
    final String data[] = shuffle(orderedData);
    final Exchanger exchanger = new Exchanger();
    final ConcurrentWeakPool index = createStringPool();
    GetIfAbsentPutRunnableWithExchange first = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
    GetIfAbsentPutRunnableWithExchange second = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
    ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
    ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
    firstThread.start();
    secondThread.start();
    firstThread.joinWithExceptionHandling();
    secondThread.joinWithExceptionHandling();
    assertEquals(max, index.size());
    first.verifyExistence();
    second.verifyExistence();
}
 
Example #14
Source File: ExchangerTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * exchange exchanges objects across two threads
 */
public void testExchange() {
    final Exchanger e = new Exchanger();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            assertSame(one, e.exchange(two));
            assertSame(two, e.exchange(one));
        }});
    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            assertSame(two, e.exchange(one));
            assertSame(one, e.exchange(two));
        }});

    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example #15
Source File: TestFullUniqueIndex.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void testContendedGetIfAbsentPut() throws Exception
{
    final int max = 1000000;
    TestObject orderedData[] = new TestObject[max];
    for(int i=0;i<max;i++)
    {
        TestObject o = new TestObject(i);
        orderedData[i] = o;
    }
    final TestObject data[] = shuffle(orderedData);
    final Exchanger exchanger = new Exchanger();
    Extractor[] extractors = {new ShiftedIntExtractor(1)};
    final ConcurrentFullUniqueIndex<TestObject> index = new ConcurrentFullUniqueIndex(extractors, 7);
    PutIfAbsentRunnableWithExchange first = new PutIfAbsentRunnableWithExchange(0, max, data, index, exchanger);
    PutIfAbsentRunnableWithExchange second = new PutIfAbsentRunnableWithExchange(0, max, data, index, exchanger);
    ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
    ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
    firstThread.start();
    secondThread.start();
    firstThread.joinWithExceptionHandling();
    secondThread.joinWithExceptionHandling();
    assertEquals(max, index.size());
    first.verifyExistence();
    second.verifyExistence();
}
 
Example #16
Source File: ConnectorStopDeadlockTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #17
Source File: ConnectorStopDeadlockTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
    System.out.println("clientClosed, will call connectorServer.stop");
    final Exchanger<Void> x = new Exchanger<Void>();
    Thread t = new Thread() {
        public void run() {
            try {
                connectorServer.stop();
            } catch (Exception e) {
                fail(e);
            }
        }
    };
    t.setName("connectorServer.stop");
    t.start();
    waitForBlock(t);
    /* If this thread is synchronized on RMIServerImpl, then
     * the thread that does connectorServer.stop will acquire
     * the clientList lock and then block waiting for the RMIServerImpl
     * lock.  Our call to super.clientClosed will then deadlock because
     * it needs to acquire the clientList lock.
     */
    System.out.println("calling super.clientClosed");
    System.out.flush();
    super.clientClosed(conn);
}
 
Example #18
Source File: StreamExTest.java    From streamex with Apache License 2.0 6 votes vote down vote up
static <T> Collector<T, ?, ?> secondConcurrentAddAssertingCollector(T first, T second) {
    return Collector.<T, Exchanger<T>>of(
            Exchanger::new,
            (exchanger, t) -> {
                T t1;
                try {
                    t1 = exchanger.exchange(t, 1, TimeUnit.SECONDS);
                } catch (InterruptedException | TimeoutException e) {
                    throw new AssertionError("Unexpected exception: ", e);
                }
                assertTrue((t1.equals(first) && t.equals(second) || t1.equals(second) && t.equals(first)));
            },
            (a, b) -> {
                throw new AssertionError(
                        "Combining is not expected within secondConcurrentAddAssertingCollector");
            },
            Collector.Characteristics.CONCURRENT,
            Collector.Characteristics.UNORDERED,
            Collector.Characteristics.IDENTITY_FINISH);
}
 
Example #19
Source File: VideoCaptureAndroid.java    From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private synchronized boolean startCapture(
    final int width, final int height,
    final int min_mfps, final int max_mfps) {
  Log.d(TAG, "startCapture: " + width + "x" + height + "@" +
      min_mfps + ":" + max_mfps);
  if (cameraThread != null || cameraThreadHandler != null) {
    throw new RuntimeException("Camera thread already started!");
  }
  Exchanger<Handler> handlerExchanger = new Exchanger<Handler>();
  cameraThread = new CameraThread(handlerExchanger);
  cameraThread.start();
  cameraThreadHandler = exchange(handlerExchanger, null);

  final Exchanger<Boolean> result = new Exchanger<Boolean>();
  cameraThreadHandler.post(new Runnable() {
      @Override public void run() {
        startCaptureOnCameraThread(width, height, min_mfps, max_mfps, result);
      }
    });
  boolean startResult = exchange(result, false); // |false| is a dummy value.
  orientationListener.enable();
  return startResult;
}
 
Example #20
Source File: TestProductionPipeline.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testRateLimit() throws Exception {
  final TestProducer p = new TestProducer();
  MockStages.setSourceCapture(p);

  final ProductionPipeline pipeline = createProductionPipeline(DeliveryGuarantee.AT_MOST_ONCE, true, 10L, PipelineType.DEFAULT);
  pipeline.registerStatusListener(new MyStateListener());
  final Exchanger<Double> rate = new Exchanger<>();
  new Thread() {
    @Override
    public void run() {
      try {
        long start = System.nanoTime();
        pipeline.run();
        rate.exchange(p.count.doubleValue() * 1000 * 1000 * 1000 / (System.nanoTime() - start));
      } catch (Exception ex) {

      }
    }
  }.start();
  Thread.sleep(10000);
  pipeline.stop();
  Double rateAchieved = rate.exchange(0.0);
  // To account for the slight loss of precision, we compare the "long-ified" versions.
  Assert.assertTrue(rateAchieved.longValue() <= 10);
}
 
Example #21
Source File: TestConcurrentDatedObjectIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
protected static Object waitForOtherThreadAndPassObject(final Exchanger exchanger, Object object)
{
    Object result = null;
    try
    {
        result = exchanger.exchange(object);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    return result;
}
 
Example #22
Source File: TestConcurrentDatedObjectIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public void testContendedGetIfAbsentPut() throws Exception
{
    Timestamp jan = new ImmutableTimestamp(timestampFormat.parse("2003-01-15 00:00:00").getTime());
    Timestamp feb = new ImmutableTimestamp(timestampFormat.parse("2003-02-15 00:00:00").getTime());
    final Timestamp in = new ImmutableTimestamp(System.currentTimeMillis());
    final int max = 1000000;
    BitemporalOrderData orderedData[] = new BitemporalOrderData[max];
    for(int i=0;i<max;i++)
    {
        BitemporalOrderData o = createData(in, jan, i);
        orderedData[i] = o;
    }
    final BitemporalOrderData data[] = shuffle(orderedData);
    final Timestamp[] asOfDates = new Timestamp[] { feb, in };
    final Exchanger exchanger = new Exchanger();
    final ConcurrentDatedObjectIndex index = new ConcurrentDatedObjectIndex(new Extractor[] {BitemporalOrderFinder.orderId()},
            BitemporalOrderFinder.getAsOfAttributes(), new BitemporalOrderDatabaseObject());
    final ExtractorBasedHashStrategy pkStrategy = ExtractorBasedHashStrategy.create(BitemporalOrderFinder.getPrimaryKeyAttributes());
    GetIfAbsentPutRunnableWithExchange first = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, asOfDates, pkStrategy, exchanger);
    GetIfAbsentPutRunnableWithExchange second = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, asOfDates, pkStrategy, exchanger);
    ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
    ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
    firstThread.start();
    secondThread.start();
    firstThread.joinWithExceptionHandling();
    secondThread.joinWithExceptionHandling();
    assertEquals(max, index.size());
    first.verifyExistence();
    second.verifyExistence();
}
 
Example #23
Source File: TestConcurrentDatedObjectIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private GetIfAbsentPutRunnableWithExchange(int start, int max, Object[] data, ConcurrentDatedObjectIndex index,
        Timestamp[] asOfDates, ExtractorBasedHashStrategy pkStrategy, Exchanger exchanger)
{
    this.start = start;
    this.max = max;
    this.data = data;
    this.index = index;
    this.asOfDates = asOfDates;
    this.pkStrategy = pkStrategy;
    this.results = new Object[max - start];
    this.exchanger = exchanger;
}
 
Example #24
Source File: TestConcurrentTransactions.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public void testNonUniqueIndexVisibility()
{

    final Exchanger rendezvous = new Exchanger();

    Runnable runnable1 = new Runnable(){
        public void run()
        {
            waitForOtherThread(rendezvous);
            MithraTransaction tx = MithraManager.getInstance().startOrContinueTransaction();
            Order order = OrderFinder.findOne(OrderFinder.orderId().eq(1));
            order.setState("Unknown");
            waitForOtherThread(rendezvous);
            waitForOtherThread(rendezvous);
            tx.rollback();
        }
    };

    Runnable runnable2 = new Runnable(){
        public void run()
        {
            OrderList orderList = new OrderList(OrderFinder.state().eq("In-Progress"));
            orderList.forceResolve();
            int previousSize = orderList.size();
            waitForOtherThread(rendezvous);
            waitForOtherThread(rendezvous);
            orderList = new OrderList(OrderFinder.state().eq("In-Progress"));
            orderList.forceResolve();
            int newSize = orderList.size();
            waitForOtherThread(rendezvous);
            assertEquals(newSize, previousSize);
        }
    };
    assertTrue(runMultithreadedTest(runnable1, runnable2));
}
 
Example #25
Source File: TestConcurrentWeakPool.java    From reladomo with Apache License 2.0 5 votes vote down vote up
protected static Object waitForOtherThreadAndPassObject(final Exchanger exchanger, Object object)
{
    Object result = null;
    try
    {
        result = exchanger.exchange(object);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    return result;
}
 
Example #26
Source File: SchedulerOffloadTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private void verifyInvokerThread(final Executor executor) throws InterruptedException {
    this.executor = executor;
    Exchanger<Thread> invoker = new Exchanger<>();
    executor.schedule(() -> {
        try {
            invoker.exchange(Thread.currentThread());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new AssertionError("Exchange interrupted.");
        }
    }, 1, TimeUnit.MILLISECONDS);
    Thread taskInvoker = invoker.exchange(Thread.currentThread());
    assertThat("Unexpected thread invoked the task.", taskInvoker.getName(),
            startsWith(EXPECTED_THREAD_PREFIX));
}
 
Example #27
Source File: TestConcurrentStringIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
protected static Object waitForOtherThreadAndPassObject(final Exchanger exchanger, Object object)
{
    Object result = null;
    try
    {
        result = exchanger.exchange(object);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    return result;
}
 
Example #28
Source File: TestConcurrentQueryIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private PutRunnableWithExchange(int start, int max, CachedQuery[] data, NonLruQueryIndex index, Exchanger exchanger)
{
    this.start = start;
    this.max = max;
    this.data = data;
    this.index = index;
    this.results = new CachedQuery[max - start];
    this.exchanger = exchanger;
}
 
Example #29
Source File: TestConcurrentQueryIndex.java    From reladomo with Apache License 2.0 5 votes vote down vote up
protected static Object waitForOtherThreadAndPassObject(final Exchanger exchanger, Object object)
{
    Object result = null;
    try
    {
        result = exchanger.exchange(object);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    return result;
}
 
Example #30
Source File: SelectCredentialDialogFragment.java    From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PublicKeyCredentialSource selectFrom(List<PublicKeyCredentialSource> credentialList) {
    // check to make sure fragmentActivity is populated
    if (fragmentActivity == null) {
        Log.w(TAG, "Must populate fragment activity before calling promptUser");
        return null;
    }
    // store some instance vars for the dialog prompt to use
    this.credentialList = credentialList;
    this.exchanger = new Exchanger<PublicKeyCredentialSource>();

    // show dialog prompt to user
    FragmentActivity fragmentActivityStrongRef = fragmentActivity.get();
    if (fragmentActivityStrongRef == null) {
        Log.w(TAG,"FragmentActivity reference was garbage collected. Returning first matching credential.");
        return credentialList.get(0);
    }
    show(fragmentActivityStrongRef.getSupportFragmentManager(), "credential");

    // wait to retrieve credential
    PublicKeyCredentialSource selectedCredential;
    try {
        selectedCredential = exchanger.exchange(null);
    } catch (InterruptedException exception) {
        Log.w(TAG, "exchange interrupted: " + exception.toString());
        return null;
    }
    return selectedCredential;
}