美文网首页
springboot应用启动时发生死锁

springboot应用启动时发生死锁

作者: 不务正业的coder | 来源:发表于2020-07-23 11:53 被阅读0次

    在实际开发过中遇到过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)

    贴一个堆栈信息:

    相关文章

      网友评论

          本文标题:springboot应用启动时发生死锁

          本文链接:https://www.haomeiwen.com/subject/dnwclktx.html