简体   繁体   English

Spring 引导应用程序的线程安全

[英]Thread safety with Spring boot application

Hi I was wondering about thread safety in spring boot applications.您好,我想知道 spring 启动应用程序中的线程安全性。

I have configured JMS listener that reads messages concurrently and then I call a mapper to convert the request into a desirable format and then send message.我已经配置了 JMS 侦听器,它可以同时读取消息,然后调用映射器将请求转换为所需的格式,然后发送消息。

So, Coming to mapper now that I am listening messages concurrently do I need to ensure thread safety in my code?所以,既然我正在同时收听消息,那么现在使用 mapper 是否需要确保我的代码中的线程安全?

Application servers use several concepts to make multithreading easy for the average programmer.应用程序服务器使用几个概念使普通程序员可以轻松地使用多线程。

The most important concept is thead confinement.最重要的概念是 thead 限制。 To understand the concept, read the answers to this question .要理解这个概念,请阅读此问题的答案。

In your situation, there is more than one thread involved, but there is also some API that enables you to handle data from one thread to the other in a safe way.在您的情况下,涉及多个线程,但也有一些 API 使您能够以安全的方式处理从一个线程到另一个线程的数据。 Most of the time there are thread pools envolved, see Introduction to Thread Pools in Java .大多数时候都会涉及到线程池,参见 Java 中的线程池简介

In JMS there are some objects that are not allowed to be used in different threads (eg the Session object), some are (eg the Connection, the JmsTemplate).在JMS 中有一些对象不允许在不同的线程中使用(例如Session 对象),有些是(例如Connection、JmsTemplate)。 Read the answers to "Why JMS Session Object is not thread safe?"阅读“为什么 JMS Session Object 不是线程安全的?”的答案。 . . The consequences are listed here . 此处列出了后果。

I recommend reading the Spring documentation on this topic .我建议阅读有关此主题的 Spring 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM