Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between newScheduledThreadPool(1) and newSingleThreadScheduledExecutor()

Tags:

java

I am wondering what is the difference between these two methods of Executors class? I have a web application where I'm checking some data every 100 ms so that's why I'm using this scheduler with scheduleWithFixedDelay method. I want to know which method should I use in this case (newScheduledThreadPool or newSingleThreadScheduledExecutor)? I also have one more question - in VisualVM where I monitor my Glassfish server I noticed that I have some threads in PARK state - for example:

java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <3cb9965d> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Is it possible that these threads are connected with scheduler because I don't have any idea what else would create them? These threads are never destroyed, so I am afraid that this could cause some troubles. Here is a screenshot (new Thread-35 will be created in 15minutes and so on...):

enter image description here

like image 309
user4341206 Avatar asked May 04 '15 11:05

user4341206


2 Answers

newSingleThreadScheduledExecuto() is wrapped by a delegate, as you can see in Executors.java:

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
}

Differences (from javadoc):

if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

Unlike the otherwise equivalent {@code newScheduledThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads.

reply to your comment:

do this also apply for newScheduledThreadPool(1) or not?

no, you need to take care of thread failure yourself.

As for Unsafe.park(), see this.

like image 171
wings Avatar answered Oct 23 '22 17:10

wings


As documentation states:

Unlike the otherwise equivalent newScheduledThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

So when using newScheduledThreadPool(1)you will be able to add more threads later.

like image 30
Tagir Valeev Avatar answered Oct 23 '22 18:10

Tagir Valeev