本文整理汇总了Java中java.util.concurrent.RunnableScheduledFuture类的典型用法代码示例。如果您正苦于以下问题:Java RunnableScheduledFuture类的具体用法?Java RunnableScheduledFuture怎么用?Java RunnableScheduledFuture使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RunnableScheduledFuture类属于java.util.concurrent包,在下文中一共展示了RunnableScheduledFuture类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTransfer
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
/**
* Called in order to initialize a connection with a remote port...
*
* @param host
* @param port
*/
public String initTransfer(final String host, final int port, final boolean isPullMode,
final String[] fileList, final String destDir, final FDTPropsDialog d, final boolean isRecursive) {
// start by constructing a dummy config
constructConfig(host, port, isPullMode, fileList, destDir, d, isRecursive);
HeaderBufferPool.initInstance();
fdtInternalMonitoringTask = (RunnableScheduledFuture) Utils.getMonitoringExecService().scheduleWithFixedDelay(FDTInternalMonitoringTask.getInstance(), 1, 5, TimeUnit.SECONDS);
consoleReporting = (RunnableScheduledFuture) Utils.getMonitoringExecService().scheduleWithFixedDelay(ConsoleReportingTask.getInstance(), 1, 2, TimeUnit.SECONDS);
// the session manager will check the "pull/push" mode and start the FDTSession
try {
currentSession = FDTSessionManager.getInstance().addFDTClientSession(port);
fdtSessionMTask = currentSession.getMonitoringTask();
} catch (Throwable t) {
logger.log(Level.WARNING, "Got exception when initiating transfer", t);
return t.getLocalizedMessage();
}
return null;
}
示例2: realMain
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
public static void realMain(String... args) throws InterruptedException {
// our tickle service
ScheduledExecutorService tickleService =
new ScheduledThreadPoolExecutor(concurrency) {
// We override decorateTask() to return a custom
// RunnableScheduledFuture which explicitly removes
// itself from the queue after cancellation.
protected <V> RunnableScheduledFuture<V>
decorateTask(Runnable runnable,
RunnableScheduledFuture<V> task) {
final ScheduledThreadPoolExecutor exec = this;
return new CustomRunnableScheduledFuture<V>(task) {
// delegate to wrapped task, except for:
public boolean cancel(boolean b) {
// cancel wrapped task & remove myself from the queue
return (task().cancel(b)
&& exec.remove(this));}};}};
for (int i = 0; i < concurrency; i++)
new ScheduledTickle(i, tickleService)
.setUpdateInterval(25, MILLISECONDS);
done.await();
tickleService.shutdown();
pass();
}
示例3: hasActiveTasks
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
/**
* Determines whether there are tasks which have started and not completed.
*
* As a side effect, this method removes all tasks which are done but are
* still in the tracking list.
*
* @return {@code true} is active tasks exist.
*/
public boolean hasActiveTasks() {
boolean doesHaveTasks = false;
synchronized (asyncTasks) {
if (asyncTasks.isEmpty())
return false;
Iterator<RunnableScheduledFuture<?>> i = asyncTasks.iterator();
while (i.hasNext()) {
RunnableScheduledFuture<?> task = i.next();
if (task.isDone())
i.remove();
else
doesHaveTasks = true;
}
}
return doesHaveTasks;
}
示例4: decorateTask
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(
Runnable runnable, RunnableScheduledFuture<V> r) {
r = super.decorateTask(runnable, r);
for (; ; ) {
final int id = idGenerator.next();
Task<V> task;
if (runnable instanceof ProjectRunnable) {
task = new ProjectTask<>((ProjectRunnable) runnable, r, this, id);
} else {
task = new Task<>(runnable, r, this, id);
}
if (all.putIfAbsent(task.getTaskId(), task) == null) {
return task;
}
}
}
示例5: decorateTask
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task) {
int priority = PRIORITY_INITIAL;
if (task.getDelay(DEFAULT_TIMEUNIT) <= 0) {
priority += PRIORITY_IMMEDIATE_OFFSET;
}
if (callable instanceof UpdateCallable<?>) {
UpdateCallable<?> updateCallable = (UpdateCallable<?>)callable;
if (updateCallable.context.getDocument().getDocument() != null) {
priority += PRIORITY_FOREGROUND_OFFSET;
}
}
return new PriorityInsertionRunnableScheduledFuture<>(task, priority);
}
示例6: queue
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
protected <V> RunnableScheduledFuture<V> queue(final RunnableScheduledFuture<V> future) {
if (isShutdown()) {
throw new RejectedExecutionException();
}
if (getPoolSize() < getCorePoolSize()) {
prestartCoreThread();
}
if (future instanceof DominoFutureTask) {
DominoFutureTask<?> dft = (DominoFutureTask<?>) future;
tasks.put(dft.sequenceNumber, dft);
if (dft.getDelay(TimeUnit.NANOSECONDS) > 0) {
dft.setState(TaskState.SLEEPING);
}
}
super.getQueue().add(future);
return future;
}
示例7: decorateTask
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(
Runnable runnable, RunnableScheduledFuture<V> task) {
// This gets called by ScheduledThreadPoolExecutor before scheduling a Runnable.
ensureRunning();
return task;
}
示例8: decorateTask
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
/**
* Method that converts a RunnableScheduledFuture task in a MyScheduledTask task
*/
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable,
RunnableScheduledFuture<V> task) {
MyScheduledTask<V> myTask=new MyScheduledTask<V>(runnable, null, task,this);
return myTask;
}
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:10,代码来源:MyScheduledThreadPoolExecutor.java
示例9: decorateTask
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task) {
RunnableScheduledFuture<V> future = super.decorateTask(runnable, task);
if (mTasks == null) {
synchronized (BackgroundScheduledThreadPoolExecutor.class) {
if (mTasks == null) {
mTasks = new ConcurrentHashMap<>();
}
}
}
mTasks.put(future, runnable);
return future;
}
示例10: afterExecute
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (r instanceof RunnableScheduledFuture) {
RunnableScheduledFuture<?> future = (RunnableScheduledFuture<?>) r;
Runnable task = mTasks.get(future);
if (future.isCancelled() || task == null) { // 当Runnable在run里面cancel自己时还会执行afterExecute方法;
if (LogUtils.isDebug()) {
LogUtils.d(TAG, "afterExecute.isCancelled.futrue = " + r + ", throwable = " + t);
}
} else {
int futureHashCode = future.hashCode();
if (LogUtils.isDebug() && mDebugTimes != null) { // LogUtils.isDebug()是动态设置的,有可能在beforeExecute里为false,在afterExecute里为true了;
LogUtils.d(TAG, "afterExecute.task = " + task
+ ", time = " + (SystemClock.elapsedRealtime() - mDebugTimes.get(futureHashCode))
+ ", throwable = " + t
+ ", sBackgroundExecutor = " + this);
if (!future.isPeriodic()) {
mDebugTimes.remove(futureHashCode);
}
}
if (!future.isPeriodic()) {
mRunningTasks.remove(task.hashCode());
mTasks.remove(future);
}
}
}
checkAndThrowThreadPoolExecutorThrowable(TAG + ".afterExecute", r, t);
}
示例11: doStateMaintenance
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
void doStateMaintenance() {
while(!isShutdown()) {
RunnableScheduledFuture<?> toSchedule;
while((toSchedule = submittedScheduledTasks.poll()) != null)
delayedTasks.add(toSchedule);
RunnableScheduledFuture<?> toExecute;
while((toExecute = delayedTasks.peek()) != null && toExecute.getDelay(TimeUnit.NANOSECONDS) <= 0) {
delayedTasks.poll();
immediateExecutor.executeWithoutWakeup(toExecute);
}
RunnableScheduledFuture<?> nextTask = delayedTasks.peek();
// signal current thread as suspended before we actually check work queues.
// this avoids wakeupWaiter() seeing an inconsistent state
currentSleeper.set(Thread.currentThread());
if(executorQueue.isEmpty() && submittedScheduledTasks.isEmpty()) {
if(nextTask != null)
LockSupport.parkNanos(nextTask.getDelay(TimeUnit.NANOSECONDS));
else
LockSupport.park();
currentSleeper.set(null);
} else {
currentSleeper.set(null);
// there are unmatched tasks in the queue, return this thread to the pool
break;
}
}
// reschedule if we fall out of loop
if(!isShutdown())
immediateExecutor.executeWithoutWakeup(scheduler);
}
示例12: cancelAllAsyncTasks
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
private int cancelAllAsyncTasks(boolean mayInterruptIfRunning) {
int notCanceled = 0;
synchronized (asyncTasks) {
for (RunnableScheduledFuture<?> task : asyncTasks) {
if (!task.cancel(mayInterruptIfRunning))
notCanceled++;
}
// remove tasks which are done
hasActiveTasks();
}
return notCanceled;
}
示例13: setTimeout
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
private void setTimeout(final long timeoutMillis) {
if (timeoutMillis > 0) {
_future = (RunnableScheduledFuture<?>) _dispatchJob.getDispatcher().getJobTimeoutExecutor().schedule(this, timeoutMillis, TimeUnit.MILLISECONDS);
} else {
_future = null;
}
}
示例14: isAllTasksCancelled
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
boolean isAllTasksCancelled() {
for(RunnableScheduledFuture<?> task: tasks) {
if (!task.isCancelled()) {
return false;
}
}
return true;
}
示例15: Task
import java.util.concurrent.RunnableScheduledFuture; //导入依赖的package包/类
Task(Runnable runnable, RunnableScheduledFuture<V> task, Executor executor, int taskId) {
this.runnable = runnable;
this.task = task;
this.executor = executor;
this.taskId = taskId;
this.running = new AtomicBoolean();
this.startTime = new Date();
}