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) throws InterruptedException;

Source Link

Document

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

Usage

From source file:org.compass.gps.device.support.parallel.ConcurrentParallelIndexExecutor.java

/**
 * Performs the indexing process using the provided index entities indexer. Creates a pool of N
 * threads (if <code>maxThreads</code> is set to -1, N is the numer of entities groups, otherwise
 * N is the number of <code>maxThreads</code>).
 *
 * @param entities             The partitioned index entities groups and index entities to index
 * @param indexEntitiesIndexer The entities indexer to use
 * @param compassGps           Compass gps interface for meta information
 *//*from ww  w  . ja v  a 2s .com*/
public void performIndex(final IndexEntity[][] entities, final IndexEntitiesIndexer indexEntitiesIndexer,
        final CompassGpsInterfaceDevice compassGps) {

    if (entities.length <= 0) {
        if (ignoreNoEtities) {
            return;
        }
        throw new IllegalArgumentException(
                "No entities listed to be indexed, have you defined your entities correctly?");
    }
    int maxThreads = this.maxThreads;
    if (maxThreads == -1) {
        maxThreads = entities.length;
    }
    ExecutorService executorService = Executors.newFixedThreadPool(maxThreads,
            new NamedThreadFactory("Compass Gps Index", false));
    try {
        ArrayList tasks = new ArrayList();
        for (int i = 0; i < entities.length; i++) {
            final IndexEntity[] indexEntities = entities[i];
            tasks.add(new Callable() {
                public Object call() throws Exception {
                    compassGps.executeForIndex(new CompassCallbackWithoutResult() {
                        protected void doInCompassWithoutResult(CompassSession session)
                                throws CompassException {
                            indexEntitiesIndexer.performIndex(session, indexEntities);
                            session.flush();
                        }
                    });
                    return null;
                }
            });
        }
        List futures;
        try {
            futures = executorService.invokeAll(tasks);
        } catch (InterruptedException e) {
            throw new CompassGpsException("Failed to index, interrupted", e);
        }

        for (Iterator it = futures.iterator(); it.hasNext();) {
            Future future = (Future) it.next();
            try {
                future.get();
            } catch (InterruptedException e) {
                throw new CompassGpsException("Failed to index, interrupted", e);
            } catch (ExecutionException e) {
                throw new CompassGpsException("Failed to index, execution exception", e);
            }
        }
    } finally {
        executorService.shutdownNow();
    }
}

From source file:org.apache.sysml.runtime.compress.CompressedMatrixBlock.java

private static ColGroup[] compressColGroups(MatrixBlock in, CompressedSizeEstimator estim,
        HashMap<Integer, Double> compRatios, int rlen, double sp, List<int[]> groups, int k)
        throws DMLRuntimeException {
    try {//from   ww  w  .ja  v  a 2  s  .c  om
        ExecutorService pool = Executors.newFixedThreadPool(k);
        ArrayList<CompressTask> tasks = new ArrayList<CompressTask>();
        for (int[] colIndexes : groups)
            tasks.add(new CompressTask(in, estim, compRatios, rlen, sp, colIndexes));
        List<Future<ColGroup>> rtask = pool.invokeAll(tasks);
        ArrayList<ColGroup> ret = new ArrayList<ColGroup>();
        for (Future<ColGroup> lrtask : rtask)
            ret.add(lrtask.get());
        pool.shutdown();
        return ret.toArray(new ColGroup[0]);
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
}

From source file:org.apache.sysml.runtime.compress.CompressedMatrixBlock.java

/**
 * Multi-thread version of leftMultByVectorTranspose.
 * //w  ww . ja  va  2 s  . c  o  m
 * @param colGroups list of column groups
 * @param vector
 *            left-hand operand of the multiplication
 * @param result
 *            buffer to hold the result; must have the appropriate size
 *            already
 * @param doTranspose if true, transpose vector
 * @param k number of threads
 * @throws DMLRuntimeException if DMLRuntimeException occurs
 */
private static void leftMultByVectorTranspose(List<ColGroup> colGroups, MatrixBlock vector, MatrixBlock result,
        boolean doTranspose, int k) throws DMLRuntimeException {
    int kuc = Math.max(1, k - colGroups.size() + 1);

    //transpose vector if required
    MatrixBlock rowVector = vector;
    if (doTranspose) {
        rowVector = new MatrixBlock(1, vector.getNumRows(), false);
        LibMatrixReorg.transpose(vector, rowVector);
    }

    // initialize and allocate the result
    result.reset();
    result.allocateDenseBlock();

    //multi-threaded execution
    try {
        ExecutorService pool = Executors.newFixedThreadPool(Math.min(colGroups.size(), k));
        ArrayList<LeftMatrixMultTask> tasks = new ArrayList<LeftMatrixMultTask>();
        for (ColGroup grp : colGroups)
            tasks.add(new LeftMatrixMultTask(grp, rowVector, result, kuc));
        pool.invokeAll(tasks);
        pool.shutdown();
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }

    // post-processing
    result.recomputeNonZeros();
}

From source file:org.fcrepo.client.test.PerformanceTests.java

/**
 * @return total time to run all iterations with threading
 *         {ingest, addDatastream, modifyDatastreamByReference,
 *         modifyDatastreamByValue, purgeDatastream, purgeObject,
 *         getDatastream, getDatastreamREST}
 *///from ww  w.ja  va 2  s .c o m
public long[] runThreadedThroughputTests() throws Exception {
    long ingestTime = 0;
    long addDsTime = 0;
    long modifyRefTime = 0;
    long modifyValTime = 0;
    long purgeDsTime = 0;
    long purgeObjectTime = 0;
    long getDatastreamTime = 0;
    long getDatastreamRestTime = 0;
    long startTime = 0;
    long stopTime = 0;

    ExecutorService threadPool = Executors.newFixedThreadPool(threads);

    // Ingest
    ArrayList<Callable<Boolean>> ingestRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        ingestRunnerList.add(new MethodRunner(MethodType.INGEST, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(ingestRunnerList);
    stopTime = System.currentTimeMillis();
    ingestTime = (stopTime - startTime);

    // Add Datastream
    ArrayList<Callable<Boolean>> addDatastreamRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        addDatastreamRunnerList.add(new MethodRunner(MethodType.ADD_DATASTREAM, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(addDatastreamRunnerList);
    stopTime = System.currentTimeMillis();
    addDsTime = (stopTime - startTime);

    // Modify Datastream By Reference
    ArrayList<Callable<Boolean>> modDatastreamRefRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        modDatastreamRefRunnerList.add(new MethodRunner(MethodType.MODIFY_DATASTREAM_REF, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(modDatastreamRefRunnerList);
    stopTime = System.currentTimeMillis();
    modifyRefTime = (stopTime - startTime);

    // Modify Datastream By Value
    ArrayList<Callable<Boolean>> modDatastreamValRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        modDatastreamValRunnerList.add(new MethodRunner(MethodType.MODIFY_DATASTREAM_VAL, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(modDatastreamValRunnerList);
    stopTime = System.currentTimeMillis();
    modifyValTime = (stopTime - startTime);

    // Purge Datastream
    ArrayList<Callable<Boolean>> purgeDatastreamRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        purgeDatastreamRunnerList.add(new MethodRunner(MethodType.PURGE_DATASTREAM, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(purgeDatastreamRunnerList);
    stopTime = System.currentTimeMillis();
    purgeDsTime = (stopTime - startTime);

    // Purge Object
    ArrayList<Callable<Boolean>> purgeObjectRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        purgeObjectRunnerList.add(new MethodRunner(MethodType.PURGE_OBJECT, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(purgeObjectRunnerList);
    stopTime = System.currentTimeMillis();
    purgeObjectTime = (stopTime - startTime);

    // Get Datastream
    ArrayList<Callable<Boolean>> getDatastreamRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        getDatastreamRunnerList.add(new MethodRunner(MethodType.GET_DATASTREAM, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(getDatastreamRunnerList);
    stopTime = System.currentTimeMillis();
    getDatastreamTime = (stopTime - startTime);

    // Get Datastream REST
    ArrayList<Callable<Boolean>> getDatastreamRestRunnerList = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < iterations; i++) {
        getDatastreamRestRunnerList.add(new MethodRunner(MethodType.GET_DATASTREAM_REST, i));
    }
    startTime = System.currentTimeMillis();
    threadPool.invokeAll(getDatastreamRestRunnerList);
    stopTime = System.currentTimeMillis();
    getDatastreamRestTime = (stopTime - startTime);

    long[] totals = { ingestTime, addDsTime, modifyRefTime, modifyValTime, purgeDsTime, purgeObjectTime,
            getDatastreamTime, getDatastreamRestTime };
    return totals;
}

From source file:org.ow2.proactive.scheduler.core.TerminationData.java

private void terminateTasks(final SchedulingService service) {

    if (tasksToTerminate.values().isEmpty()) {
        return;/* w  w w. j  a  va2s .co m*/
    }

    ExecutorService parallelTerminationService = Executors.newFixedThreadPool(tasksToTerminate.values().size());

    try {
        List<Callable<Void>> callables = new ArrayList<>(tasksToTerminate.values().size());

        for (final TaskTerminationData taskToTerminate : tasksToTerminate.values()) {

            callables.add(() -> {
                try {
                    RunningTaskData taskData = taskToTerminate.taskData;
                    if (taskToTerminate.terminatedWhileRunning()) {
                        terminateRunningTask(service, taskToTerminate, taskData);
                    }
                } catch (Throwable e) {
                    logger.error("Failed to terminate task " + taskToTerminate.taskData.getTask().getName(), e);
                    throw new RuntimeException(e);
                }
                return null;
            });
        }
        parallelTerminationService.invokeAll(callables);
    } catch (Exception e) {
        logger.error("Failed to terminate tasks ", e);
    } finally {
        parallelTerminationService.shutdown();
    }
}

From source file:massbank.BatchSearchWorker.java

/**
 * T}t@C???iHTML`?j//from  w ww .j  av a  2 s  . c o  m
 * @param resultFile t@C
 * @param htmlFile YtpHTMLt@C
 */
private void createSummary(File resultFile, File htmlFile) {
    LineNumberReader in = null;
    PrintWriter out = null;
    try {
        //(1) t@C?
        String line;
        int cnt = 0;
        ArrayList<String> nameList = new ArrayList<String>();
        ArrayList<String> top1LineList = new ArrayList<String>();
        TreeSet<String> top1IdList = new TreeSet<String>();
        in = new LineNumberReader(new FileReader(resultFile));
        while ((line = in.readLine()) != null) {
            line = line.trim();
            if (line.equals("")) {
                cnt = 0;
            } else {
                cnt++;
                if (cnt == 1) {
                    nameList.add(line);
                } else if (cnt == 2) {
                    if (line.equals("-1")) {
                        top1LineList.add("Invalid");
                    }
                    if (line.equals("0")) {
                        top1LineList.add("0");
                    }
                } else if (cnt == 4) {
                    String[] vals = line.split("\t");
                    String id = vals[0];
                    top1IdList.add(id);
                    top1LineList.add(line);
                }
            }
        }

        //? http://www.massbank.jp/ T?[o??KEGG???s
        HashMap<String, ArrayList> massbank2mapList = new HashMap<String, ArrayList>(); //(2)p
        HashMap<String, String> massbank2keggList = new HashMap<String, String>(); //(2)p
        HashMap<String, ArrayList> map2keggList = new HashMap<String, ArrayList>(); //(3)p
        ArrayList<String> mapNameList = new ArrayList<String>(); //(4)p
        boolean isKeggReturn = false;
        //         if (serverUrl.indexOf("www.massbank.jp") == -1) {
        //            isKeggReturn = false;
        //         }
        if (isKeggReturn) {

            //(2) KEGG ID, Map IDDB
            String where = "where MASSBANK in(";
            Iterator it = top1IdList.iterator();
            while (it.hasNext()) {
                String id = (String) it.next();
                where += "'" + id + "',";
            }
            where = where.substring(0, where.length() - 1);
            where += ")";
            String sql = "select MASSBANK, t1.KEGG, MAP from " + "(SELECT MASSBANK,KEGG FROM OTHER_DB_IDS "
                    + where + ") t1, PATHWAY_CPDS t2" + " where t1.KEGG=t2.KEGG order by MAP,MASSBANK";

            ArrayList<String> mapList = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String connectUrl = "jdbc:mysql://localhost/MassBank_General";
                Connection con = DriverManager.getConnection(connectUrl, "bird", "bird2006");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                String prevId = "";
                while (rs.next()) {
                    String id = rs.getString(1);
                    String kegg = rs.getString(2);
                    String map = rs.getString(3);
                    if (!id.equals(prevId)) {
                        if (!prevId.equals("")) {
                            massbank2mapList.put(prevId, mapList);
                        }
                        mapList = new ArrayList<String>();
                        massbank2keggList.put(id, kegg);
                    }
                    mapList.add(map);
                    prevId = id;
                }
                massbank2mapList.put(prevId, mapList);

                rs.close();
                stmt.close();
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (mapList != null) {

                //(3) Pathway Map?FtXg??
                it = massbank2mapList.keySet().iterator();
                while (it.hasNext()) {
                    String id = (String) it.next();
                    String kegg = (String) massbank2keggList.get(id);

                    ArrayList<String> list1 = massbank2mapList.get(id);
                    for (int i = 0; i < list1.size(); i++) {
                        String map = list1.get(i);
                        ArrayList<String> list2 = null;
                        if (map2keggList.containsKey(map)) {
                            list2 = map2keggList.get(map);
                            list2.add(kegg);
                        } else {
                            list2 = new ArrayList<String>();
                            list2.add(kegg);
                            map2keggList.put(map, list2);
                        }
                    }
                }

                //(4) SOAPPathway Map?Ft?\bh?s
                it = map2keggList.keySet().iterator();
                List<Callable<HashMap<String, String>>> tasks = new ArrayList();
                while (it.hasNext()) {
                    String map = (String) it.next();
                    mapNameList.add(map);
                    ArrayList<String> list = map2keggList.get(map);
                    String[] cpds = list.toArray(new String[] {});
                    Callable<HashMap<String, String>> task = new ColorPathway(map, cpds);
                    tasks.add(task);
                }
                Collections.sort(mapNameList);

                // Xbhv?[10
                ExecutorService exsv = Executors.newFixedThreadPool(10);
                List<Future<HashMap<String, String>>> results = exsv.invokeAll(tasks);

                // Pathway mapi[??
                String saveRootPath = MassBankEnv.get(MassBankEnv.KEY_TOMCAT_APPTEMP_PATH) + "pathway";
                File rootDir = new File(saveRootPath);
                if (!rootDir.exists()) {
                    rootDir.mkdir();
                }
                //               String savePath = saveRootPath + File.separator + this.jobId;
                //               File newDir = new File(savePath);
                //               if ( !newDir.exists() ) {
                //                  newDir.mkdir();
                //               }

                //(6) Pathway mapURL
                for (Future<HashMap<String, String>> future : results) {
                    HashMap<String, String> res = future.get();
                    it = res.keySet().iterator();
                    String map = (String) it.next();
                    String mapUrl = res.get(map);
                    String filePath = saveRootPath + File.separator + this.jobId + "_" + map + ".png";
                    FileUtil.downloadFile(mapUrl, filePath);
                }
            }
        }

        //(7) ?o
        out = new PrintWriter(new BufferedWriter(new FileWriter(htmlFile)));
        // wb_?[?o
        String reqIonStr = "Both";
        try {
            if (Integer.parseInt(this.ion) > 0) {
                reqIonStr = "Positive";
            } else if (Integer.parseInt(this.ion) < 0) {
                reqIonStr = "Negative";
            }
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        String title = "Summary of Batch Service Results";
        out.println("<html>");
        out.println("<head>");
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>" + title + "</h1>");
        out.println("<hr>");
        out.println("<h3>Request Date : " + this.time + "</h3>");
        out.println("Instrument Type : " + this.inst + "<br>");
        out.println("MS Type : " + this.ms + "<br>");
        out.println("Ion Mode : " + reqIonStr + "<br>");
        out.println("<br>");
        out.println("<hr>");
        out.println("<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">");
        String cols = String.valueOf(mapNameList.size());
        out.println("<tr>");
        out.println("<th bgcolor=\"LavenderBlush\" rowspan=\"1\">No.</th>");
        out.println("<th bgcolor=\"LavenderBlush\" rowspan=\"1\">Query&nbsp;Name</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Score</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Hit</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">MassBank&nbsp;ID</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Record&nbsp;Title</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Formula</th>");
        out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Exact Mass</th>");
        if (isKeggReturn) {
            out.println("<th bgcolor=\"LightYellow\" rowspan=\"2\">KEGG&nbsp;ID</th>");
            out.println(
                    "<th bgcolor=\"LightYellow\" colspan=\"" + cols + "\">Colored&nbsp;Pathway&nbsp;Maps</th>");
        }
        out.println("</tr>");
        out.print("<tr bgcolor=\"moccasin\">");
        for (int i = 0; i < mapNameList.size(); i++) {
            out.print("<th>MAP" + String.valueOf(i + 1) + "</th>");
        }
        out.println("</tr>");

        for (int i = 0; i < nameList.size(); i++) {
            out.println("<tr>");
            String no = String.format("%5d", i + 1);
            no = no.replace(" ", "&nbsp;");
            out.println("<td>" + no + "</td>");
            // Query Name
            String queryName = nameList.get(i);
            out.println("<td nowrap>" + queryName + "</td>");

            line = top1LineList.get(i);
            if (line.equals("0")) {
                if (isKeggReturn) {
                    cols = String.valueOf(mapNameList.size() + 5);
                } else {
                    cols = String.valueOf(6);
                }
                out.println("<td colspan=\"" + cols + "\">No Hit Record</td>");
            } else if (line.equals("Invalid")) {
                if (isKeggReturn) {
                    cols = String.valueOf(mapNameList.size() + 5);
                } else {
                    cols = String.valueOf(4);
                }
                out.println("<td colspan=\"" + cols + "\">Invalid Query</td>");
            } else {
                String[] data = formatLine(line);
                String id = data[0];
                String recTitle = data[1];
                String formula = data[2];
                String emass = data[3];
                String score = data[4];
                String hit = data[5];

                boolean isHiScore = false;
                if (Integer.parseInt(hit) >= 3 && Double.parseDouble(score) >= 0.8) {
                    isHiScore = true;
                }

                // Score
                if (isHiScore) {
                    out.println("<td><b>" + score + "</b></td>");
                } else {
                    out.println("<td>" + score + "</td>");
                }

                // hit peak
                if (isHiScore) {
                    out.println("<td align=\"right\"><b>" + hit + "</b></td>");
                } else {
                    out.println("<td align=\"right\">" + hit + "</td>");
                }

                // MassBank ID & Link
                out.println("<td><a href=\"" + serverUrl + "jsp/FwdRecord.jsp?id=" + id
                        + "\" target=\"_blank\">" + id + "</td>");
                // Record Title
                out.println("<td>" + recTitle + "</td>");

                // Formula
                out.println("<td nowrap>" + formula + "</td>");

                // Exact Mass
                out.println("<td nowrap>" + emass + "</td>");

                // KEGG ID & Link
                if (isKeggReturn) {
                    String keggLink = "&nbsp;&nbsp;-";
                    if (massbank2keggList.containsKey(id)) {
                        String keggUrl = "http://www.genome.jp/dbget-bin/www_bget?";
                        String kegg = massbank2keggList.get(id);
                        switch (kegg.charAt(0)) {
                        case 'C':
                            keggUrl += "cpd:" + kegg;
                            break;
                        case 'D':
                            keggUrl += "dr:" + kegg;
                            break;
                        case 'G':
                            keggUrl += "gl:" + kegg;
                            break;
                        }
                        keggLink = "<a href=\"" + keggUrl + "\" target=\"_blank\">" + kegg + "</a>";
                    }
                    out.println("<td>" + keggLink + "</td>");
                    // Pathway Map Link
                    if (massbank2mapList.containsKey(id)) {
                        ArrayList<String> list = massbank2mapList.get(id);
                        for (int l1 = mapNameList.size() - 1; l1 >= 0; l1--) {
                            boolean isFound = false;
                            String map = "";
                            for (int l2 = list.size() - 1; l2 >= 0; l2--) {
                                map = list.get(l2);
                                if (map.equals(mapNameList.get(l1))) {
                                    isFound = true;
                                    break;
                                }
                            }
                            if (isFound) {
                                ArrayList<String> list2 = map2keggList.get(map);
                                String mapUrl = serverUrl + "temp/pathway/" + this.jobId + "_" + map + ".png";
                                out.println("<td nowrap><a href=\"" + mapUrl + "\" target=\"_blank\">map:" + map
                                        + "(" + list2.size() + ")</a></td>");
                            } else {
                                out.println("<td>&nbsp;&nbsp;-</td>");
                            }
                        }
                    } else {
                        for (int l1 = mapNameList.size() - 1; l1 >= 0; l1--) {
                            out.println("<td>&nbsp;&nbsp;-</td>");
                        }
                    }
                }
            }
            out.println("</tr>");
        }
        out.println("</table>");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:com.topekalabs.synchronization.LockTest.java

private void testWithContentionHelper(int numThreads, ExecutorService es) {
    final long timeout = 4000;
    final int numIncs = 500;
    final int expectedIncs = numIncs * numThreads;

    final MutableInt a = new MutableInt();
    final MutableInt b = new MutableInt();
    final Lock lock;

    try {/*from www  .ja  v  a  2  s . com*/
        lock = lockClass.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }

    Callable<Integer> callable = new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            for (int incCount = 0; incCount < numIncs; incCount++) {
                lock.lock();
                int tempB = b.intValue();
                a.increment();
                Thread.sleep(1);
                a.add(b.intValue() - tempB);
                b.increment();
                lock.unlock();
            }

            return 1;
        }
    };

    List<Callable<Integer>> callables = CollectionUtils.fillEmptyCollection(new ArrayList<Callable<Integer>>(),
            callable, numThreads);

    try {
        List<Future<Integer>> futures = es.invokeAll(callables);
        ThreadUtils.done(futures, timeout, TimeUnit.MINUTES);
    } catch (InterruptedException | TimeoutException | ExecutionException ex) {
        ErrorThrower.kill(ex);
    }

    Assert.assertEquals("Incorrect number of increments.", expectedIncs, a.intValue());
    Assert.assertEquals("Incorrect number of increments.", expectedIncs, b.intValue());
}

From source file:io.hops.security.TestUsersGroups.java

public void testConcurrentAddUser(int cacheTime, int cacheSize) throws Exception {
    Configuration conf = new Configuration();
    conf.set(CommonConfigurationKeys.HOPS_UG_CACHE_SECS, Integer.toString(cacheTime));
    conf.set(CommonConfigurationKeys.HOPS_UG_CACHE_SIZE, Integer.toString(cacheSize));
    HdfsStorageFactory.resetDALInitialized();
    HdfsStorageFactory.setConfiguration(conf);
    HdfsStorageFactory.formatStorage();//  w  w w  . j a  v  a  2s  .  c o m
    UsersGroups.createSyncRow();

    final String userName = "user1";
    final String groupNmae = "group1";
    final int CONCURRENT_USERS = 100;
    ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_USERS);

    List<Callable<Integer>> callables = new ArrayList<>();
    for (int i = 0; i < CONCURRENT_USERS; i++) {
        callables.add(new AddUser(userName, groupNmae));
    }

    List<Future<Integer>> futures = executorService.invokeAll(callables);
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.SECONDS);

    UsersGroups.clearCache();

    for (Future<Integer> f : futures) {
        try {
            f.get();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
            fail();
        }
    }
}

From source file:com.basho.riak.client.http.util.logging.ConcurrentLoggingTest.java

/**
 * Test method for/*from   ww  w .  ja va 2s .c o  m*/
 * {@link com.basho.riak.client.http.util.logging.LogNoHttpResponseRetryHandler#retryMethod(org.apache.commons.httpclient.HttpMethod, java.io.IOException, int)}
 * .
 * 
 * @throws InterruptedException
 */
@Test
public void retry_concurrentLogAndDump() throws InterruptedException {
    // create a bunch of threads
    // each must log 10 statements and call flush
    // ALL the statements must be present BUT ONCE in
    // the mock delegate appender (order does not matter)
    final int numThreads = 10;
    final LogNoHttpResponseRetryHandler handler = new LogNoHttpResponseRetryHandler();
    ExecutorService es = Executors.newFixedThreadPool(numThreads);
    List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(numThreads);

    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch dumpLatch = new CountDownLatch(10);

    for (int i = 0; i < numThreads; i++) {
        final int threadCounter = i;
        tasks.add(new Callable<Void>() {

            @Override
            public Void call() {
                Logger logger = Logger.getLogger("httpclient.wire");
                try {
                    startLatch.await();

                    for (int j = 0; j < 10; j++) {
                        logger.debug(String.format(MESSAGE, new Object[] { threadCounter, j }));
                    }

                    dumpLatch.countDown();
                    dumpLatch.await();

                    handler.retryMethod(new GetMethod(), new NoHttpResponseException(), 0);

                    return null;
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e);
                }
            }
        });
    }

    startLatch.countDown();
    es.invokeAll(tasks);

    verify(mockLogger, times(100)).callAppenders(logEventCaptor.capture());

    TreeSet<Integer> check = new TreeSet<Integer>();

    for (LoggingEvent le : logEventCaptor.getAllValues()) {
        // verify that each of Thread:Iter is present for 0-90-9
        int loc = Integer.parseInt(le.getMessage().toString());
        check.add(loc);
    }

    assertEquals(100, check.size());
    assertEquals(0, (int) check.first());
    assertEquals(99, (int) check.last());
}

From source file:com.concursive.connect.web.portal.PortletManager.java

private static boolean renderPortlet(ActionContext context, PortletContainer container,
        DashboardPortlet thisPortlet, PortletWindowImpl portletWindow, PortalServletRequest portalRequest,
        PortalServletResponse portalResponse) {
    ExecutorService executor = null;
    List<Future<PortletWindowImpl>> futures = null;
    try {/*from w  w  w. j  av a  2  s  .  c om*/
        long doRenderStartTime = System.currentTimeMillis();
        // NOTE: The infrastructure is here to run in threads, but more work needs to be done
        // for this to be reliable.  So the timeout feature of Executor cannot be used yet
        //      if (thisPortlet.getTimeout() <= 0) {
        if (true) {
            // Remove the project since it might get set by another project
            if (LOG.isDebugEnabled()) {
                LOG.debug("before container.doRender");
                if (portalRequest.getAttribute("project") != null) {
                    LOG.debug("  The project for rendering: "
                            + ((Project) portalRequest.getAttribute("project")).getUniqueId());
                }
            }
            portalRequest.removeAttribute("project");
            // Render the portlet immediately
            container.doRender(portletWindow, portalRequest, portalResponse);
        } else {
            // Render the portlet using an executor; this will allow for cancelling timed-out portlets
            LOG.debug("Using executor...");
            List<PortletRenderTask> renderTasks = new ArrayList<PortletRenderTask>();
            renderTasks.add(new PortletRenderTask(container, portletWindow, portalRequest, portalResponse));
            executor = Executors.newFixedThreadPool(1);
            // NOTE: this wrapper fix is for Java 1.5
            final Collection<Callable<PortletWindowImpl>> wrapper = Collections
                    .<Callable<PortletWindowImpl>>unmodifiableCollection(renderTasks);
            if (thisPortlet.getTimeout() <= 0) {
                futures = executor.invokeAll(wrapper);
            } else {
                futures = executor.invokeAll(wrapper, thisPortlet.getTimeout(), TimeUnit.SECONDS);
            }
            for (Future<PortletWindowImpl> f : futures) {
                if (f.isCancelled()) {
                    LOG.debug("Portlet was cancelled due to timeout");
                    return false;
                }
            }
        }
        long doRenderEndTime = System.currentTimeMillis();
        LOG.debug("Portlet (" + thisPortlet.getName() + ") took: " + (doRenderEndTime - doRenderStartTime)
                + " ms");
        // When the portlet is rendered, place the response in the request for displaying later
        context.getRequest().setAttribute("portal_response_" + thisPortlet.getId(),
                portalResponse.getInternalBuffer().toString());
        LOG.trace("Render response: " + portalResponse.getInternalBuffer().toString());
    } catch (Exception re) {
        // The portlet could not be rendered so skip it
        LOG.error("Render exception", re);
        return false;
    } finally {
        if (executor != null) {
            executor.shutdown();
        }
    }
    return true;
}