Javamail中的Threadsafety [英] Threadsafety in Javamail

查看:100
本文介绍了Javamail中的Threadsafety的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究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.

多个线程可以使用会话。

Multiple threads can use a Session.

由于传输表示与邮件服务器的连接,并且一次只有一个线程可以使用该连接,因此传输将同步来自多个线程的访问权限保持线程安全,但你真的只想在一个线程中使用它。

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.

同样,一个商店可以被多个线程使用,但访问底层连接将被同步并且是单线程的。

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...

这篇关于Javamail中的Threadsafety的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆