0

In my code, I have an executor service running:

//these are global variables
def newXmlParser = new XmlParser()
def processedResponse
Someloop.each{
    URL->
    executorService.execute(new Runnable() {
                public void run() {
                    someURLService(URL)
                }
            });
}

where

public void someURLService(URL){
    def urlConnection = HttpURLConnection(URL)
    def response = urlConnection.getInputStream()    
    parseResponse(response)
}
public synchronized void parseResponse(response){
    //previously declared XmlParser
     processedResponse = newXmlParser.parse(response)
}

I am trying to implement this, because I have code dependent on the results of all finished tasks of the ExecutorService:

How to wait for all threads to finish, using ExecutorService?

Someloop.each{
    URL->
    executorService.execute(new Runnable() {
                public void run() {
                    someURLService(URL)
                }
            });
}
executorService.shutdown();
try {
  executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
  ...
}

However, it doesn't wait until it's finished, but bypasses the 'awaitTermination' with an InterruptedException. It doesn't give me any message, localized message, or cause.

Does anybody have a solution to this problem? Let me know if you need more details.

Community
  • 1
  • 1
James.Wyst
  • 859
  • 3
  • 8
  • 13
  • 1
    if you got an InterruptedException, that means that your "main" thread was interrupted. you need to figure out what that is happening. – jtahlborn Sep 16 '15 at 17:03
  • @jtahlborn If you look at the answer I'm trying to implement, the InterruptedException is happening at executorService.awaitTermination. I'm not sure why that's happening. – James.Wyst Sep 16 '15 at 17:07
  • In given example question, the timeunit used is nanoseconds. It may happen in your case that your code is taking longer time than that. Try with some higher TimeUnit. Check how long it is taking to parse all the someloop elements. – Sandeep Poonia Sep 16 '15 at 17:13
  • @SandeepPoonia I don't think that should be the case--Long.MAX_VALUE is (2^63-1) and the TimeUnit is in Nanoseconds--this takes about five minutes. – James.Wyst Sep 16 '15 at 17:19
  • 1
    please read my answer again. the _only_ reason that awaitTermination throws InterruptedException, is if the _calling thread is interrupted_. – jtahlborn Sep 16 '15 at 18:09
  • @jtahlborn I don't know much about multithreading--what is the calling thread in this case? – James.Wyst Sep 16 '15 at 22:42
  • 1
    the thread which is making the call to `awaitTermination()`. – jtahlborn Sep 17 '15 at 00:42

0 Answers0