Simultaneously processing in one Servlet

淺唱寂寞╮ 提交于 2020-01-04 14:31:52

问题


I have a Servlet which recieves a request, has to process 5 tasks (Grab data Form external Servers) and send all the data back to the client ordered.

How to process the 5 Task simultaneously and continue with the servlet code after all 5 tasks are completed?


回答1:


You can use CoundDownLatch

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

sample code:

    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(5); // 5 tasks

    class Worker implements Runnable {
        private final CountDownLatch startSignal;
        private final CountDownLatch doneSignal;
        private final int threadNumber;

        // you can pass additional arguments as well
        Worker(CountDownLatch startSignal, CountDownLatch doneSignal,
                                                   int threadNumber) {
            this.startSignal = startSignal;
            this.doneSignal = doneSignal;
            this.threadNumber = threadNumber;
        }

        public void run() {
            try {
                startSignal.await();
                doWork(); // actual work to be performed here    
                doneSignal.countDown();
            } catch (InterruptedException ex) {
                LOGGER.error(ex);
            }
        }
    }

    // 5 new threads are started
    for(int i=1;i<=5;i++){
        new Thread(new Worker(startSignal, doneSignal, i)).start();
    }

    startSignal.countDown(); // let all threads proceed
    try {
        doneSignal.await(); // wait for all to finish
        // all 5 tasks are finished and do whatever you want to do next
    } catch (InterruptedException interruptedException) {
        LOGGER.error(interruptedException);
    }

Read more... and Find more examples...




回答2:


Another option is the ExecutorService. There are a variety of examples available including the following:

  • How to wait for all threads to finish, using ExecutorService?
  • ExecutorService, how to wait for all tasks to finish

Here is some example code taken from the first link found above:

ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
  taskExecutor.execute(new MyTask());
}
taskExecutor.shutdown();
try {
  taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
  ...
}



回答3:


5 seconds is too long, it might take up the web server resource, you can let the client to send a request A, which trigger your 5 seconds task, but it don't wait, return immediately.Since you know it will take 5 seconds, you can send another request B, 5 seconds later, to get the data. If data not ready yet, you can request after another 5 seconds, until MAX_RETRY_COUNT (defined by yourself) reached.



来源:https://stackoverflow.com/questions/24981726/simultaneously-processing-in-one-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!