New thread every n minutes (java.lang.Thread.State: WAIT on sun.misc.Unsafe.park (native method))

I have read all the existing threads, but I have not found a solution for my problem. I am monitoring my server in Glassfish using VisualVM and I noticed some strange behavior. Here's a screenshot:

enter image description here

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)

      

As you can see, a new thread is created every 20 minutes (the next one will be created by Thread-38, then Thread-39, etc.). These streams never end. I am using newSingleThreadExecutor () from the Executors class which is scheduled with a scheduleWithFixedDelay () with 100ms delay, the other code is just DB read / write (so nothing fancy that would create new waiting threads) ... Someone knows that could be causing this problem?

ScheduledExecutorService service = service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                //do something...
            }
        }, 1, readInterval, TimeUnit.MILLISECONDS);

      

EDIT: New threads are created every 20 minutes, not even applications are deployed to the server. Has anyone noticed similar problems? I also noticed that all new threads are waiting for the same ID (<3cb9965d> in this example) ...

+3


source to share


1 answer


Perhaps your observation reflects the behavior of the thread pool ...

If a thread has not been used in the thread pool for some time , it will be removed from the thread pool .

Depending on your settings, a new thread will be created in glassfish domain.xml to satisfy the minimum number of threads in the pool. Please note that Glassfish uses its own defaults unless you specify them in the domain.xml file.

The threads have the same ID because they belong to the same thread pool .

To make sure you can add idle-thread-timeout-seconds = "300" attribute to the http-thread-pool in domain.xml and check it again with VisualVM.

 <thread-pool idle-thread-timeout-seconds="300" name="http-thread-pool"></thread-pool>
      

Run codeHide result


The above sets a timeout for idle threads up to 5 minutes. You might find "idle-thread-timeout-seconds =" 1200 "somewhere in your .xml domain, this explains the 20 minute interval you observed ...



Further explanation:
If you look at domain.xml you can find the following entries (you may have different ...):

   [...]

<!-- HTTP -->
<network-listener protocol="http-listener-1" port="8080" 
    name="http-listener-1" thread-pool="http-thread-pool" 
    transport="tcp"></network-listener>
<!--HTTPS -->
<network-listener protocol="http-listener-2" port="8181" 
   name="http-listener-2" thread-pool="http-thread-pool"
   transport="tcp"></network-listener>
   [...]

<!--Threadpools wich can be used by network-listeners-->
<thread-pools>
    <thread-pool name="admin-thread-pool" max-queue-size="256" max-thread-pool-size="50"></thread-pool>
    <thread-pool name="http-thread-pool"></thread-pool>
    <thread-pool name="thread-pool-1" max-thread-pool-size="200"></thread-pool>
  </thread-pools>

[...]
      

Run codeHide result


As you can see, there are no attributes set for the "http-thread-pool" thread pool . This means galssfish will use its own defaults.

The default values ​​for "http-thread-pool" in my Glassfish (4.1) installation have the following values ​​*:

  • Maximum queue size: 4096
  • Maximum pool size: 5
  • Min Thread Pool Size: 5
  • Idle Time Timeout: 900 (seconds)

In this case, if a thread is idle for 15 minutes, it will be removed from the thread pool. Due to the value "Min Thread Pool Size = 5" a new thread will be created if the number of threads is less than five.

* You can go to glassfish admin console and open "Configs -> server-config -> Thread Pools" to find out your defaults.

+1


source







All Articles