scheduledexecutorservice

ScheduledExecutorService with dating format added to ActionListener

本小妞迷上赌 提交于 2021-02-10 18:34:49
问题 I understand that in Java, it is possible to use a ScheduledExecutorService to perform a specific task after a certain delay. This post shows how to execute the task at a specific date, but not with a SimpleDateFormat . For example, I have a format initialized like below: dateFormatter = new SimpleDateFormat("MM-dd-yyyy hh:mm aa"); Additionally, I want to execute a particular task declared under an else if statement in an ActionListener , such as below: foo.addActionListener(e -> { if

java ScheduleExecutorService timeout task

社会主义新天地 提交于 2021-01-28 21:34:38
问题 I have to schedule some tasks at some interval and have to kill the task which is taking more than specified time. code: public class ExecutorMain { public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { ScheduledThreadPoolExecutor scheduledExecutorService = new ScheduledThreadPoolExecutor(2); scheduledExecutorService.scheduleAtFixedRate(new ShortTask(2), 0, 3, TimeUnit.SECONDS); scheduledExecutorService.scheduleAtFixedRate(new LongTask(1),

executorService.shutdownNow() doesn't stop the thread

廉价感情. 提交于 2021-01-28 19:41:02
问题 I have a scheduled Executor service setup like class Test { private final ScheduledExecutorService executor; public Test() { executor = Executors.newSingleThreadScheduledExecutor((runnable) -> { Thread thread = new Thread(runnable, this.getClass().getName()); thread.setDaemon(true); return thread; }); executor.scheduleWithFixedDelay( new TestRefreshTask(), 0, 50000, TimeUnit.MILLISECONDS ); } private class TestRefreshTask implements Runnable { @Override public void run() { //refresh some data

How accurate is the task scheduling of a ScheduledThreadPoolExecutor

╄→尐↘猪︶ㄣ 提交于 2020-06-14 05:59:28
问题 I was reading ScheduledThreadPoolExecutor JavaDoc and came across the following thing: Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence . Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission. So, if I write something like this: ScheduledExecutorService ses = Executors.newScheduledThreadPool(4); //uses ScheduledThreadPoolExecutor

Unreliable ScheduledExecutorService in Service

自古美人都是妖i 提交于 2020-03-25 18:46:28
问题 I created a ScheduledExecutorService in my Service that shuold run a runnable regulary. During debuging all it´s working fine. But on the smartphone the runnable is executed much later and the Service crashes occasionaly. The runnable is scheduled every 8-30 minutes. What can I do to make the Service more reliable? Are there better ways to realize this plan? (before I was using Thread and ExecutorService with nearly the same results). Here my current code: public class MyService extends

Scheduled executor: poll for result at fix rate and exit if timeout or result valid

僤鯓⒐⒋嵵緔 提交于 2020-01-28 10:54:06
问题 Problem: I have a requirement to call a dao method at fix rate say every 10 sec, then I need to check if the result is valid if yes exit, else keep on calling that method every 10 sec till I get a valid result or defined time out (say 2 min) is over. Approaches: I want to keep the task and scheduler logic separate, and write a task in such a way that it can be used by different classes having similar requirement. One way I can think is to define a new poller task public abstract class

How to run a particular task every week, every month and every six month?

一笑奈何 提交于 2020-01-16 05:09:11
问题 I am working on making a cron job in Java. I want to run a particular task every week, month, three month, six month and nine month. public Interface interfaceA { public String abc() throws Exception; } public class TestTaskA implements interfaceA { @Override public String abc() throws Exception { // some code } } I am running it like this - TestTaskA testTaskA = new TestTaskA(); testTaskA.abc(); I want to run TestTaskA every week, every month, every three month, every six month, every nine

Why is java ExecutorService newSingleThreadExecutor spawning two threads?

淺唱寂寞╮ 提交于 2020-01-14 07:37:07
问题 I have a sample java code below which if run as a console application behaves as I expected ( spawning a single thread to execute the runnable). The strange behavior (spawning two threads - sample below) I see is when I run this sample as a service application using Apache's prunsrv64.exe. I am testing this on a Windows 7 machine - 64bit. Sample Output: Thread -28 Current time: 09:50:11 AM Thread -52 Current time: 09:50:12 AM Thread -28 Current time: 09:50:21 AM Thread -52 Current time: 09:50

Calling ExecutorService after some interval of time

无人久伴 提交于 2020-01-05 03:01:07
问题 Is their a way to schedule a thread pool using ExecutorService , in lines similar to thread.sleep() My current code looks something like Executors.newScheduledThreadPool(poolSize); public void run() { try { pool.execute(new Worker()); } But I want to call the run method, only after some time interval. Can someone let me know how to do this? 回答1: This can be achieved using ScheduledThreadPoolExecutor. Sample code pool = new ScheduledThreadPoolExecutor(10); pool.scheduleWithFixedDelay(new

ScheduledThreadPoolExecutor for a periodic task (using Retrofit) just firing once and never again

孤人 提交于 2019-12-28 18:15:49
问题 I have the following code for polling the unread notification count every X seconds from a server I start the this process via ScheduledThreadPoolExecutor in the App.onCreate() and Log.d("XXX", "Requesting Notification count from server ..."); is called once (i can see in Logcat), but neither of the two Retrofit call back functions getting called (and in fact no Retrofit debug logs). Morever, the "Requesting Notification count from server...." is never printed again (i.e. the periodic task is