本文整理汇总了Java中java.util.concurrent.ScheduledExecutorService.schedule方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledExecutorService.schedule方法的具体用法?Java ScheduledExecutorService.schedule怎么用?Java ScheduledExecutorService.schedule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ScheduledExecutorService
的用法示例。
在下文中一共展示了ScheduledExecutorService.schedule方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tokenSession
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public void tokenSession(String token, Countly.CountlyMessagingMode mode) {
checkInternalState();
final String data = "app_key=" + appKey_
+ "×tamp=" + Countly.currentTimestampMs()
+ "&hour=" + Countly.currentHour()
+ "&dow=" + Countly.currentDayOfWeek()
+ "&token_session=1"
+ "&android_token=" + token
+ "&test_mode=" + (mode == Countly.CountlyMessagingMode.TEST ? 2 : 0)
+ "&locale=" + DeviceInfo.getLocale()
+ "&sdk_version=" + Countly.COUNTLY_SDK_VERSION_STRING
+ "&sdk_name=" + Countly.COUNTLY_SDK_NAME;
// To ensure begin_session will be fully processed by the server before token_session
final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
worker.schedule(new Runnable() {
@Override
public void run() {
store_.addConnection(data);
tick();
}
}, 10, TimeUnit.SECONDS);
}
示例2: startTimer
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private static void startTimer(View v, final int minutes, Context c) {
final String impossible = c.getString(R.string.impossible);
final String minute = c.getString(R.string.minute);
final String minutess = c.getString(R.string.minutes);
final String stop = c.getString(R.string.stop);
final String minuteTxt;
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final int delay = (minutes * 60) * 1000;
if (delay == 0) {
Toast.makeText(c, impossible, Toast.LENGTH_LONG).show();
return;
}
if (minutes < 10) {
minuteTxt = minute;
} else {
minuteTxt = minutess;
}
mTask = scheduler.schedule(new runner(c), delay, TimeUnit.MILLISECONDS);
Toast.makeText(c, stop + " " + minutes + " " + minuteTxt, Toast.LENGTH_LONG).show();
running = true;
setState(true);
reduceVolume(delay);
}
示例3: main
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public static void main(String[] args) {
Monitor monitor = createMonitor();
MonitorServer server = MonitorServer
.create(monitor::currentStatistics)
.start();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(monitor::updateStatistics, 1, 1, TimeUnit.SECONDS);
scheduler.schedule(() -> {
scheduler.shutdown();
server.shutdown();
},
10,
TimeUnit.SECONDS);
}
示例4: scheduleTimeout
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public void scheduleTimeout(ScheduledExecutorService timer, final Runnable timeoutCallback, TimeValue timeValue) {
synchronized (this) {
if (timeoutFuture != null) {
throw new IllegalStateException("scheduleTimeout may only be called once");
}
if (started == false) {
timeoutFuture = timer.schedule(new Runnable() {
@Override
public void run() {
if (remove(TieBreakingPrioritizedRunnable.this)) {
runAndClean(timeoutCallback);
}
}
}, timeValue.nanos(), TimeUnit.NANOSECONDS);
}
}
}
示例5: testNewSingleThreadScheduledExecutor
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* a newSingleThreadScheduledExecutor successfully runs delayed task
*/
public void testNewSingleThreadScheduledExecutor() throws Exception {
final ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch proceed = new CountDownLatch(1);
final Runnable task = new CheckedRunnable() {
public void realRun() {
await(proceed);
}};
long startTime = System.nanoTime();
Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
timeoutMillis(), MILLISECONDS);
assertFalse(f.isDone());
proceed.countDown();
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
assertSame(Boolean.TRUE, f.get());
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
}
}
示例6: schedule
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Override
public Disposable schedule(final Runnable action, long delayTime, TimeUnit unit) {
if (innerSubscription.isDisposed()) {
return Disposables.empty();
}
final ScheduledAction scheduledAction = new ScheduledAction(action, operationQueue);
final ScheduledExecutorService executor = IOSScheduledExecutorPool.getInstance();
Future<?> future;
if (delayTime <= 0) {
future = executor.submit(scheduledAction);
} else {
future = executor.schedule(scheduledAction, delayTime, unit);
}
scheduledAction.add(Disposables.fromFuture(future));
scheduledAction.addParent(innerSubscription);
return scheduledAction;
}
示例7: JdbcQuery
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public JdbcQuery ( final StorageDao jdbcStorageDao, final Filter filter, final ScheduledExecutorService executor, final List<JdbcQuery> openQueries ) throws SQLException, NotSupportedException
{
openQueries.add ( this );
this.openQueries = new WeakReference<List<JdbcQuery>> ( openQueries );
this.jdbcStorageDao = jdbcStorageDao;
this.resultSet = jdbcStorageDao.queryEvents ( filter );
this.statement = this.resultSet.getStatement ();
this.hasMore = this.resultSet.next ();
this.future = executor.schedule ( new Callable<Boolean> () {
@Override
public Boolean call ()
{
logger.warn ( "Query '{}' was open for over an hour, or service is being shut down, and will now be closed automatically" );
dispose ();
return true;
}
}, 1, TimeUnit.HOURS );
}
示例8: startTimeout
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
void startTimeout(final long time, final TimeUnit unit, final ScheduledExecutorService scheduler) {
this.lock.lock();
try {
final ScheduledFuture<?> future = scheduler.schedule(this::cancel, time, unit);
final ChronoUnit cronut = chronoUnit(unit);
final Timeout t = new TimeoutBuilder().future(future).finishAt(Instant.now().plus(time, cronut)).build();
final Timeout old = this.timeout.get();
this.timeout.set(t);
// try to cancel as we are replacing the timeout, best effort
if (old != null) {
old.future().cancel(false);
}
} finally {
this.lock.unlock();
}
}
示例9: testNewScheduledThreadPool
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* a newScheduledThreadPool successfully runs delayed task
*/
public void testNewScheduledThreadPool() throws Exception {
final ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch proceed = new CountDownLatch(1);
final Runnable task = new CheckedRunnable() {
public void realRun() {
await(proceed);
}};
long startTime = System.nanoTime();
Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
timeoutMillis(), MILLISECONDS);
assertFalse(f.isDone());
proceed.countDown();
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
assertSame(Boolean.TRUE, f.get());
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
}
}
示例10: start
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public void start(){
StringBuilder b;
Path p;
File[] storageLocations = new File[storageTiering.length];
for (int i = 0; i < storageTiering.length; i++) {
storageLocations[i] = new File(storageTiering[i]);
}
Runnable clean=()->{
for (int i = 0; i < index.length(); i++) {
for (int z = 0; z < storageTiering.length; z++) {
if (a.get(i).getAccessAverage() < thresholds[z]) {
b.append(storageTiering[i]);
b.append("/");
if (a.get(i).getLocationTier() != 0) {
String str = new String(Files.readAllBytes( Paths.get( index.get(i).getPath())));
dataStore.put(a.get(i), str);
File file=new File(index.get(a).getPath());
file.delete();
}
b.append(a.get(i).getTitle());
b.append(".txt");
p.get(b.toString());
Files.write(p, dataStore.get(a.get(i)).getBytes());
index.get(i).setPath(b.toString());
b.setLength(0);
index.get(i).setLocationTier(z);
}
}
}
};
ScheduledExecutorService service=Executors.newScheduledThreadPool(1);
ScheduledFuture future=service.schedule(clean, (long)checkInterval,unit);
}
示例11: createWindow
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Override
public void createWindow(String caption) throws Exception {
super.createWindow(caption);
final ProgressIndicatorWindow progressIndicatorWindow = new ProgressIndicatorWindow();
UI.getCurrent().addWindow(progressIndicatorWindow);
progressIndicatorWindow.bringToFront();
Runnable seviceCall = new Runnable() {
@Override
public void run() {
// Make service calls in the UI thread, since the calls will update the UI components
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
try {
makeServiceCalls(progressIndicatorWindow);
} finally {
progressIndicatorWindow.close();
}
}
});
}
};
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.schedule(seviceCall, 1, TimeUnit.MILLISECONDS);
}
示例12: main
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public static void main(String[] args) {
final LoadMonitor monitor = new LoadMonitor(null);
final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
final ScheduledFuture<?> monitorTask =
monitor.startMonitoring(scheduler);
final ScheduledFuture<?> printTask =
monitor.printMonitoring(scheduler);
// Run the tasks for 2 minutes
scheduler.schedule(
new Runnable() {
public void run() {
monitorTask.cancel(true);
printTask.cancel(true);
}
}, 5*60, TimeUnit.SECONDS);
}
示例13: initialize
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* Init the HEOS group. Starts an extra thread to avoid blocking
* during start up phase. Gathering all information can take longer
* than 5 seconds which can throw an error within the openhab system.
*/
@Override
public void initialize() {
this.gid = this.thing.getConfiguration().get(GID).toString();
api.registerforChangeEvents(this);
ScheduledExecutorService executerPool = Executors.newScheduledThreadPool(1);
executerPool.schedule(new InitializationRunnable(), 4, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
updateState(CH_ID_STATUS, StringType.valueOf(ONLINE));
super.initialize();
}
示例14: startClients
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private void startClients(int count, ScheduledExecutorService executorService) {
for (int i = 0; i < count; i++) {
executorService.schedule(() -> {
try {
new ProcessRunnerImpl().start(CLIENT_START_COMMAND);
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage());
}
}, CLIENT_START_DELAY_SEC + i, TimeUnit.SECONDS);
}
}
示例15: trialRunEngine
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* Trial run of the engine. Shuts down after {@code timeout} seconds after startup.
*
* @param engine the engine.
* @param timeout timeout in seconds.
*/
public static void trialRunEngine(Engine engine, int timeout) {
final Semaphore semaphore = new Semaphore(0, true);
// Startup the engine. After startup the engine runs on the threads other than the current one.
engine.startup();
try {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(() -> {
// Release the semaphore after timeout.
semaphore.release();
}, timeout, TimeUnit.SECONDS);
try {
// Wait for releasing the semaphore after timeout.
semaphore.acquire();
} catch (InterruptedException e) {
logger.warn("trialRunEngine", e);
}
executor.shutdown();
} finally {
// Shutdown the engine.
engine.shutdown();
}
}