Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public class Consumer {
  2.  
  3.     private BlockingQueue<Item> items;
  4.  
  5.     private int blockingQueueSize;
  6.  
  7.     private int numOfThreads;
  8.  
  9.     private ExecutorService executor;
  10.  
  11.     @PostConstruct
  12.     public void init() {
  13.         items = new ArrayBlockingQueue<Item>(blockingQueueSize);
  14.         executor = Executors.newFixedThreadPool(numOfThreads);
  15.  
  16.         for (int i = 0; i < numOfThreads; i++) {
  17.             executor.execute(new Runnable() -> {
  18.  
  19.                 @Override
  20.                 public void run() {
  21.                     try {
  22.                         while(true) {
  23.                             Item item = items.take();
  24.                             try {
  25.                                 processItem(item);
  26.                             } catch(exception) { /// };
  27.                         }
  28.                     } catch(Exception e) {
  29.                         log.error(...);
  30.                     }
  31.                 }
  32.             });
  33.         }
  34.     }
  35.  
  36.     public void processItem(Item item) {
  37.         try {
  38.             items.put(item);
  39.         } catch(exception) {
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement