java.util.concurrent.Executors.newScheduledThreadPool()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(205)

本文整理了Java中java.util.concurrent.Executors.newScheduledThreadPool()方法的一些代码示例,展示了Executors.newScheduledThreadPool()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Executors.newScheduledThreadPool()方法的具体详情如下:
包路径:java.util.concurrent.Executors
类名称:Executors
方法名:newScheduledThreadPool

Executors.newScheduledThreadPool介绍

[英]Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
[中]创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。

代码示例

代码示例来源:origin: code4craft/webmagic

private void initFlushThread() {
  flushThreadPool = Executors.newScheduledThreadPool(1);
  flushThreadPool.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
      flush();
    }
  }, 10, 10, TimeUnit.SECONDS);
}

代码示例来源:origin: oracle/helidon

private synchronized void planNextTry(long afterMillis) {
  if (scheduledExecutorService == null) {
    scheduledExecutorService = Executors.newScheduledThreadPool(1);
  }
  scheduledExecutorService.schedule(this::tryPublish, afterMillis, TimeUnit.MILLISECONDS);
}

代码示例来源:origin: stackoverflow.com

Runnable helloRunnable = new Runnable() {
  public void run() {
    System.out.println("Hello world");
  }
};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 3, TimeUnit.SECONDS);

代码示例来源:origin: linkedin/parseq

public static void main(String[] args) throws InterruptedException {
  final long viewerId = 0;

  final Set<Long> unclassified = new HashSet<Long>();
  for (long i = 0; i < 20; i++) {
   unclassified.add(i);
  }

  final ScheduledExecutorService serviceScheduler = Executors.newSingleThreadScheduledExecutor();
  final Client restLiClient = new ClientImpl(serviceScheduler);

  final int numCores = Runtime.getRuntime().availableProcessors();
  final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(numCores + 1);
  final Engine engine = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler).build();

  final ClassifierPlanFactory classifier = new ClassifierPlanFactory(restLiClient);
  try {
   final Task<Map<Long, Classification>> classifications = classifier.classify(viewerId, unclassified);
   engine.run(classifications);
   classifications.await();
   System.out.println(classifications.get());

   ExampleUtil.printTracingResults(classifications);
  } finally {
   serviceScheduler.shutdownNow();
   engine.shutdown();
   scheduler.shutdownNow();
  }
 }
}

代码示例来源:origin: LeonardoZ/java-concurrency-patterns

public static void usingScheduledThreadPool() {
  System.out.println("=== ScheduledThreadPool ===");
  ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4);
  scheduledThreadPool.scheduleAtFixedRate(
      () -> System.out.println("Print every 2s"), 0, 2, TimeUnit.SECONDS);
  scheduledThreadPool.scheduleWithFixedDelay(
      () -> System.out.println("Print every 2s delay"), 0, 2, TimeUnit.SECONDS);
  try {
    scheduledThreadPool.awaitTermination(6, TimeUnit.SECONDS);
    scheduledThreadPool.shutdown();
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: biezhi/learn-java8

private static void test1() throws InterruptedException {
  ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
  Runnable           task   = () -> System.out.println("Scheduling: " + System.nanoTime());
  int                delay  = 3;
  ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS);
  TimeUnit.MILLISECONDS.sleep(1337);
  long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
  System.out.printf("Remaining Delay: %sms\n", remainingDelay);
}

代码示例来源:origin: stackoverflow.com

System.out.println("sampling started");
CLASS=className;
METHOD=method;
EXECUTOR = Executors.newScheduledThreadPool(1);
if(t==null) System.out.println("method not seen");
else printCallTree(t, 0, 100);
 sb.append(chPercent).append("% (").append(cht.cpu*percent/num)
  .append("% cpu) ").append(ch.getKey()).append(" ");
 System.out.println(sb.toString());
 printCallTree(cht, ind+2, chPercent);

代码示例来源:origin: stackoverflow.com

import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
 private final ScheduledExecutorService scheduler =
   Executors.newScheduledThreadPool(1);
 public void beepForAnHour() {
   final Runnable beeper = new Runnable() {
       public void run() { System.out.println("beep"); }
     };
   final ScheduledFuture<?> beeperHandle =
     scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
   scheduler.schedule(new Runnable() {
       public void run() { beeperHandle.cancel(true); }
     }, 60 * 60, SECONDS);
 }
}

代码示例来源:origin: prestodb/presto

public TestScanFilterAndProjectOperator()
{
  executor = newCachedThreadPool(daemonThreadsNamed("test-executor-%s"));
  scheduledExecutor = newScheduledThreadPool(2, daemonThreadsNamed("test-scheduledExecutor-%s"));
}

代码示例来源:origin: apache/hive

cacheUpdateMaster = Executors.newScheduledThreadPool(1, new ThreadFactory() {
 @Override
 public Thread newThread(Runnable r) {
 cacheUpdateMaster.scheduleAtFixedRate(new CacheUpdateMasterWork(conf, shouldRunPrewarm), 0,
   cacheRefreshPeriodMS, TimeUnit.MILLISECONDS);
cacheUpdateMaster.schedule(new CacheUpdateMasterWork(conf, shouldRunPrewarm), 0, TimeUnit.MILLISECONDS);

代码示例来源:origin: prestodb/presto

@Provides
@Singleton
@ForExchange
public static ScheduledExecutorService createExchangeExecutor(ExchangeClientConfig config)
{
  return newScheduledThreadPool(config.getClientThreads(), daemonThreadsNamed("exchange-client-%s"));
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws InterruptedException {
  ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
  Runnable r = new Runnable() {
    @Override
    public void run() {
      System.out.println("Hello");
    }
  };
  ScheduledFuture<?> scheduledFuture =
    scheduledExecutorService.scheduleAtFixedRate(r, 1L, 1L, TimeUnit.SECONDS);
  Thread.sleep(5000L);
  scheduledFuture.cancel(false);
}

代码示例来源:origin: linkedin/parseq

final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(numCores + 1);
final Engine engine = new EngineBuilder()
  .setTaskExecutor(scheduler)
 engine.run(classifications);
 classifications.await();
 System.out.println(classifications.get());

代码示例来源:origin: stackoverflow.com

ScheduledExecutorService scheduler = Executors
     .newScheduledThreadPool(1);
 ScheduledFuture<String> future = scheduler.schedule(
     new ScheduledPrinter(), 10, TimeUnit.SECONDS);
 System.out.println(future.get());

代码示例来源:origin: biezhi/learn-java8

private static void test3() {
  ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
  Runnable task = () -> {
    try {
      TimeUnit.SECONDS.sleep(2);
      System.out.println("Scheduling: " + System.nanoTime());
    } catch (InterruptedException e) {
      System.err.println("task interrupted");
    }
  };
  executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS);
}

代码示例来源:origin: prestodb/presto

@BeforeClass
public void setUp()
{
  executor = newCachedThreadPool(daemonThreadsNamed("test-executor-%s"));
  scheduledExecutor = newScheduledThreadPool(2, daemonThreadsNamed("test-scheduledExecutor-%s"));
}

代码示例来源:origin: pinterest/secor

private static void loop(ProgressMonitor progressMonitor, long interval) {
final ProgressMonitor monitor = progressMonitor;
Runnable runner = new Runnable() {
  public void run() {
    try {
    monitor.exportStats();
    } catch (Throwable t) {
    LOG.error("Progress monitor failed", t);
    }
  }
  };
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(runner, 0, interval, TimeUnit.SECONDS);
}

代码示例来源:origin: normanmaurer/netty-in-action

/**
 * Listing 7.2 Scheduling a task with a ScheduledExecutorService
 * */
public static void schedule() {
  ScheduledExecutorService executor =
      Executors.newScheduledThreadPool(10);
  ScheduledFuture<?> future = executor.schedule(
    new Runnable() {
    @Override
    public void run() {
      System.out.println("Now it is 60 seconds later");
    }
  }, 60, TimeUnit.SECONDS);
  //...
  executor.shutdown();
}

代码示例来源:origin: prestodb/presto

@BeforeClass
public void setUp()
{
  stateNotificationExecutor = newScheduledThreadPool(5, daemonThreadsNamed("test-%s"));
}

代码示例来源:origin: biezhi/learn-java8

private static void test2() {
  ScheduledExecutorService executor     = Executors.newScheduledThreadPool(1);
  Runnable                 task         = () -> System.out.println("Scheduling: " + System.nanoTime());
  int                      initialDelay = 0;
  int                      period       = 1;
  executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
}

相关文章