本文整理汇总了Java中com.google.common.util.concurrent.ListenableFutureTask类的典型用法代码示例。如果您正苦于以下问题:Java ListenableFutureTask类的具体用法?Java ListenableFutureTask怎么用?Java ListenableFutureTask使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ListenableFutureTask类属于com.google.common.util.concurrent包,在下文中一共展示了ListenableFutureTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: callFromMainThread
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public <V> ListenableFuture<V> callFromMainThread(Callable<V> callable)
{
Validate.notNull(callable);
if (!this.isCallingFromMinecraftThread() && !this.isServerStopped())
{
ListenableFutureTask<V> listenablefuturetask = ListenableFutureTask.<V>create(callable);
synchronized (this.futureTaskQueue)
{
this.futureTaskQueue.add(listenablefuturetask);
return listenablefuturetask;
}
}
else
{
try
{
return Futures.<V>immediateFuture(callable.call());
}
catch (Exception exception)
{
return Futures.immediateFailedCheckedFuture(exception);
}
}
}
示例2: addScheduledTask
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public <V> ListenableFuture<V> addScheduledTask(Callable<V> callableToSchedule)
{
Validate.notNull(callableToSchedule);
if (!this.isCallingFromMinecraftThread())
{
ListenableFutureTask<V> listenablefuturetask = ListenableFutureTask.<V>create(callableToSchedule);
synchronized (this.scheduledTasks)
{
this.scheduledTasks.add(listenablefuturetask);
return listenablefuturetask;
}
}
else
{
try
{
return Futures.<V>immediateFuture(callableToSchedule.call());
}
catch (Exception exception)
{
return Futures.immediateFailedCheckedFuture(exception);
}
}
}
示例3: addScheduledTask
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public <V> ListenableFuture<V> addScheduledTask(Callable<V> callableToSchedule) {
Validate.notNull(callableToSchedule);
if (!this.isCallingFromMinecraftThread()) {
ListenableFutureTask<V> listenablefuturetask = ListenableFutureTask.<V>create(callableToSchedule);
synchronized (this.scheduledTasks) {
this.scheduledTasks.add(listenablefuturetask);
return listenablefuturetask;
}
} else {
try {
return Futures.<V>immediateFuture(callableToSchedule.call());
} catch (Exception exception) {
return Futures.immediateFailedCheckedFuture(exception);
}
}
}
示例4: addScheduledTask
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public <V> ListenableFuture<V> addScheduledTask(Callable<V> callableToSchedule)
{
Validate.notNull(callableToSchedule);
if (this.isCallingFromMinecraftThread())
{
try
{
return Futures.<V>immediateFuture(callableToSchedule.call());
}
catch (Exception exception)
{
return Futures.immediateFailedCheckedFuture(exception);
}
}
else
{
ListenableFutureTask<V> listenablefuturetask = ListenableFutureTask.<V>create(callableToSchedule);
synchronized (this.scheduledTasks)
{
this.scheduledTasks.add(listenablefuturetask);
return listenablefuturetask;
}
}
}
示例5: main
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
// create ListenableFutureTask instance
ListenableFutureTask<String> listenableFutureTask = ListenableFutureTask.create(() -> {
log.info("listenable future begin to execute");
TimeUnit.SECONDS.sleep(5); // monitor
return "listenable future task done";
});
// add three listeners, use MoreExecutors.directExecutor() to execute Runnable in ExecutorServcie
listenableFutureTask.addListener(()-> log.info("listener one execute"), MoreExecutors.directExecutor());
listenableFutureTask.addListener(() -> log.info("listener two execute"), MoreExecutors.directExecutor());
listenableFutureTask.addListener(() -> log.info("listener three execute"), MoreExecutors.directExecutor());
// execute listenable future task
executorService.execute(listenableFutureTask);
// ExecutorService.shutdown()
executorService.shutdown();
}
示例6: main
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
final CyclicBarrier barrier = new CyclicBarrier(4);
ExecutorService executorService = Executors.newSingleThreadExecutor();
ListenableFutureTask<String> listenableFutureTask = ListenableFutureTask.create(() -> {
log.info("listenable future begin to execute");
TimeUnit.SECONDS.sleep(5);
return "listenable future task done";
});
// !!!!! barrier貌似并不能实现,对比下CountDownLatch,两种控制的应用场景
Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(0, barrier), executorService);
Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(1, barrier), executorService);
Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(2, barrier), executorService);
executorService.execute(listenableFutureTask);
barrier.await();
executorService.shutdown();
}
示例7: main
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
final CountDownLatch latch = new CountDownLatch(3);
//ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ExecutorService executorService = Executors.newSingleThreadExecutor();
ListenableFutureTask<String> listenableFutureTask = ListenableFutureTask.create(() -> {
log.info("listenable future begin to execute");
TimeUnit.SECONDS.sleep(5);
return "listenable future task done";
});
// add three callbacks, use executorService instance to execute
Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(0, latch), executorService);
Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(1, latch), executorService);
Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(2, latch), executorService);
// execute listenable future task
executorService.execute(listenableFutureTask);
// ensure listener execution before ExecutorService#shutdown
if (latch.getCount() > 0) {
latch.await();
}
// ExecutorService.shutdown()
executorService.shutdown();
}
示例8: AbstractJobInstance
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public AbstractJobInstance(Config config) {
Assert.notNull(config.parameters, "parameters is null");
Assert.notNull(config.job, "job is null");
Assert.notNull(config.jobsManager, "jobsManager is null");
Assert.notNull(config.info, "info is null");
this.jobContext = new JobContext(this, config.parameters);
this.infoRef = new AtomicReference<>(config.info);
this.manager = config.jobsManager;
// create wait future with stub
this.atEndFuture = SettableFuture.create();
this.job = config.job;
this.authentication = config.authentication;
this.watcher = config.watcher;
this.cancelFuture = ListenableFutureTask.create(this::innerCancel);
this.startFuture = ListenableFutureTask.create(this::innerStart);
}
示例9: sheduleUpdateHtml
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
private ListenableFuture<Void> sheduleUpdateHtml(final WaveId waveId) {
ListenableFutureTask<Void> task = processingWaves.get(waveId);
if (task == null) {
task = ListenableFutureTask.create(new Callable<Void>() {
@Override
public Void call() throws Exception {
processingWaves.remove(waveId);
try {
updateHtml(waveId);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Failed to make HTML for " + waveId.serialise(), e);
throw e;
}
return null;
}
});
processingWaves.put(waveId, task);
executor.schedule(task, WAVE_UPDATE_HTML_DELAY_MIN, TimeUnit.MINUTES);
}
return task;
}
示例10: close
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
@Override
public ListenableFuture close() {
checkOpened();
writeLock.lock();
try {
closingFutureTask = ListenableFutureTask.<Void>create(closingTask);
if (latestVersionToPersist != null) {
deferredClosing = true;
} else {
persistExecutor.execute(closingFutureTask);
}
return closingFutureTask;
} finally {
writeLock.unlock();
}
}
示例11: sheduleCommitIndex
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
private ListenableFutureTask<Void> sheduleCommitIndex() {
ListenableFutureTask<Void> task = commitTask.get();
if (task == null) {
task = ListenableFutureTask.create(new Callable<Void>() {
@Override
public Void call() throws Exception {
commitTask.set(null);
try {
LOG.info("Commiting indexes...");
indexWriter.commit();
LOG.info("Commiting indexes is complete");
} catch (IndexException e) {
LOG.log(Level.SEVERE, "Index commit failed", e);
throw e;
}
return null;
}
});
commitTask.set(task);
indexExecutor.schedule(task, WAVE_COMMIT_DELAY_SEC, TimeUnit.SECONDS);
}
return task;
}
示例12: loadSegmentWaveletState
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
@VisibleForTesting
static ListenableFuture<SegmentWaveletState> loadSegmentWaveletState(
final WaveletName waveletName,
final WaveletStateFactory waveletStateFactory,
Executor waveletLoadExecutor) {
ListenableFutureTask<SegmentWaveletState> task
= ListenableFutureTask.<SegmentWaveletState>create(
new Callable<SegmentWaveletState>() {
@Override
public SegmentWaveletState call()
throws PersistenceException, OperationException, WaveletStateException, InterruptedException, ExecutionException {
BlockWaveletState blockState = waveletStateFactory.createBlockWaveletState(waveletName);
blockState.open();
SegmentWaveletState state = waveletStateFactory.createSegmentWaveletState(waveletName, blockState);
state.open();
return state;
}
});
waveletLoadExecutor.execute(task);
return task;
}
示例13: load
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
@Override
public ListenableFuture<ArrayChunk> load(final Pair<Integer, Integer> key) throws Exception {
final PyDebugValue value = myProvider.getDebugValue();
final PyDebugValue slicedValue =
new PyDebugValue(myProvider.getSliceText(), value.getType(), value.getValue(), value.isContainer(), value.isErrorOnEval(),
value.getParent(), value.getFrameAccessor());
ListenableFutureTask<ArrayChunk> task = ListenableFutureTask.create(new Callable<ArrayChunk>() {
@Override
public ArrayChunk call() throws Exception {
return value.getFrameAccessor()
.getArrayItems(slicedValue, key.first, key.second, Math.min(CHUNK_ROW_SIZE, getRowCount() - key.first),
Math.min(CHUNK_COL_SIZE, getColumnCount() - key.second),
myProvider.getFormat());
}
});
myExecutorService.execute(task);
return task;
}
示例14: testOneSlice
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
private static void testOneSlice(final String id, final NavigableSet<Integer> test, final NavigableSet<Integer> canon, List<ListenableFuture<?>> results)
{
ListenableFutureTask<?> f = ListenableFutureTask.create(new Runnable()
{
@Override
public void run()
{
test(id + " Count", test.size(), canon.size());
testEqual(id, test.iterator(), canon.iterator());
testEqual(id + "->DSCI", test.descendingIterator(), canon.descendingIterator());
testEqual(id + "->DSCS", test.descendingSet().iterator(), canon.descendingSet().iterator());
testEqual(id + "->DSCS->DSCI", test.descendingSet().descendingIterator(), canon.descendingSet().descendingIterator());
}
}, null);
results.add(f);
COMPARE.execute(f);
}
示例15: a
import com.google.common.util.concurrent.ListenableFutureTask; //导入依赖的package包/类
public <V> ListenableFuture<V> a(Callable<V> callable) {
Validate.notNull(callable);
if (!this.isMainThread() && !this.isStopped()) {
ListenableFutureTask listenablefuturetask = ListenableFutureTask.create(callable);
Queue queue = this.j;
synchronized (this.j) {
this.j.add(listenablefuturetask);
return listenablefuturetask;
}
} else {
try {
return Futures.immediateFuture(callable.call());
} catch (Exception exception) {
return Futures.immediateFailedCheckedFuture(exception);
}
}
}