java.util.Timer.scheduleAtFixedRate()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(86)

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

Timer.scheduleAtFixedRate介绍

[英]Schedule a task for repeated fixed-rate execution after a specific delay has passed.
[中]在经过特定延迟后,安排任务重复执行固定速率。

代码示例

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

/**
 * Start the timer to run at fixed intervals.
 */
@Override
public void start() {
  rotationTimer = new Timer(true);
  TimerTask task = new TimerTask() {
    @Override
    public void run() {
      rotationTimerTriggered.set(true);
    }
  };
  rotationTimer.scheduleAtFixedRate(task, interval, interval);
}

代码示例来源:origin: facebook/facebook-android-sdk

@Override
  public void run() {
    try {
      if (indexingTimer != null) {
        indexingTimer.cancel();
      }
      previousDigest = null;
      indexingTimer = new Timer();
      indexingTimer.scheduleAtFixedRate(
          indexingTask,
          0,
          Constants.APP_INDEXING_SCHEDULE_INTERVAL_MS
      );
    } catch (Exception e) {
      Log.e(TAG, "Error scheduling indexing job", e);
    }
  }
});

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

Timer timer = new Timer();
TimerTask task = new TimerTask() {
  @Override
  public void run() {
    System.out.println(System.currentTimeMillis());
  }
};
timer.scheduleAtFixedRate(task, 0, 1000);

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

int delay = 1000;
int period = 1000;
timer = new Timer();
interval = Integer.parseInt(secs);
System.out.println(secs);
timer.scheduleAtFixedRate(new TimerTask() {
    System.out.println(setInterval());
  timer.cancel();
return --interval;

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

public static void main(String args[]) {
  int period = 2000;
  int delay = 2000;

  Timer timer = new Timer();

  timer.scheduleAtFixedRate(new TimerTask() {
    // Variables as member variables instead of local variables in main()
    private double lastPrice = 0;
    private Price priceObject = new Price();
    private double price = 0;

    public void run() {
      price = priceObject.getNextPrice(lastPrice);
      System.out.println();
      lastPrice = price;
    }
  }, delay, period);      
}

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

while (true) {
      try {
        System.out.println("Thread reading counter is: " + counter);
        if (counter == 3) {
          System.out.println("Counter has reached 3 now will terminate");
          timer.cancel();//end the timer
          break;//end this loop
timer = new Timer("MyTimer");//create a new timer
timer.scheduleAtFixedRate(timerTask, 30, 3000);//start timer in 30ms to increment  counter

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

public void contextInitialized(ServletContextEvent event) {
  Timer timer = new Timer(true);
  timer.scheduleAtFixedRate(new CleanDBTask(), 0, oneHourInMillis);
  timer.scheduleAtFixedRate(new StatisticsTask(), 0, oneQuartInMillis);
}

代码示例来源:origin: k9mail/k-9

@Override
protected void copyData(InputStream inputStream, OutputStream outputStream) throws IOException {
  final CountingOutputStream countingOutputStream = new CountingOutputStream(outputStream);
  Timer timer = new Timer();
  try {
    timer.scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        progressListener.updateProgress(countingOutputStream.getCount());
      }
    }, 0, 50);
    super.copyData(inputStream, countingOutputStream);
  } finally {
    timer.cancel();
  }
}

代码示例来源:origin: SeldonIO/seldon-server

public void createOutputSimilaritiesTimer(Namespace ns)
{
  int windowSecs = ns.getInt("output_poll_secs");
  int timer_ms = windowSecs * 1000;
  System.out.println("Scheduling at "+timer_ms);
  outputTimer = new Timer(true);
  outputTimer.scheduleAtFixedRate(new TimerTask() {
      public void run()  
      {
        long time = ItemSimilarityProcessor.this.outputSimilaritiesTime.get();
        if (time > 0)
        {
          SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy 'at' h:mm a");
          String date = sdf.format(time*1000);
          System.out.println("getting similarities at "+date);
          List<JaccardSimilarity> res = streamJaccard.getSimilarity(time);
          System.out.println("Results size "+res.size()+". Sending Messages...");
          sendMessages(res, time);
          System.out.println("Messages sent");
          ItemSimilarityProcessor.this.outputSimilaritiesTime.set(0);
        }
        else
        {
          System.out.println("Timer: not outputing similarities");
        }
      }
    }, timer_ms, timer_ms);
}

代码示例来源:origin: camunda/camunda-bpm-platform

private void runWorkers(ExecutorService executorService) throws InterruptedException {
 final int numberOfIterations = configuration.getNumberOfIterations();
 final int taskCount = numberOfIterations * configuration.getWorkerTasks().length;
 final CountDownLatch sync = new CountDownLatch(taskCount);
 final Timer timer = new Timer();
 timer.scheduleAtFixedRate(new ProgressReporter(taskCount, sync, configuration.isColor()), 2000, 2000);
 System.out.println("Generating load. Total tasks: "+taskCount+"... ");
 for (int i = 1; i <= numberOfIterations; i++) {
  for (Runnable runnable : configuration.getWorkerTasks()) {
   executorService.execute(wrap(runnable, sync));
  }
 }
 sync.await();
 timer.cancel();
 if(configuration.isColor()) System.out.print(CLEAR_LINE + ANSI_GREEN);
 System.out.println("Finished generating load.");
 if(configuration.isColor()) System.out.print(ANSI_RESET);
 executorService.shutdown();
}

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

new Timer().scheduleAtFixedRate(task, after, interval);

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

private synchronized void scheduleHostLBCheckerTask(final long checkInterval) {
  if (hostLBTimer != null) {
    hostLBTimer.cancel();
  }
  if (checkInterval > 0L) {
    s_logger.info("Scheduling preferred host timer task with host.lb.interval=" + checkInterval + "ms");
    hostLBTimer = new Timer("Host LB Timer");
    hostLBTimer.scheduleAtFixedRate(new PreferredHostCheckerTask(), checkInterval, checkInterval);
  }
}

代码示例来源:origin: knowm/XChange

System.out.println(exchangeSpecification.toString());
Exchange bitcoiniumExchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
Timer timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 10000); // every ten

代码示例来源:origin: ltsopensource/light-task-scheduler

public static void start() {
  if (start.compareAndSet(false, true)) {
    timer = new Timer("AliveKeepingService");
    timer.scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("I'm alive");
        }
      }
    }, 1000 * 60 * 10, 1000 * 60 * 10);
  }
}

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

myTimer = new Timer();
  myTimer.scheduleAtFixedRate(new Task(), START_TIME, RETRY_TIME);
  timerRunning = true;
public int onStartCommand(Intent intent, int flags, int startId) {
  if (!timerRunning) {
    myTimer = new Timer();
    myTimer.scheduleAtFixedRate(new Task(), START_TIME, RETRY_TIME);
    timerRunning = true;
    myTimer.cancel();

代码示例来源:origin: knowm/XChange

System.out.println(exchangeSpecification.toString());
Exchange bitcoiniumExchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
Timer timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 10000); // every ten

代码示例来源:origin: ltsopensource/light-task-scheduler

public static void start() {
  if (start.compareAndSet(false, true)) {
    timer = new Timer("AliveKeepingService");
    timer.scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("I'm alive");
        }
      }
    }, 1000 * 60 * 10, 1000 * 60 * 10);
  }
}

代码示例来源:origin: knowm/XChart

timer.cancel(); // just in case
if (chartInfo.getExampleChartName().startsWith("RealtimeChart01")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart02")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart03")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart04")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart05")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart06")) {
 timer = new Timer();
 timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);

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

import java.util.Timer;
import java.util.TimerTask;

public class Test {

  static int counter = 0;

  public static void main(String[] args) {

    TimerTask timerTask = new TimerTask() {

      @Override
      public void run() {
        System.out.println("TimerTask executing counter is: " + counter);
        counter++;//increments the counter
      }
    };

    Timer timer = new Timer("MyTimer");//create a new Timer

    timer.scheduleAtFixedRate(timerTask, 30, 3000);//this line starts the timer at the same time its executed
  }
}

代码示例来源:origin: igniterealtime/Openfire

/**
 * Add a limited life time to the Session.
 * The Session is stoped and remove from agents List after a certain time.
 * Prevents that network cycles, refreshes a Session forever.
 *
 * @param lifetime time in Seconds to kill the Session
 */
void addLifeTime(long lifetime) {
  lifetime *= 1000;
  if (lifeTimer != null) return;
  lifeTimer = new Timer();
  lifeTimer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
      stopAgent();
    }
  }, lifetime, lifetime);
}

相关文章