spring 启动计划任务挂起
spring boot scheduled task get hanging
我使用 spring 引导和 @Scheduled 有一段时间了,但最近我发现有一个危险的潜在威胁,如下所述:
我多次发现作为应用程序 运行s 和计划任务 运行,有许多线程仍在等待但未完成,这在 'kill -3' 的线程堆栈跟踪中显示。为了清除任何可能导致这个问题的东西,我做了一个完全虚拟的任务:
@Component
public class TestJob
{
/**
* LOGGER
*/
private static Logger log = LogManager.getLogger(TestJob.class);
@Scheduled(fixedDelay = 60000, initialDelay = 1000)
public void test()
{
log.info("---------------[{}]", Thread.currentThread().getId());
}
}
这是我的日志:
20151102 11:54:50.660 | INFO | pool-3-thread-2 | ---------------[26] | TestJob.test(TestJob.java:19)
20151102 11:55:50.662 | INFO | pool-3-thread-4 | ---------------[28] | TestJob.test(TestJob.java:19)
20151102 11:56:50.664 | INFO | pool-3-thread-5 | ---------------[33] | TestJob.test(TestJob.java:19)
20151102 11:57:50.666 | INFO | pool-3-thread-6 | ---------------[37] | TestJob.test(TestJob.java:19)
线程堆栈跟踪:
"pool-3-thread-2" #26 prio=5 os_prio=0 tid=0x00007fbea0cd9800 nid=0x74f2 waiting on condition [0x00007fbf0d3d2000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000763ed3710> (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)
"pool-3-thread-4" #28 prio=5 os_prio=0 tid=0x00007fbea0783800 nid=0x74f4 waiting on condition [0x00007fbf0d1d0000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000763ed3710> (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)
计划的 javadoc 说
Processing of @Scheduled annotations is performed by registering a ScheduledAnnotationBeanPostProcessor.
我自己没有命名这个 class,只是用 @EnableScheduling 注释了 main class.
有人知道如何解决这个问题吗?
更新:
我附上一张Eclipse调试截屏的图片,池在增加,所有旧线程都在运行宁...希望它能澄清我的问题。
更新:
我想这次我做对了。 spring 启动调度程序的默认池大小为 100,所有线程都处于 运行ning 状态。我真的不明白,运行宁在什么?我认为应该等待某事,为什么不呢?
有谁知道如何配置 spring boot scheduled pool size with annotation?我没有在我的应用程序中使用 xml,也不愿意只为调度程序引入它。
我在您的示例中看到了绝对正常的行为。
您有计划任务,它每分钟由线程池中的不同线程执行。线程池中的线程处于线程池预期的等待(停放)状态。
如果你想减少线程数,你可以配置:
http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/htmlsingle/#scheduling
<task:scheduler id="scheduler" pool-size="2"/>
我使用 spring 引导和 @Scheduled 有一段时间了,但最近我发现有一个危险的潜在威胁,如下所述: 我多次发现作为应用程序 运行s 和计划任务 运行,有许多线程仍在等待但未完成,这在 'kill -3' 的线程堆栈跟踪中显示。为了清除任何可能导致这个问题的东西,我做了一个完全虚拟的任务:
@Component
public class TestJob
{
/**
* LOGGER
*/
private static Logger log = LogManager.getLogger(TestJob.class);
@Scheduled(fixedDelay = 60000, initialDelay = 1000)
public void test()
{
log.info("---------------[{}]", Thread.currentThread().getId());
}
}
这是我的日志:
20151102 11:54:50.660 | INFO | pool-3-thread-2 | ---------------[26] | TestJob.test(TestJob.java:19) 20151102 11:55:50.662 | INFO | pool-3-thread-4 | ---------------[28] | TestJob.test(TestJob.java:19) 20151102 11:56:50.664 | INFO | pool-3-thread-5 | ---------------[33] | TestJob.test(TestJob.java:19) 20151102 11:57:50.666 | INFO | pool-3-thread-6 | ---------------[37] | TestJob.test(TestJob.java:19)
线程堆栈跟踪:
"pool-3-thread-2" #26 prio=5 os_prio=0 tid=0x00007fbea0cd9800 nid=0x74f2 waiting on condition [0x00007fbf0d3d2000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x0000000763ed3710> (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)
"pool-3-thread-4" #28 prio=5 os_prio=0 tid=0x00007fbea0783800 nid=0x74f4 waiting on condition [0x00007fbf0d1d0000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x0000000763ed3710> (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)
计划的 javadoc 说
Processing of @Scheduled annotations is performed by registering a ScheduledAnnotationBeanPostProcessor.
我自己没有命名这个 class,只是用 @EnableScheduling 注释了 main class.
有人知道如何解决这个问题吗?
更新:
我附上一张Eclipse调试截屏的图片,池在增加,所有旧线程都在运行宁...希望它能澄清我的问题。
更新: 我想这次我做对了。 spring 启动调度程序的默认池大小为 100,所有线程都处于 运行ning 状态。我真的不明白,运行宁在什么?我认为应该等待某事,为什么不呢? 有谁知道如何配置 spring boot scheduled pool size with annotation?我没有在我的应用程序中使用 xml,也不愿意只为调度程序引入它。
我在您的示例中看到了绝对正常的行为。 您有计划任务,它每分钟由线程池中的不同线程执行。线程池中的线程处于线程池预期的等待(停放)状态。 如果你想减少线程数,你可以配置: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/htmlsingle/#scheduling
<task:scheduler id="scheduler" pool-size="2"/>