简体   繁体   中英

Java ScheduledExecutorService producer\consumer

I have the next project:

Spring(3.2)-based Web application(Tomcat 7), in background I have several tasks.

I have a queue with some information for processing. This queue is updating periodically(but just when it empty).

Also I have several threads that enqueue periodically data from this queue and process.

For scheduling I wanted to use ScheduledExecutorService.

I have several problems\\questions:

  1. How to keep this queue? As I think it should be global. Should I make it static in some "holder" class? Won't be this a poor desing?

  2. Where will be the good place for initializing this queue and all the task with ScheduledExecutorService? Is ServletContextLoadingListener a good place for it? Are there any ways to init this with Spring?

  3. Should I use several ScheduledExecutorService instances if I need to control the number of consumer threads exactly?

  4. Will ArrayBlockingQueue be good for this case?

  1. You can use an ordinary class and let spring manage it with the scope singleton .
  2. You can configure the bean with an init-method in spring configuration or implement the InitializingBean interface in your class.
  3. The thread number for ScheduledExecutorService is configurable via the constructor arguments. If you have tasks of several types and want to run them in separate thread pools, you can use multiple ScheduledExecutorService instances. However, if all your task are of the same type, I don't see the need for multiple ScheduledExecutorService instances.
  4. The ScheduledThreadPoolExecutor provided within JDK has an internal working queue of class java.util.concurrent.ScheduledThreadPoolExecutor.DelayedWorkQueue.DelayedWorkQueue (and it is not configurable). I am not sure where do you put the ArrayBlockingQueue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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