简体   繁体   中英

Java - thread Pool

My scheduled thread pool--->

private static ScheduledExecutorService pendingCheckScheduler = Executors.newScheduledThreadPool(1);

Thread 1--->

private ScheduledFuture<?> pendingTranStatusCheckHandle=pendingCheckScheduler.scheduleAtFixedRate(new PendingTranStatusCheck(),0,checkForPendingTrans,TimeUnit.SECONDS );

Thread2--->

private ScheduledFuture<?> forcibleBatchCloseHandle=pendingCheckScheduler.schedule(new ForcibleBatchClose(), waitForPendingTrans, TimeUnit.MINUTES);

Thread 1 is executing every 10 seconds. Thread 2 is supposed to start after 30 minutes.

Thread 1 is behaving as expected where as Thread2 which is expected to start after 30 minutes,is starting after 1 hour.

Does latency in thread1 is contributing to this problem?If so, thread2 is supposed to be givcen priority when thread 1 finishes considering that we have only one thread in thread pool.Why Thread2 is stretched too long to start after 1 hour?

I am clueless and expect some pointers.Please help me.

It's because you have single thread in executor.

Executors.newScheduledThreadPool(1);

It causes second Runnable wait until first Runnable finish.

Try using two threads:

Executors.newScheduledThreadPool(2);

Your concepts are slightly off, which offsets your conclusions. You have one thread altogether, and two tasks scheduled to run on that one thread. So you can definitely expect interference if the repeated task executes for a long time, occupying the thread.

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