开发者

http request and thread pool

I have this problem. In order to create a response for a single http request, I have to do several tasks that can be performed concurrently. I can perform those tasks in a Executor (thread pool) but then I have to worry about concurrency (synchronization) in my web app. Is there a better way to 开发者_StackOverflowsolve this problem without using thread pool?

Thanks,


Using an Executor takes care of the messy details of thread synchronization, so it should be the easiest way to perform multiple tasks concurrently. You just submit jobs to the executor and wait for them to finish.

If these background tasks access some shared resource (especially in-memory data structures), then this access needs to be coordinated. The easiest way is to avoid shared resources, but if you can do that depends on what you actually need to do. If you must have shared resources, you can use Java synchronization primitives, or some other utilities from the concurrency package.

You cannot have multiple threads and not think about synchronization. No silver bullet here.

(By the way, even without multiple threads for a single web request, you probably have to consider threading issues in a web application anyway, because the same web server can be hit by multiple requests simultaneously).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜