当前位置: 首页>>代码示例>>Java>>正文


Java ListenableFuture.get方法代码示例

本文整理汇总了Java中org.threadly.concurrent.future.ListenableFuture.get方法的典型用法代码示例。如果您正苦于以下问题:Java ListenableFuture.get方法的具体用法?Java ListenableFuture.get怎么用?Java ListenableFuture.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.threadly.concurrent.future.ListenableFuture的用法示例。


在下文中一共展示了ListenableFuture.get方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: watchAlreadyDoneFutureWithFailureTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void watchAlreadyDoneFutureWithFailureTest() throws InterruptedException {
  final Throwable rootCause = new Exception();
  ListenableFuture<Object> lfResult = poller.watch(new AlreadyDoneFuture() {
    @Override
    public Object get() throws ExecutionException {
      throw new ExecutionException(rootCause);
    }

    @Override
    public Object get(long timeout, TimeUnit unit) throws ExecutionException {
      throw new ExecutionException(rootCause);
    }
  });
  
  assertTrue(lfResult.isDone());
  try {
    lfResult.get();
    fail("Exception should have thrown");
  } catch (ExecutionException e) {
    assertTrue(e.getCause() == rootCause);
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:24,代码来源:PollerTest.java

示例2: watchIncompleteFutureWithFailureTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void watchIncompleteFutureWithFailureTest() throws InterruptedException {
  poller = new Poller(scheduler, POLL_INTERVAL, POLL_INTERVAL * 2);  // must have timeout to avoid just casting SLF
  Throwable rootCause = new Exception();
  SettableListenableFuture<Object> slf = new SettableListenableFuture<>();
  ListenableFuture<Object> lfResult = poller.watch(slf);
  
  assertFalse(lfResult.isDone());
  
  slf.setFailure(rootCause);
  assertEquals(1, scheduler.advance(POLL_INTERVAL));
  
  assertTrue(lfResult.isDone());
  try {
    lfResult.get();
    fail("Exception should have thrown");
  } catch (ExecutionException e) {
    assertTrue(e.getCause() == rootCause);
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:21,代码来源:PollerTest.java

示例3: submitRunnableExceptionTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void submitRunnableExceptionTest() throws InterruptedException {
  SubmitterExecutorFactory factory = getSubmitterExecutorFactory();
  try {
    SubmitterExecutor executor = factory.makeSubmitterExecutor(TEST_QTY, false);
    
    RuntimeException failure = new SuppressedStackRuntimeException();
    TestRuntimeFailureRunnable tr = new TestRuntimeFailureRunnable(failure);
    ListenableFuture<?> future = executor.submit(tr);
    // no exception should propagate
    
    try {
      future.get();
      fail("Exception should have thrown");
    } catch (ExecutionException e) {
      assertTrue(e.getCause() == failure);
    }
  } finally {
    factory.shutdown();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:22,代码来源:SubmitterExecutorInterfaceTest.java

示例4: submitRunnableWithResultExceptionTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void submitRunnableWithResultExceptionTest() throws InterruptedException {
  SubmitterExecutorFactory factory = getSubmitterExecutorFactory();
  try {
    SubmitterExecutor executor = factory.makeSubmitterExecutor(TEST_QTY, false);
    
    RuntimeException failure = new SuppressedStackRuntimeException();
    TestRuntimeFailureRunnable tr = new TestRuntimeFailureRunnable(failure);
    ListenableFuture<?> future = executor.submit(tr, new Object());
    // no exception should propagate
    
    tr.blockTillFinished();
    assertTrue(future.isDone());
    try {
      future.get();
      fail("Exception should have thrown");
    } catch (ExecutionException e) {
      assertTrue(e.getCause() == failure);
    }
  } finally {
    factory.shutdown();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:24,代码来源:SubmitterExecutorInterfaceTest.java

示例5: submitCallableExceptionTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void submitCallableExceptionTest() throws InterruptedException {
  SubmitterExecutorFactory factory = getSubmitterExecutorFactory();
  try {
    SubmitterExecutor executor = factory.makeSubmitterExecutor(TEST_QTY, false);
    
    final RuntimeException failure = new SuppressedStackRuntimeException();
    ListenableFuture<?> future = executor.submit(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
        throw failure;
      }
    });
    // no exception should propagate
    
    try {
      future.get();
      fail("Exception should have thrown");
    } catch (ExecutionException e) {
      assertTrue(e.getCause() == failure);
    }
  } finally {
    factory.shutdown();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:26,代码来源:SubmitterExecutorInterfaceTest.java

示例6: startWitExecutorAndTimeoutTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void startWitExecutorAndTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException {
  StrictPriorityScheduler ps = new StrictPriorityScheduler(2);
  try {
    long start = Clock.accurateForwardProgressingMillis();
    ListenableFuture<String> lf = profiler.start(ps, DELAY_TIME);
    String result = lf.get(10 * 1000, TimeUnit.MILLISECONDS);
    long end = Clock.accurateForwardProgressingMillis();

    // profiler should be stopped now
    assertFalse(profiler.isRunning());
    assertTrue(end - start >= DELAY_TIME);
    assertNotNull(result);
  } finally {
    ps.shutdownNow();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:18,代码来源:ProfilerTest.java

示例7: limitTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void limitTest() throws InterruptedException, ExecutionException {
  int rateLimit = 100;
  final AtomicInteger ranPermits = new AtomicInteger();
  PriorityScheduler pse = new StrictPriorityScheduler(32);
  try {
    RateLimiterExecutor rls = new RateLimiterExecutor(pse, rateLimit);
    ListenableFuture<?> lastFuture = null;
    double startTime = Clock.accurateForwardProgressingMillis();
    boolean flip = true;
    for (int i = 0; i < TEST_QTY * 2; i++) {
      final int permit = 5;
      if (flip) {
        lastFuture = rls.submit(permit, new Runnable() {
          @Override
          public void run() {
            ranPermits.addAndGet(permit);
          }
        });
        flip = false;
      } else {
        lastFuture = rls.submit(permit, new Callable<Void>() {
          @Override
          public Void call() {
            ranPermits.addAndGet(permit);
            return null;
          }
        });
        flip = true;
      }
    }
    lastFuture.get();
    long endTime = Clock.accurateForwardProgressingMillis();
    double actualLimit = ranPermits.get() / ((endTime - startTime) / 1000);
    
    assertEquals(rateLimit, actualLimit, SLOW_MACHINE ? 75 : 50);
  } finally {
    pse.shutdownNow();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:41,代码来源:RateLimiterExecutorTest.java

示例8: startWithTimeoutTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void startWithTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException {
  long start = Clock.accurateForwardProgressingMillis();
  ListenableFuture<String> lf = profiler.start(DELAY_TIME);
  String result = lf.get(DELAY_TIME + (10 * 1000), TimeUnit.MILLISECONDS);
  long end = Clock.accurateForwardProgressingMillis();

  // profiler should be stopped now
  assertFalse(profiler.isRunning());
  assertTrue(end - start >= DELAY_TIME);
  assertNotNull(result);
}
 
开发者ID:threadly,项目名称:threadly,代码行数:13,代码来源:ProfilerTest.java

示例9: limitTest

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void limitTest() throws InterruptedException, ExecutionException {
  Object key = new Object();
  int rateLimit = 100;
  final AtomicInteger ranPermits = new AtomicInteger();
  PriorityScheduler pse = new StrictPriorityScheduler(32);
  try {
    KeyedRateLimiterExecutor krls = new KeyedRateLimiterExecutor(pse, rateLimit);
    ListenableFuture<?> lastFuture = null;
    double startTime = Clock.accurateForwardProgressingMillis();
    boolean flip = true;
    for (int i = 0; i < TEST_QTY * 2; i++) {
      final int permit = 5;
      if (flip) {
        lastFuture = krls.submit(permit, key, new Runnable() {
          @Override
          public void run() {
            ranPermits.addAndGet(permit);
          }
        });
        flip = false;
      } else {
        lastFuture = krls.submit(permit, key, new Callable<Void>() {
          @Override
          public Void call() {
            ranPermits.addAndGet(permit);
            return null;
          }
        });
        flip = true;
      }
    }
    lastFuture.get();
    long endTime = Clock.accurateForwardProgressingMillis();
    double actualLimit = ranPermits.get() / ((endTime - startTime) / 1000);
    
    assertEquals(rateLimit, actualLimit, SLOW_MACHINE ? 75 : 50);
  } finally {
    pse.shutdownNow();
  }
}
 
开发者ID:threadly,项目名称:threadly,代码行数:42,代码来源:KeyedRateLimiterExecutorTest.java

示例10: main

import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@SuppressWarnings("javadoc")
public static void main(final String args[]) throws InterruptedException {
  if (args.length == 0) {
    System.err.println("No number to test provided");
    System.err.println("Usage: java -cp threadly_examples.jar " + 
                         NextPrime.class.getName() + " [number to start search from]...");
    System.exit(1);
  }
  
  final int processingThreads = Runtime.getRuntime().availableProcessors() * 2;
  int threadPoolSize = processingThreads + args.length - 1;
  final PriorityScheduler executor = new PriorityScheduler(threadPoolSize, true);
  executor.prestartAllThreads();
  
  Deque<ListenableFuture<PrimeResult>> futures = new ArrayDeque<ListenableFuture<PrimeResult>>(processingThreads * 2);
  
  int value = Integer.parseInt(args[0]) + 1;
  while (true) {
    final int f_i = value++;
    futures.add(executor.submit(new Callable<PrimeResult>() {
      @Override
      public PrimeResult call() throws InterruptedException {
        return testNumber(executor, processingThreads, f_i);
      }
    }));
    
    boolean firstRun = true;
    while (firstRun || futures.size() > processingThreads * 2) {
      firstRun = false;
      Iterator<ListenableFuture<PrimeResult>> it = futures.iterator();
      while (it.hasNext()) {
        ListenableFuture<PrimeResult> f = it.next();
        if (f.isDone()) {
          try {
            PrimeResult result = f.get();
            if (result.isPrime) {
              System.out.println("The next prime number is: " + result.testNumber);
              System.exit(0);
            } else {
              it.remove();
            }
          } catch (ExecutionException e) {
            throw ExceptionUtils.makeRuntime(e.getCause());
          }
        } else {
          break;
        }
      }
      
      Thread.sleep(10);
    }
  }
}
 
开发者ID:threadly,项目名称:threadly_examples,代码行数:54,代码来源:NextPrime.java


注:本文中的org.threadly.concurrent.future.ListenableFuture.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。