springboot应用启动时发生死锁

在实际开发过中遇到过2次,此问题隐藏的比较深。

main线程在启动时,做Ioc容器初始化操作,如果应用中涉及到大量的Bean初始化,这个过程比较耗时。

此时,你的应用有个地方在起了个线程,该线程执行了一些类似application.getBean(xxx)的操作,就有可能发送deadlock.

本质原因:IOC容器维护了两个map,分别是BeanDefinitionMap和SingletonObjectMap。前者是bean定义的元数据信息,bean的实例化是基于该元数据创建的。如果创建的时单例,则会放入singletonObjectMap中。

main线程的加锁顺序:先BeanDefinitionMap后SingletonObjectMap ;子线程是获取bean是先SingletonObjectMap后BeanDefinitionMap。这个过程会发生死锁。

示例:堆栈信息


Thread Name: main

Waited Count: 9 Waited Time: -1ms

Blocked Count: 332 Blocked Time: -1ms

Lock Name: java.lang.Object@34e911de Lock Class Name: java.lang.Object Lock Identity Hash Code: 887689694

Waiting for lock owned by 86 Owner Name: datacollect-1

Locked Monitors

Frame: 21 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . getSingleton  (DefaultSingletonBeanRegistry.java)

Frame: 35 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . getSingleton  (DefaultSingletonBeanRegistry.java)

Frame: 40 org.springframework.context.support.AbstractApplicationContext . refresh  (AbstractApplicationContext.java)

Stack Trace

org.springframework.amqp.rabbit.connection.CachingConnectionFactory . createConnection  (CachingConnectionFactory.java:626)

org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils . createConnection  (ConnectionFactoryUtils.java:240)

org.springframework.amqp.rabbit.core.RabbitTemplate . doExecute  (RabbitTemplate.java:1816)

org.springframework.amqp.rabbit.core.RabbitTemplate . execute  (RabbitTemplate.java:1790)

org.springframework.amqp.rabbit.core.RabbitTemplate . execute  (RabbitTemplate.java:1771)

cn.caijiajia.magic.realtimecompute.connector.rabbit.bind.AbstractRabbitExchangeBind . queueBind  (AbstractRabbitExchangeBind.java:70)

cn.caijiajia.magic.realtimecompute.connector.rabbit.bind.AsyncRabbitExchangeBind . synExchange  (AsyncRabbitExchangeBind.java:76)

cn.caijiajia.magic.realtimecompute.connector.rabbit.bind.AsyncRabbitExchangeBind . asyncInitRabbit  (AsyncRabbitExchangeBind.java:68)

sun.reflect.NativeMethodAccessorImpl . invoke0  (NativeMethodAccessorImpl.java) (Native)

sun.reflect.NativeMethodAccessorImpl . invoke  (NativeMethodAccessorImpl.java:62)

sun.reflect.DelegatingMethodAccessorImpl . invoke  (DelegatingMethodAccessorImpl.java:43)

java.lang.reflect.Method . invoke  (Method.java:498)

org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement . invoke  (InitDestroyAnnotationBeanPostProcessor.java:366)

org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata . invokeInitMethods  (InitDestroyAnnotationBeanPostProcessor.java:309)

org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor . postProcessBeforeInitialization  (InitDestroyAnnotationBeanPostProcessor.java:136)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . applyBeanPostProcessorsBeforeInitialization  (AbstractAutowireCapableBeanFactory.java:416)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . initializeBean  (AbstractAutowireCapableBeanFactory.java:1686)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . doCreateBean  (AbstractAutowireCapableBeanFactory.java:573)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . createBean  (AbstractAutowireCapableBeanFactory.java:495)

org.springframework.beans.factory.support.AbstractBeanFactory . lambda$doGetBean$0  (AbstractBeanFactory.java:317)

org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$168/1906808037 . getObject  ()

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . getSingleton  (DefaultSingletonBeanRegistry.java:222)

org.springframework.beans.factory.support.AbstractBeanFactory . doGetBean  (AbstractBeanFactory.java:315)

org.springframework.beans.factory.support.AbstractBeanFactory . getBean  (AbstractBeanFactory.java:204)

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor . autowireResource  (CommonAnnotationBeanPostProcessor.java:514)

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor . getResource  (CommonAnnotationBeanPostProcessor.java:485)

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement . getResourceToInject  (CommonAnnotationBeanPostProcessor.java:619)

org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement . inject  (InjectionMetadata.java:180)

org.springframework.beans.factory.annotation.InjectionMetadata . inject  (InjectionMetadata.java:90)

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor . postProcessPropertyValues  (CommonAnnotationBeanPostProcessor.java:318)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . populateBean  (AbstractAutowireCapableBeanFactory.java:1336)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . doCreateBean  (AbstractAutowireCapableBeanFactory.java:572)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory . createBean  (AbstractAutowireCapableBeanFactory.java:495)

org.springframework.beans.factory.support.AbstractBeanFactory . lambda$doGetBean$0  (AbstractBeanFactory.java:317)

org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$168/1906808037 . getObject  ()

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . getSingleton  (DefaultSingletonBeanRegistry.java:222)

org.springframework.beans.factory.support.AbstractBeanFactory . doGetBean  (AbstractBeanFactory.java:315)

org.springframework.beans.factory.support.AbstractBeanFactory . getBean  (AbstractBeanFactory.java:199)

org.springframework.beans.factory.support.DefaultListableBeanFactory . preInstantiateSingletons  (DefaultListableBeanFactory.java:759)

org.springframework.context.support.AbstractApplicationContext . finishBeanFactoryInitialization  (AbstractApplicationContext.java:867)

org.springframework.context.support.AbstractApplicationContext . refresh  (AbstractApplicationContext.java:548)

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext . refresh  (ServletWebServerApplicationContext.java:140)

org.springframework.boot.SpringApplication . refresh  (SpringApplication.java:754)

org.springframework.boot.SpringApplication . refreshContext  (SpringApplication.java:386)

org.springframework.boot.SpringApplication . run  (SpringApplication.java:307)

org.springframework.boot.SpringApplication . run  (SpringApplication.java:1242)

org.springframework.boot.SpringApplication . run  (SpringApplication.java:1230)

cn.caijiajia.magic.realtimecompute.RealtimeApplication . main  (RealtimeApplication.java:31)

sun.reflect.NativeMethodAccessorImpl . invoke0  (NativeMethodAccessorImpl.java) (Native)

sun.reflect.NativeMethodAccessorImpl . invoke  (NativeMethodAccessorImpl.java:62)

sun.reflect.DelegatingMethodAccessorImpl . invoke  (DelegatingMethodAccessorImpl.java:43)

java.lang.reflect.Method . invoke  (Method.java:498)

org.springframework.boot.loader.MainMethodRunner . run  (MainMethodRunner.java:48)

org.springframework.boot.loader.Launcher . launch  (Launcher.java:87)

org.springframework.boot.loader.Launcher . launch  (Launcher.java:50)

org.springframework.boot.loader.JarLauncher . main  (JarLauncher.java:51)

--------------------------------------------------------------------------------

Thread Name: datacollect-1

Waited Count: 3 Waited Time: -1ms

Blocked Count: 13 Blocked Time: -1ms

Lock Name: java.util.concurrent.ConcurrentHashMap@1b841c8 Lock Class Name: java.util.concurrent.ConcurrentHashMap Lock Identity Hash Code: 28852680

Waiting for lock owned by 1 Owner Name: main

Locked Synchronizers

Class Name: java.util.concurrent.ThreadPoolExecutor$Worker Identity Hash Code: 1810349933

Locked Monitors

Frame: 15 org.springframework.amqp.rabbit.connection.CachingConnectionFactory . createConnection  (CachingConnectionFactory.java)

Stack Trace

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . getSingleton  (DefaultSingletonBeanRegistry.java:179)

org.springframework.beans.factory.support.AbstractBeanFactory . isTypeMatch  (AbstractBeanFactory.java:490)

org.springframework.beans.factory.support.DefaultListableBeanFactory . doGetBeanNamesForType  (DefaultListableBeanFactory.java:426)

org.springframework.beans.factory.support.DefaultListableBeanFactory . getBeanNamesForType  (DefaultListableBeanFactory.java:397)

org.springframework.beans.factory.support.DefaultListableBeanFactory . getBeansOfType  (DefaultListableBeanFactory.java:510)

org.springframework.beans.factory.support.DefaultListableBeanFactory . getBeansOfType  (DefaultListableBeanFactory.java:502)

org.springframework.context.support.AbstractApplicationContext . getBeansOfType  (AbstractApplicationContext.java:1196)

org.springframework.amqp.rabbit.core.RabbitAdmin . initialize  (RabbitAdmin.java:485)

org.springframework.amqp.rabbit.core.RabbitAdmin . lambda$null$9  (RabbitAdmin.java:453)

org.springframework.amqp.rabbit.core.RabbitAdmin$$Lambda$594/1637332799 . doWithRetry  ()

org.springframework.retry.support.RetryTemplate . doExecute  (RetryTemplate.java:287)

org.springframework.retry.support.RetryTemplate . execute  (RetryTemplate.java:164)

org.springframework.amqp.rabbit.core.RabbitAdmin . lambda$afterPropertiesSet$10  (RabbitAdmin.java:452)

org.springframework.amqp.rabbit.core.RabbitAdmin$$Lambda$591/256139608 . onCreate  ()

org.springframework.amqp.rabbit.connection.CompositeConnectionListener . onCreate  (CompositeConnectionListener.java:36)

org.springframework.amqp.rabbit.connection.CachingConnectionFactory . createConnection  (CachingConnectionFactory.java:634)

org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils . createConnection  (ConnectionFactoryUtils.java:240)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . doExecute  (CjjRabbitTemplate.java:1467)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . access$300  (CjjRabbitTemplate.java:121)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate$3 . doWithRetry  (CjjRabbitTemplate.java:1420)

org.springframework.retry.support.RetryTemplate . doExecute  (RetryTemplate.java:287)

org.springframework.retry.support.RetryTemplate . execute  (RetryTemplate.java:180)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . execute  (CjjRabbitTemplate.java:1411)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . sendme  (CjjRabbitTemplate.java:535)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . sendTwice  (CjjRabbitTemplate.java:527)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . send  (CjjRabbitTemplate.java:513)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . convertAndSend  (CjjRabbitTemplate.java:628)

cn.caijiajia.rabbitmq.client.CjjRabbitTemplate . convertAndSend  (CjjRabbitTemplate.java:623)

cn.caijiajia.traceplus.starter.collector.reporter.RabbitmqReporter . report  (RabbitmqReporter.java:46)

cn.caijiajia.traceplus.starter.collector.AbstractDataCollector . syncReport  (AbstractDataCollector.java:143)

cn.caijiajia.traceplus.starter.collector.AbstractDataCollector . lambda$asyncReport$0  (AbstractDataCollector.java:120)

cn.caijiajia.traceplus.starter.collector.AbstractDataCollector$$Lambda$582/962927234 . run  ()

java.util.concurrent.Executors$RunnableAdapter . call  (Executors.java:511)

java.util.concurrent.FutureTask . run  (FutureTask.java:266)

java.util.concurrent.ThreadPoolExecutor . runWorker  (ThreadPoolExecutor.java:1149)

java.util.concurrent.ThreadPoolExecutor$Worker . run  (ThreadPoolExecutor.java:624)

java.lang.Thread . run  (Thread.java:748)



贴一个堆栈信息:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342