Javamail 中的线程安全
我正在研究 Javamail 是否是线程安全的,特别是在有许多会话对应不同用户、多个 SMTP 服务器以及使用创建 MIME 消息和使用 transport.sendMessage
方法的情况下.我知道 Javamail 是面向桌面使用的,这让我怀疑它在构建时可能没有考虑到线程,我想知道是否有人有这样的经验.
I'm researching whether Javamail is threadsafe, in particular in a situation with many sessions corresponding to different users, several SMTP servers and the use of creating MIME messages and use of transport.sendMessage
method. I know Javamail is oriented toward desktop-use which makes me suspect it may not have been built with threading in mind, and am wondering if anyone has such experience.
推荐答案
诚然,JavaMail 的线程安全规则没有很好的文档记录,但希望它们大多符合您的预期.
Admittedly the thread safety rules for JavaMail are not well documented, but hopefully they mostly match what you would expect.
多个线程可以使用一个 Session.
Multiple threads can use a Session.
由于 Transport 表示与邮件服务器的连接,并且一次只能有一个线程使用该连接,Transport 将同步来自多个线程的访问以维护线程安全,但您真的只想使用它来自一个线程.
Since a Transport represents a connection to a mail server, and only a single thread can use the connection at a time, a Transport will synchronize access from multiple threads to maintain thread safety, but you'll really only want to use it from a single thread.
同样,一个Store可以被多个线程使用,但是对底层连接的访问将是同步的并且是单线程的.
Similarly, a Store can be used by multiple threads, but access to the underlying connection will be synchronized and single threaded.
一条消息一次只能由一个线程修改,但多个线程应该能够安全地读取一条消息(尽管不清楚为什么要这样做).
A Message should only be modified by a single thread at a time, but multiple threads should be able to read a message safely (although it's not clear why you would want to do that).
希望对您有所帮助...
Hope that helps...
相关文章