scheduledexecutorservice

ScheduledExecutorService - Ignore already running runnable

谁说胖子不能爱 提交于 2019-12-25 15:40:10
问题 I'm using a scheduled executor service private ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(1); running a runnable at fixed rate pool.scheduleAtFixedRate(new CoolRunnable(), 10, 10, TimeUnit.MILLISECONDS); This thread pool waits that the previous execution finishes, but i'd like it to run the runnable every 10 milliseconds no matter whether the previous is done or not. How can I do that? EDIT: Fixed the problem replacing the MySQL connection with a connection pool. The

How to schedule a task at fixed rate with a duration longer than the rate?

风格不统一 提交于 2019-12-25 09:03:25
问题 I'm trying to schedule a task that requires ~2.25 sec to be run every second.Thus I know 3 threads should be enough to handle the load. My code looks like this: private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4); scheduler.scheduleAtFixedRate(new Streamer(), 0, 1000, TimeUnit.MILLISECONDS); interestingly this behaves like scheduledAtFixedDelay with a delay of 0 (threads finish every ~2.25 sec). I know, that scheduleAtFixedRate can be late if a thread runs

Pause and resume a ScheduledExecutorService

主宰稳场 提交于 2019-12-24 04:27:45
问题 I'm writing a tetris-clone. I want to make the pieces fall a tad faster after every 60 seconds, and for this I'm using a ScheduledExecutorService: executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { levelUp(); } }, 60, 60, TimeUnit.SECONDS); Now, I can pause the game, and while paused the game obviously stops doing anything until I resume it. As it is now, pausing the game is well and all for everything but this executor taking care of "leveling up". For example, if I

Distinction between ScheduledExecutorService and rolling your own Runnable with Thread.sleep()

匆匆过客 提交于 2019-12-21 20:45:47
问题 What are the benefits of using ScheduledExecutorService's scheduleAtFixedRate() to run a piece of code on a regular basis instead of creating a new Runnable that has a forever loop coupled with a Thread.sleep() that causes the thread to sleep for the desired period? Is there a performance gain with one of the methods? 回答1: The biggest benefit of using ScheduledExecutorService is that you don't need to write the code, and that it is well tested. It does also have support for cancelling tasks

Start & Stop a ScheduledExecutorService in Java EE environment using servlet

主宰稳场 提交于 2019-12-17 20:51:05
问题 We have a requirement where in we need to monitor remote JVM details via JMX using a simple servlet application. So things done till now in a standalone application is 1) Creat a JMX connector & get the Memory data --> done 2) We need to constantly monitor & get the records (2.1 > which can be considered as scheduled task at constant delay & insert the records into DB Or 2.2> does the JMX gives the history if yes which MBean to access for the info?). Here I am planning to use an interface to

How to run certain task every day at a particular time using ScheduledExecutorService?

自闭症网瘾萝莉.ら 提交于 2019-12-17 02:34:10
问题 I am trying to run a certain task everyday at 5 AM in the morning. So I decided to use ScheduledExecutorService for this but so far I have seen examples which shows how to run task every few minutes. And I am not able to find any example which shows how to run a task every day at a particular time (5 AM) in the morning and also considering the fact of daylight saving time as well - Below is my code which will run every 15 minutes - public class ScheduledTaskExample { private final

How does the method scheduledExecutorService.scheduleAtFixedRate() ensures real-time execution?

混江龙づ霸主 提交于 2019-12-13 02:23:41
问题 Because I'm executing a time critical task every second, I compared several methods to find the best way to ensure that my task is really executed in fixed time steps. After calculating the standard derivation of the error for all methods it seems like using the method scheduledExecutorService.scheduleAtFixedRate() leads to the best results, but I don't have a clue why it is so. Does anybody know how that method internally works? How does it for example in comparison to a simple sleep()

What should I do in order to prevent the UI from freezing(scheduledexecutorservice)

坚强是说给别人听的谎言 提交于 2019-12-12 05:26:28
问题 I am using a scheduledexecutorservice to perform an animation for drawing a graph(one vertex and one edge at a time). I have a problem in updating the actual UI step by step and instead I can't see the actual animation but only the final graph. private Runnable newRunnable() { return new Runnable() { @Override public void run() { // this method just adds the graph to a JPanel displayDiagram(); } }; } private void animate() { executorService.schedule(newRunnable(), 1500, TimeUnit.MILLISECONDS)

How to schedule crawler4j crawl control to run periodically?

五迷三道 提交于 2019-12-12 01:48:26
问题 I'm using crawler4j to build a simple web crawler. What I want to do is to invoke the crawl control every 10 minutes. I created a servlet that starts when my Tomcat server starts, and in the servlet I am using ScheduledExecutorService for the scheduling. However, the crawl control only fetches me data ONCE (not every 10 mins like I wanted). Is there a better way to schedule my crawl to execute every 10 mins? Below is my code in the servlet. public class ScheduleControl extends HttpServlet {

after I press home button ScheduledExecutorService does not run in the background

别来无恙 提交于 2019-12-12 01:34:59
问题 I have created this in onCreate() and after I press home button ; ScheduledExecutorService does not run every 10 mins in the background.Why ? Where is the mistake. Please help me out . Thanks in Advance. ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleWithFixedDelay(new Runnable(){ public void run() { //play music to check if it executes every 10 mins mp3.start(); } }, 0, 600, TimeUnit.SECONDS); 回答1: I think that AlarmManager is more well