开发者

Use concurrent collection in EJB program

Can I use concurrent collection (java.util.concurrent) in EJB program? Because there are many constrains in managed-environment. Perhaps EJB container manages all threads control, thus开发者_运维百科 java.util.concurrent will violate the EJB environment.

Do I understand correctly? If so, thread related operations like sleep(), synchronized cannot used in EJB program either.

For example, BlockingQueue causes thread suspended in an EJB session bean.


It depends on the circumstances, but in general there is nothing why you couldn't use concurrent collections in an EJB program.

The EJB container manages (among others) request threads, that is, it takes care of allocating each incoming request to a separate thread (taken from a thread pool). However, concurrent collections do not interfere with this in any way: they don't create or manipulate threads internally, just ensure that the data stored inside them can safely be accessed and modified by multiple threads in parallel.

Note that you only need to use concurrent collections when their contents are accessed/modified from multiple threads. If your data is confined to a single thread (request), you most probably don't need to be concerned about thread safety.

About sleep() and synchronized you should understand that these are on a different (lower) level of abstraction than the concurrent collections. You don't need to use any of these directly when using concurrent collections. (Some - but not all - of the collections use synchronized blocks internally in their implementation. About sleep(), my bet is it's probably not used in any concurrent collection).

Without more concrete details on what you would like to achieve, it is hard to tell more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜