问题
I read all existing topics but I didn't find any solution to my problem. I monitor my glassfish server with VisualVM and I have noticed some strange behaviour. Here is screenshot:

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 new thread is created every 20 minutes (next one, Thread-38 will be created and then Thread-39 and so on). These threads are never finished. I am using newSingleThreadExecutor() from class Executors which is scheduled with scheduleWithFixedDelay() with 100 ms delay, other code is just DB read/write (so nothing special that would create new waiting threads)... Does someone have any idea what may 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 even no applications are deployed to server. Did anybody notice similar problems? I also noticed that all newly threads are waiting for same ID (in this example <3cb9965d>)...
回答1:
Maybe your observation reflects the behaviour of the thread-pool...
If a thread has been idle for a certain time in the thread-pool it will be removed from the thread-pool.
Depending on your settings in glassfish's domain.xml a new thread will be created to meet the minimum amount of threads in the pool. Please note that glassfish uses its own default values if you do not specify them in the domain.xml-file.
The threads have all the same ID because they belong to the same thread-pool.
To verify that you can add the attribute idle-thread-timeout-seconds="300" to the http-thread-pool in the domain.xml and check it again with VisualVM.
<thread-pool idle-thread-timeout-seconds="300" name="http-thread-pool"></thread-pool>
The above sets the timeout for idle threads to 5 minutes. Maybe you find "idle-thread-timeout-seconds="1200" somewhere in your domain.xml, this would explain the 20 minutes intervall you observed...
Further Explanation:
If you take a look at the domain.xml you might find following entries (yours might be 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>
[...]
As you can see there are no attributes specified for the thread-pool "http-thread-pool". This means that galssfish will use its own default values.
The default values for the "http-thread-pool" in my glassfish installation (4.1) have follwoing values*:
- Max Queue Size: 4096
- Max Thread Pool Size: 5
- Min Thread Pool Size: 5
- Idle Thread Timeout: 900 (seconds)
In this case, if a thread has been idle for 15 minutes, it will be removed from the thread pool. Due to the value of "Min Thread Pool Size = 5" a new thread will be created if the number of threads goes below five.
*You can go to the Admin console of glassfish and open "Configurations -> server-config -> Thread pools" to find out your default values.
来源:https://stackoverflow.com/questions/30030271/new-thread-every-n-minutes-java-lang-thread-state-waiting-at-sun-misc-unsafe-p