Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use queues and daemon worker threads that are always running and wait for something to arrive in the queue. This way it is guaranteed, that only one worker is working on a request. If you only want one thread to run, turn POOLSIZE down to 1, or use newSingleThreadExecutor. </p> <p>I do not quite understand your second requirement: do you mean only 1 thread is allowed to run as background task? If so, you could create another SingleThreadExecutor and use that for the background task. Then it would not make too much sense to have POOLSIZE>1, unless the work done in the background thread is very short compared to that done in the worker itself.</p> <pre><code>private static interface Request {}; private final int POOLSIZE = 10; private final int QUEUESIZE = 1000; BlockingQueue&lt;Request&gt; e = new LinkedBlockingQueue&lt;Request&gt;(QUEUESIZE); public void startWorkers() { ExecutorService threadPool = Executors.newFixedThreadPool(POOLSIZE); for(int i=0; i&lt;POOLSIZE; i++) { threadPool.execute(new Runnable() { @Override public void run() { try { final Request request = e.take(); doStuffWithRequest(request); } catch (InterruptedException e) { // LOG // Shutdown worker thread. } } }); } } public void handleRequest(Request request) { if(!e.offer(request)) { //Cancel request, queue is full; } } </code></pre> <p>At startup-time, startworkers starts the workers (surprise!). handleRequest handles requests coming from a webservice, servlet or whatever.</p> <p>Of course you need to adapt "Request" and "doStuffWithRequest" to your need, and add some additional logic for shutdown etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload