[concurrency-interest] Callable inside a Runnable and RejectedExecutionException

robert lazarski robertlazarski at gmail.com
Wed Dec 13 15:20:57 EST 2006


Hi all, getting closer on my DNS problem. One of the things I'm
noticing is that on some Context.list() calls are taking too much time
to timeout. Unfortunately putting in a new dep such as dnsjava in this
project is a hard sell. Anyways What I'd like to do is limit the time
this  list() call can take via a Future. Is it illegal to have a
Callable inside Runnable? The code below throws on
exec.submit(listTask). Thanks for any help.

  private static void parallelRecursiveDNS(final ExecutorService exec,
final DirContext ictx,
          final NamingEnumeration hostEnumeration, final
Collection<String> domainsVisitedQueue,
          final Collection<String> results)
      throws Exception {

      taskCount.incrementAndGet();

      while (hostEnumeration.hasMore()) {

          exec.execute(new Runnable() {
              public void run() {
                  long runStart = System.currentTimeMillis();
                  try {
                      String host = null;
                      host = ((NameClassPair) hostEnumeration.next())
                              .getNameInNamespace();
                      if (results.contains(host)) {
                          return;
                      }
                      results.add(host);
                      out.println("Found host: " + host);
                      Attributes aDNS = ictx.getAttributes(host,
                                  new String[] { "NS" });
                      NamingEnumeration allDNS = aDNS.getAll();
                      while (allDNS.hasMore()) {
                          out.println("Entering allDNS: ");
                          Attribute attr = (Attribute) allDNS.next();
                          NamingEnumeration values = attr.getAll();
                          final String dns = values.next().toString();
                          if (domainsVisitedQueue.contains(dns)) {
                              continue;
                          }
                          domainsVisitedQueue.add(dns);

                          NamingEnumeration newEnumeration;
                          boolean gotList = false;
                          try {
                              Callable <NamingEnumeration> listTask =
                                  new Callable<NamingEnumeration>() {
                                      public NamingEnumeration call() {
                                          try {
                                              return ictx.list(dns);
                                          } catch (Exception ex) {
                                              ex.printStackTrace();
                                          }
                                          return null;
                                      }
                                  };
                              Future <NamingEnumeration> future =
exec.submit(listTask);
                              newEnumeration = future.get(10L,
TimeUnit.SECONDS);
                              if (hostEnumeration != null) {
                                  gotList = true;
                              }
                          } catch (Exception ex) {
                              ex.printStackTrace();
                              throw new Exception(ex);
                          }
                          if (!gotList) {
                              continue;
                          }
                          parallelRecursiveDNS(exec, ictx,
newEnumeration, domainsVisitedQueue, results);
                      }
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  } finally {
                      long runEnd = System.currentTimeMillis();
                      out.println("runnable execution time was "
                        + (runEnd - runStart) / 1000 + " seconds ");
                      if (taskCount.decrementAndGet() == 0) {
                      }
                  }

              }
          });
      }
  }


More information about the Concurrency-interest mailing list