简体   繁体   中英

java ScheduledExecutorService task stops after several executions

I have a problem, I am trying to execute a Task in ScheduledExecutorService, and I am executing the task with the command:

updateTagDataHandle = scheduler.scheduleWithFixedDelay(updateTagDataRunnable, 500, 500, TimeUnit.MILLISECONDS);

and after several success runs it stops. the task itself takes a few seconds I checked with println that it go to the end of the task with no errors, and I print any exception and didnt see an exception in the end. I need it to continue run infinite number of times.

any help would be appreciated

edit

my code for initializing the task scheduler was:

scheduler = Executors.newScheduledThreadPool(1);

so the corePoolSize = 1 so there only one thread alive and the task share this on thread. but setting the threadPool to be more than one is not helping and still there seems to be only one thread active.

same question here: Executors Factory method newScheduledThreadPool always returns the same Thread pool

and here:

Why doesn't ScheduledExecutorService spawn threads as needed?

any help would be appreciated

edit:

didnt find a solution so used the scheduler custom thread creation :

scheduler = Executors.newScheduledThreadPool(7, new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {

            return new Thread(r);
        }
    });

Try changing the initial delay to 1000 milliseconds.
I was trying the same code in my android app and this solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM