Example usage for java.util.concurrent ExecutorService invokeAll

List of usage examples for java.util.concurrent ExecutorService invokeAll

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService invokeAll.

Prototype

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
        throws InterruptedException;

Source Link

Document

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.

Usage

From source file:org.apache.hadoop.contrib.bkjournal.TestBookKeeperJournalManager.java

/**
 * Tests that concurrent calls to format will still allow one to succeed.
 *///  w  w  w .  java2s  . c  om
@Test
public void testConcurrentFormat() throws Exception {
    final URI uri = BKJMUtil.createJournalURI("/hdfsjournal-concurrentformat");
    final NamespaceInfo nsi = newNSInfo();

    // populate with data first
    BookKeeperJournalManager bkjm = new BookKeeperJournalManager(conf, uri, nsi);
    bkjm.format(nsi);
    for (int i = 1; i < 100 * 2; i += 2) {
        bkjm.startLogSegment(i, NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
        bkjm.finalizeLogSegment(i, i + 1);
    }
    bkjm.close();

    final int numThreads = 40;
    List<Callable<ThreadStatus>> threads = new ArrayList<Callable<ThreadStatus>>();
    final CyclicBarrier barrier = new CyclicBarrier(numThreads);

    for (int i = 0; i < numThreads; i++) {
        threads.add(new Callable<ThreadStatus>() {
            public ThreadStatus call() {
                BookKeeperJournalManager bkjm = null;
                try {
                    bkjm = new BookKeeperJournalManager(conf, uri, nsi);
                    barrier.await();
                    bkjm.format(nsi);
                    return ThreadStatus.COMPLETED;
                } catch (IOException ioe) {
                    LOG.info("Exception formatting ", ioe);
                    return ThreadStatus.GOODEXCEPTION;
                } catch (InterruptedException ie) {
                    LOG.error("Interrupted. Something is broken", ie);
                    Thread.currentThread().interrupt();
                    return ThreadStatus.BADEXCEPTION;
                } catch (Exception e) {
                    LOG.error("Some other bad exception", e);
                    return ThreadStatus.BADEXCEPTION;
                } finally {
                    if (bkjm != null) {
                        try {
                            bkjm.close();
                        } catch (IOException ioe) {
                            LOG.error("Error closing journal manager", ioe);
                        }
                    }
                }
            }
        });
    }
    ExecutorService service = Executors.newFixedThreadPool(numThreads);
    List<Future<ThreadStatus>> statuses = service.invokeAll(threads, 60, TimeUnit.SECONDS);
    int numCompleted = 0;
    for (Future<ThreadStatus> s : statuses) {
        assertTrue(s.isDone());
        assertTrue("Thread threw invalid exception",
                s.get() == ThreadStatus.COMPLETED || s.get() == ThreadStatus.GOODEXCEPTION);
        if (s.get() == ThreadStatus.COMPLETED) {
            numCompleted++;
        }
    }
    LOG.info("Completed " + numCompleted + " formats");
    assertTrue("No thread managed to complete formatting", numCompleted > 0);
}

From source file:org.kuali.rice.krad.data.provider.ProviderRegistryImplTest.java

/**
 * Verifies ProviderRegistryImpl is threadsafe
 *///from   w  w w  .j  a  va2  s  .  co m
@Test
public void testConcurrency() throws InterruptedException {
    final Class<? extends Provider>[] TYPES = new Class[] { Provider.class, MetadataProvider.class,
            PersistenceProvider.class, CustomProvider.class };

    int providers = 50;
    int threads = providers * 2; // just use live threads for all consumers/producers to ensure no consumer deadlock

    final BlockingQueue<Provider> queue = new LinkedBlockingQueue<Provider>();
    ExecutorService threadpool = Executors.newFixedThreadPool(threads);

    Callable<Object>[] producers = new Callable[providers];
    Callable<Object>[] consumers = new Callable[providers];
    Callable<Object> producer = new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            Provider p = mock(TYPES[RandomUtils.nextInt(5)]);
            registry.registerProvider(p);
            queue.add(p);
            return null;
        }
    };
    Callable<Object> consumer = new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            Provider p = queue.take();
            registry.unregisterProvider(p);
            return null;
        }
    };

    Arrays.fill(producers, producer);
    Arrays.fill(consumers, consumer);

    List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(providers * 2);
    tasks.addAll(Arrays.asList(producers));
    tasks.addAll(Arrays.asList(consumers));
    Collections.shuffle(tasks);

    System.out.println("Registering and unregistering " + providers + " providers");
    threadpool.invokeAll(tasks, 10, TimeUnit.SECONDS);

    // all producers and consumers should have run, we should be back at 0 providers registered
    assertEquals(0, registry.getProviders().size());
}

From source file:org.apache.helix.messaging.handling.HelixTaskExecutor.java

@Override
public List<Future<HelixTaskResult>> invokeAllTasks(List<MessageTask> tasks, long timeout, TimeUnit unit)
        throws InterruptedException {
    if (tasks == null || tasks.size() == 0) {
        return null;
    }/*from w ww  . j a  v  a  2 s . com*/

    // check all tasks use the same executor-service
    ExecutorService exeSvc = findExecutorServiceForMsg(tasks.get(0).getMessage());
    for (int i = 1; i < tasks.size(); i++) {
        MessageTask task = tasks.get(i);
        ExecutorService curExeSvc = findExecutorServiceForMsg(task.getMessage());
        if (curExeSvc != exeSvc) {
            LOG.error("Fail to invoke all tasks because they are not using the same executor-service");
            return null;
        }
    }

    // TODO: check if any of the task has already been scheduled

    // this is a blocking call
    List<Future<HelixTaskResult>> futures = exeSvc.invokeAll(tasks, timeout, unit);

    return futures;
}

From source file:com.hp.avmon.deploy.service.DeployService.java

public Map deployMonitors(String moId, String instanceIds) {
    String msg = "";
    String[] instanceIdList = instanceIds.split(",");
    // add by mark start
    ExecutorService executor = Executors.newFixedThreadPool(10);
    List<Callable<Map<String, String>>> tasklist = new ArrayList<Callable<Map<String, String>>>();
    List<Future<Map<String, String>>> results = new ArrayList<Future<Map<String, String>>>();
    // add by mark end      
    for (String inst : instanceIdList) {
        if (inst.trim().length() > 0) {
            ///*from  w  w  w .ja  v  a2 s  . co m*/
            // add by mark start
            tasklist.add(new DeployMonitorThread(moId, inst));
            // add by mark end
        }
    }
    // add by mark start
    try {
        results = executor.invokeAll(tasklist, 30, TimeUnit.SECONDS);
        for (Future<Map<String, String>> future : results) {
            Map<String, String> map = future.get();
            String s = map.get("msg");
            if (s != null && s.length() > 0) {
                msg += s + "<br>";
            }
            //TODO ?AMP
        }
    } catch (InterruptedException e) {
        logger.error(this.getClass().getName() + e.getMessage());
    } catch (ExecutionException e) {
        logger.error(this.getClass().getName() + e.getMessage());
    }
    // add by mark start 

    Map m = new HashMap();
    m.put("success", true);
    m.put("msg", msg);
    m.put("moId", moId);
    return m;
}

From source file:hudson.plugins.sshslaves.SSHLauncher.java

/**
 * {@inheritDoc}//from www  .  j  a  v  a2 s. c  o  m
 */
@Override
public synchronized void launch(final SlaveComputer computer, final TaskListener listener)
        throws InterruptedException {
    connection = new Connection(host, port);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Set<Callable<Boolean>> callables = new HashSet<Callable<Boolean>>();
    callables.add(new Callable<Boolean>() {
        public Boolean call() throws InterruptedException {
            Boolean rval = Boolean.FALSE;
            try {

                openConnection(listener);

                verifyNoHeaderJunk(listener);
                reportEnvironment(listener);

                String java = resolveJava(computer, listener);

                final String workingDirectory = getWorkingDirectory(computer);
                if (workingDirectory == null) {
                    listener.error("Cannot get the working directory for " + computer);
                    return Boolean.FALSE;
                }
                copySlaveJar(listener, workingDirectory);

                startSlave(computer, listener, java, workingDirectory);

                PluginImpl.register(connection);
                rval = Boolean.TRUE;
            } catch (RuntimeException e) {
                e.printStackTrace(listener.error(Messages.SSHLauncher_UnexpectedError()));
            } catch (Error e) {
                e.printStackTrace(listener.error(Messages.SSHLauncher_UnexpectedError()));
            } catch (IOException e) {
                e.printStackTrace(listener.getLogger());
            } finally {
                return rval;
            }
        }
    });

    final Node node = computer.getNode();
    final String nodeName = node != null ? node.getNodeName() : "unknown";
    try {
        long time = System.currentTimeMillis();
        List<Future<Boolean>> results;
        if (this.getLaunchTimeoutMillis() > 0) {
            results = executorService.invokeAll(callables, this.getLaunchTimeoutMillis(),
                    TimeUnit.MILLISECONDS);
        } else {
            results = executorService.invokeAll(callables);
        }
        long duration = System.currentTimeMillis() - time;
        Boolean res;
        try {
            res = results.get(0).get();
        } catch (ExecutionException e) {
            res = Boolean.FALSE;
        }
        if (!res) {
            System.out.println(
                    Messages.SSHLauncher_LaunchFailedDuration(getTimestamp(), nodeName, host, duration));
            listener.getLogger().println(getTimestamp() + " Launch failed - cleaning up connection");
            cleanupConnection(listener);
        } else {
            System.out.println(
                    Messages.SSHLauncher_LaunchCompletedDuration(getTimestamp(), nodeName, host, duration));
        }
        executorService.shutdown();
    } catch (InterruptedException e) {
        System.out.println(Messages.SSHLauncher_LaunchFailed(getTimestamp(), nodeName, host));
    }

}

From source file:com.hp.avmon.deploy.service.DeployService.java

/**
 * ?momonitorInstance ??mo11|cpu01,mo12|cpu01...
 * // w  ww .  java2s .  c o  m
 * @param moIdAndMonitorInstanceIdList
 * @return
 */
public Map batchDeployMonitor(String moIdAndMonitorInstanceIdList) {
    String msg = "";
    String[] instanceIdList = moIdAndMonitorInstanceIdList.split(",");
    // add by mark start
    ExecutorService executor = Executors.newCachedThreadPool();
    List<Callable<Map<String, String>>> tasklist = new ArrayList<Callable<Map<String, String>>>();
    List<Future<Map<String, String>>> results = new ArrayList<Future<Map<String, String>>>();
    // add by mark end   
    for (String inst : instanceIdList) {
        if (inst.split("\\|").length > 1) {
            String moId = inst.split("\\|")[0];
            String monitorInstanceId = inst.split("\\|")[1];
            // add by mark start
            String ampType = inst.split("\\|")[2];
            String ampName = inst.split("\\|")[3];
            String status = "0";
            String agentId = moId;
            String ampId = ampName;
            String ampInstId = monitorInstanceId;

            try {
                saveAgentAmp(ampId, ampInstId, ampName, status, agentId, ampType);
            } catch (Exception e) {
                logger.error(this.getClass().getName() + e.getMessage());
                e.printStackTrace();
            }

            //??AMP
            tasklist.add(new DeployMonitorThread(moId, monitorInstanceId));
            // add by mark end
        }
    }

    // add by mark start
    try {
        results = executor.invokeAll(tasklist, 20, TimeUnit.MINUTES);
        for (Future<Map<String, String>> future : results) {
            Map<String, String> map = future.get();
            String s = map.get("msg");
            if (s != null && s.length() > 0) {
                msg += s + "<br>";
            }
            //TODO ?AMP
        }
        executor.shutdown();
    } catch (InterruptedException e) {
        logger.error(this.getClass().getName() + e.getMessage());
    } catch (ExecutionException e) {
        logger.error(this.getClass().getName() + e.getMessage());
    }
    // add by mark start 
    Map m = new HashMap();
    m.put("success", true);
    m.put("msg", msg);
    return m;
}