Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:org.apache.juneau.rest.client.RestClient.java

ExecutorService getExecutorService(boolean create) {
    if (executorService != null || !create)
        return executorService;
    synchronized (this) {
        if (executorService == null)
            executorService = new ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(10));
        return executorService;
    }/*www  . ja v  a  2 s.  c o  m*/
}

From source file:la2launcher.MainFrame.java

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
    final long initTime = new Date().getTime();
    ReentrantLock lock = new ReentrantLock();
    final String patcherUrl = "http://" + updateHost + "/hf//updater.lst.la2";//new ArrayBlockingQueue<Runnable>(10000)
    final ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 5, 1, TimeUnit.HOURS,
            new ArrayBlockingQueue<Runnable>(10000));
    filesProcessed = 0;//from   w ww.ja  v  a2 s.  c  o  m
    tpe.execute(new Runnable() {
        @Override
        public void run() {
            jTextArea2.setText("");
            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
            jProgressBar1.setMinimum(0);
            jProgressBar1.setMaximum(model.getRowCount());
            jProgressBar1.setValue(0);
            jLabel4.setText("0/" + model.getRowCount());
            for (int i = 0; i < model.getRowCount(); i++) {
                boolean checked = (Boolean) model.getValueAt(i, 1);
                String fileName = (String) model.getValueAt(i, 0);
                if (checked) {
                    tpe.execute(new Runnable() {
                        @Override
                        public void run() {

                            final String fileUrl = "http://" + updateHost + "/hf/" + fileName.replace("\\", "/")
                                    + ".la2";
                            try {
                                printMsg("  " + fileUrl);
                                File file = new File(gamePath + fileName + ".rar");
                                File fileExt = new File(gamePath + fileName);

                                file.getParentFile().mkdirs();
                                FileOutputStream fos = new FileOutputStream(file);
                                CloseableHttpClient httpclient = HttpClients.createDefault();
                                HttpGet httpGet = new HttpGet(fileUrl);
                                CloseableHttpResponse response1 = httpclient.execute(httpGet);

                                HttpEntity entity1 = response1.getEntity();
                                copyStream(entity1.getContent(), fos, new CopyListener() {
                                    @Override
                                    public void transfered(int n) {
                                        bytesRecieved += n;
                                        bytesRecievedTotal += n;
                                    }
                                });
                                response1.close();
                                fos.close();

                                printMsg("?? : " + fileName);

                                lock.lock();
                                //fixBzip2File(file);
                                //printMsg(" ?");

                                extractArchive(file.getAbsolutePath());

                                //BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new FileInputStream(file));
                                //OutputStream pout = new FileOutputStream(fileExt);
                                //copyStream(archStream, pout, null);
                                //pout.close();
                                //archStream.close();
                                //jTextArea2.setText(jTextArea2.getText() + "\r\n? : " + fileName);
                                printMsg("? : " + fileName);
                                //file.delete();

                                //                                    File tgt = new File(gamePath + fileName);
                                //                                    if (tgt.exists()) {
                                //                                        tgt.delete();
                                //                                    }
                                //tgt.getParentFile().mkdirs();
                                //Files.move(fileExt.toPath(), new File(gamePath + fileName).toPath());
                                //jTextArea2.setText(jTextArea2.getText() + "\r\n ??: " + fileName);
                                printMsg(" ??: " + fileName);
                                jProgressBar1.setIndeterminate(false);
                                jLabel4.setText((++filesProcessed) + "/" + model.getRowCount());
                                jProgressBar1.setValue((int) filesProcessed);
                                lock.unlock();
                            } catch (IOException ex) {
                                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (Exception ex) {
                                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });
                }
            }
        }
    });

    jButton5.setEnabled(false);
    jButton6.setEnabled(false);
    jButton7.setEnabled(false);
    jButton8.setEnabled(false);
    jButton9.setEnabled(false);
    jButton10.setEnabled(false);
    jProgressBar1.setIndeterminate(true);
    new Thread() {
        @Override
        public void run() {
            do {
                long millis = new Date().getTime();
                try {
                    sleep(300);
                } catch (InterruptedException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                millis = new Date().getTime() - millis;
                BigDecimal totBig = new BigDecimal(bytesRecievedTotal / (1024 * 1024.0));
                totBig = totBig.setScale(2, BigDecimal.ROUND_CEILING);
                jLabel5.setText("?: " + (bytesRecieved / millis) + "KB/s. : "
                        + totBig + " MB");
                bytesRecieved = 0;
            } while (tpe.getActiveCount() > 0);
            tpe.shutdown();
            jButton5.setEnabled(true);
            jButton6.setEnabled(true);
            jButton7.setEnabled(true);
            jButton8.setEnabled(true);
            jButton9.setEnabled(true);
            jButton10.setEnabled(true);
            jProgressBar1.setIndeterminate(false);
            printMsg("  " + (new Date().getTime() - initTime)
                    + " ?.");
        }
    }.start();
}

From source file:org.lockss.hasher.SimpleHasher.java

/**
 * Provides the executor of asynchronous hashing operations.
 * /* ww w . j  a  va  2s.  co m*/
 * @return a ThreadPoolExecutor with the executor.
 */
private static synchronized ThreadPoolExecutor getExecutor() {
    if (EXECUTOR == null) {
        Configuration config = ConfigManager.getCurrentConfig();
        int poolsize = config.getInt(PARAM_THREADPOOL_SIZE, DEFAULT_THREADPOOL_SIZE);
        long keepalive = config.getTimeInterval(PARAM_THREADPOOL_KEEPALIVE, DEFAULT_THREADPOOL_KEEPALIVE);
        EXECUTOR = new ThreadPoolExecutor(poolsize, poolsize, keepalive, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
        EXECUTOR.allowCoreThreadTimeOut(true);
    }
    return EXECUTOR;
}

From source file:la2launcher.MainFrame.java

private void processValidation(boolean full) {
    final long initTime = new Date().getTime();
    final String patcherUrl = "http://" + updateHost + "/hf/updater.lst.la2";//new ArrayBlockingQueue<Runnable>(10000)
    final ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 5, 1, TimeUnit.HOURS,
            new ArrayBlockingQueue<Runnable>(10000));
    tpe.execute(new Runnable() {
        @Override/*from  w ww . j  ava  2s . c om*/
        public void run() {
            jTextArea2.setText("");
            try {
                if (full) {
                    jTextArea2.setText(jTextArea2.getText() + "\r\n?  ");
                } else {
                    jTextArea2.setText(jTextArea2.getText() + "\r\n?  system");
                }
                File patcher = File.createTempFile("la2", "la2");
                patcher.deleteOnExit();
                File patcherExt = File.createTempFile("la2", "la2");
                patcherExt.deleteOnExit();
                FileOutputStream fos = new FileOutputStream(patcher);
                CloseableHttpClient httpclient = HttpClients.createDefault();
                HttpGet httpGet = new HttpGet(patcherUrl);
                CloseableHttpResponse response1 = httpclient.execute(httpGet);

                HttpEntity entity1 = response1.getEntity();
                copyStream(entity1.getContent(), fos, null);
                response1.close();
                fos.close();
                jTextArea2.setText(jTextArea2.getText()
                        + "\r\n??  ? ?: " + patcherUrl);

                fixBzip2File(patcher);
                jTextArea2.setText(jTextArea2.getText() + "\r\n ?");

                BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new FileInputStream(patcher));
                OutputStream pout = new FileOutputStream(patcherExt);
                copyStream(bz, pout, new CopyListener() {
                    @Override
                    public void transfered(int n) {
                        bytesRecieved += n;
                        bytesRecievedTotal += n;
                    }
                });
                pout.close();
                bz.close();
                jTextArea2.setText(jTextArea2.getText() + "\r\n? ?");

                if (full) {
                    jTextArea2.setText(jTextArea2.getText() + "\r\n  ");
                } else {
                    jTextArea2.setText(jTextArea2.getText()
                            + "\r\n     system");
                }

                DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
                model.setRowCount(0);

                int filesCount = scanSumFilesCount(patcherExt, full);
                jProgressBar1.setMinimum(0);
                jProgressBar1.setMaximum(filesCount);
                jProgressBar1.setValue(0);
                jLabel4.setText("0/" + filesCount);
                scanSumFile(patcherExt, new SumHandler() {

                    private ReentrantLock lock = new ReentrantLock();

                    @Override
                    public void handle(MDNamePair pair) {
                        try {
                            jProgressBar1.setIndeterminate(false);
                            //lock.unlock();
                            tpe.execute(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        lock.lock();
                                        //printMsg(pair.filename);
                                        String crc = digest(new File(gamePath + pair.filename));
                                        //printMsg("    : " + pair.crc);
                                        //printMsg("   ? ? : " + crc);
                                        if (!pair.crc.equals(crc)) {
                                            DefaultTableModel dtm = (DefaultTableModel) jTable2.getModel();
                                            dtm.addRow(new Object[] { pair.filename, false });
                                        }
                                        jProgressBar1.setValue(jProgressBar1.getValue() + 1);
                                        jLabel4.setText(jProgressBar1.getValue() + "/" + filesCount);
                                        lock.unlock();
                                    } catch (NoSuchAlgorithmException ex) {
                                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                                    } catch (IOException ex) {
                                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                                    } finally {
                                        //if (lock.isLocked()) lock.unlock();
                                    }
                                }
                            });
                        } finally {
                            //if (lock.isLocked()) lock.unlock();
                        }
                    }
                }, full);
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

    jButton5.setEnabled(false);
    jButton6.setEnabled(false);
    jButton7.setEnabled(false);
    jButton8.setEnabled(false);
    jButton10.setEnabled(false);
    jProgressBar1.setIndeterminate(true);
    new Thread() {
        @Override
        public void run() {
            do {
                long millis = new Date().getTime();
                try {
                    sleep(300);
                } catch (InterruptedException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                millis = new Date().getTime() - millis;
                BigDecimal totBig = new BigDecimal(bytesRecievedTotal / (1024 * 1024.0));
                totBig = totBig.setScale(2, BigDecimal.ROUND_CEILING);
                jLabel5.setText("?: " + (bytesRecieved / millis) + "KB/s. : "
                        + totBig + " MB");
                bytesRecieved = 0;
            } while (tpe.getActiveCount() > 0);
            tpe.shutdown();
            jButton5.setEnabled(true);
            jButton6.setEnabled(true);
            jButton7.setEnabled(true);
            jButton8.setEnabled(true);
            jButton10.setEnabled(true);
            jProgressBar1.setIndeterminate(false);
            printMsg("  " + (new Date().getTime() - initTime)
                    + " ?.");
        }
    }.start();
}

From source file:org.apache.hadoop.hbase.util.FSUtils.java

/**
 * This function is to scan the root path of the file system to get either the
 * mapping between the region name and its best locality region server or the
 * degree of locality of each region on each of the servers having at least
 * one block of that region. The output map parameters are both optional.
 *
 * @param conf/*from  ww  w.j ava2s  .  co  m*/
 *          the configuration to use
 * @param desiredTable
 *          the table you wish to scan locality for
 * @param threadPoolSize
 *          the thread pool size to use
 * @param regionToBestLocalityRSMapping
 *          the map into which to put the best locality mapping or null
 * @param regionDegreeLocalityMapping
 *          the map into which to put the locality degree mapping or null,
 *          must be a thread-safe implementation
 * @throws IOException
 *           in case of file system errors or interrupts
 */
private static void getRegionLocalityMappingFromFS(final Configuration conf, final String desiredTable,
        int threadPoolSize, Map<String, String> regionToBestLocalityRSMapping,
        Map<String, Map<String, Float>> regionDegreeLocalityMapping) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path rootPath = FSUtils.getRootDir(conf);
    long startTime = EnvironmentEdgeManager.currentTimeMillis();
    Path queryPath;
    // The table files are in ${hbase.rootdir}/data/<namespace>/<table>/*
    if (null == desiredTable) {
        queryPath = new Path(new Path(rootPath, HConstants.BASE_NAMESPACE_DIR).toString() + "/*/*/*/");
    } else {
        queryPath = new Path(FSUtils.getTableDir(rootPath, TableName.valueOf(desiredTable)).toString() + "/*/");
    }

    // reject all paths that are not appropriate
    PathFilter pathFilter = new PathFilter() {
        @Override
        public boolean accept(Path path) {
            // this is the region name; it may get some noise data
            if (null == path) {
                return false;
            }

            // no parent?
            Path parent = path.getParent();
            if (null == parent) {
                return false;
            }

            String regionName = path.getName();
            if (null == regionName) {
                return false;
            }

            if (!regionName.toLowerCase().matches("[0-9a-f]+")) {
                return false;
            }
            return true;
        }
    };

    FileStatus[] statusList = fs.globStatus(queryPath, pathFilter);

    if (null == statusList) {
        return;
    } else {
        LOG.debug("Query Path: " + queryPath + " ; # list of files: " + statusList.length);
    }

    // lower the number of threads in case we have very few expected regions
    threadPoolSize = Math.min(threadPoolSize, statusList.length);

    // run in multiple threads
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(statusList.length));
    try {
        // ignore all file status items that are not of interest
        for (FileStatus regionStatus : statusList) {
            if (null == regionStatus) {
                continue;
            }

            if (!regionStatus.isDirectory()) {
                continue;
            }

            Path regionPath = regionStatus.getPath();
            if (null == regionPath) {
                continue;
            }

            tpe.execute(new FSRegionScanner(fs, regionPath, regionToBestLocalityRSMapping,
                    regionDegreeLocalityMapping));
        }
    } finally {
        tpe.shutdown();
        int threadWakeFrequency = conf.getInt(HConstants.THREAD_WAKE_FREQUENCY, 60 * 1000);
        try {
            // here we wait until TPE terminates, which is either naturally or by
            // exceptions in the execution of the threads
            while (!tpe.awaitTermination(threadWakeFrequency, TimeUnit.MILLISECONDS)) {
                // printing out rough estimate, so as to not introduce
                // AtomicInteger
                LOG.info("Locality checking is underway: { Scanned Regions : " + tpe.getCompletedTaskCount()
                        + "/" + tpe.getTaskCount() + " }");
            }
        } catch (InterruptedException e) {
            throw (InterruptedIOException) new InterruptedIOException().initCause(e);
        }
    }

    long overhead = EnvironmentEdgeManager.currentTimeMillis() - startTime;
    String overheadMsg = "Scan DFS for locality info takes " + overhead + " ms";

    LOG.info(overheadMsg);
}

From source file:com.mirth.connect.server.controllers.DonkeyEngineController.java

@Override
public synchronized List<ChannelFuture> submitTasks(List<ChannelTask> tasks, ChannelTaskHandler handler) {
    List<ChannelFuture> futures = new ArrayList<ChannelFuture>();

    /*//from ww w  .  ja va  2s .  com
     * If no handler is given then use the default handler to that at least errors will be
     * logged out.
     */
    if (handler == null) {
        handler = new LoggingTaskHandler();
    }

    for (ChannelTask task : tasks) {
        ExecutorService engineExecutor = engineExecutors.get(task.getChannelId());

        if (engineExecutor == null) {
            engineExecutor = new ThreadPoolExecutor(0, 1, 10L, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>());
            engineExecutors.put(task.getChannelId(), engineExecutor);
        }

        task.setHandler(handler);
        try {
            futures.add(task.submitTo(engineExecutor));
        } catch (RejectedExecutionException e) {
            /*
             * This can happen if a channel was halted, in which case we don't want to perform
             * whatever task this was anyway.
             */
            handler.taskErrored(task.getChannelId(), task.getMetaDataId(), e);
        }
    }

    return futures;
}

From source file:com.mirth.connect.server.controllers.DonkeyEngineController.java

private List<ChannelFuture> submitHaltTasks(Set<String> channelIds, ChannelTaskHandler handler) {
    List<ChannelFuture> futures = new ArrayList<ChannelFuture>();

    /*/* w  ww .  ja  va  2 s. c o m*/
     * If no handler is given then use the default handler to that at least errors will be
     * logged out.
     */
    if (handler == null) {
        handler = new LoggingTaskHandler();
    }

    for (String channelId : channelIds) {
        /*
         * Shutdown the executor to prevent any new tasks from being submitted. This needs to be
         * called once outside of the synchronized block in order to halt certain actions such
         * as restoring server configuration.
         */
        shutdownExecutor(channelId);

        synchronized (this) {
            /*
             * Shutdown the executor to prevent any new tasks from being submitted. This needs
             * to be called once inside the synchronized block in case multiple halts were
             * performed.
             */
            shutdownExecutor(channelId);

            /*
             * Create a new executor to submit the halt task to. Since all the submit methods
             * are synchronized, it is not possible for any other tasks for this channel to
             * occur before the halt task.
             */
            ExecutorService engineExecutor = new ThreadPoolExecutor(0, 1, 10L, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>());
            engineExecutors.put(channelId, engineExecutor);

            ChannelTask haltTask = new HaltTask(channelId);
            haltTask.setHandler(handler);
            futures.add(haltTask.submitTo(engineExecutor));
        }

    }
    return futures;
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testIssue9() throws Exception {
    int threadCount = 10;

    final int objectSize = 10 * 1000 * 1000; // size is not a power of 2.
    final MetadataList list = new MetadataList();
    list.addMetadata(new Metadata("test-data", null, true));
    final EsuApi api = esu;
    final List<Identifier> cleanupList = cleanup;
    ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 0, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    try {/* w w  w . j a  v a2 s.  c  o m*/
        for (int i = 0; i < threadCount; i++) {
            executor.execute(new Thread() {
                public void run() {
                    ObjectId oid = api.createObjectFromStream(null, list, new RandomInputStream(objectSize),
                            objectSize, null);
                    cleanupList.add(oid);
                }
            });
        }
        while (true) {
            Thread.sleep(1000);
            if (executor.getActiveCount() < 1)
                break;
        }
    } finally {
        executor.shutdown();
    }
}

From source file:com.emc.atmos.api.test.AtmosApiClientTest.java

@Test
public void testIssue9() throws Exception {
    int threadCount = 10;

    final int objectSize = 10 * 1000 * 1000; // not a power of 2
    final AtmosApi atmosApi = api;
    final List<ObjectIdentifier> cleanupList = new ArrayList<ObjectIdentifier>();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 0, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    try {// www .  jav  a2 s . c o m
        for (int i = 0; i < threadCount; i++) {
            executor.execute(new Thread() {
                public void run() {
                    CreateObjectRequest request = new CreateObjectRequest();
                    request.content(new RandomInputStream(objectSize)).contentLength(objectSize)
                            .userMetadata(new Metadata("test-data", null, true));
                    ObjectId oid = atmosApi.createObject(request).getObjectId();
                    cleanupList.add(oid);
                }
            });
        }
        while (true) {
            Thread.sleep(1000);
            if (executor.getActiveCount() < 1)
                break;
        }
    } finally {
        executor.shutdown();
        cleanup.addAll(cleanupList);
        if (cleanupList.size() < threadCount)
            Assert.fail("At least one thread failed");
    }
}

From source file:com.emc.atmos.api.test.AtmosApiClientTest.java

@Test
public void testMultiThreadedBufferedWriter() throws Exception {
    int threadCount = 20;
    ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 5000, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    // test with String
    List<Throwable> errorList = Collections.synchronizedList(new ArrayList<Throwable>());
    for (int i = 0; i < threadCount; i++) {
        executor.execute(//from ww  w  .  j a  va 2  s  . com
                new ObjectTestThread<String>("Test thread " + i, "text/plain", String.class, errorList));
    }
    do {
        Thread.sleep(500);
    } while (executor.getActiveCount() > 0);
    if (!errorList.isEmpty()) {
        for (Throwable t : errorList)
            t.printStackTrace();
        Assert.fail("At least one thread failed");
    }

    // test with JAXB bean
    try {
        for (int i = 0; i < threadCount; i++) {
            executor.execute(new ObjectTestThread<AccessTokenPolicy>(
                    createTestTokenPolicy("Test thread " + i, "x.x.x." + i), "text/xml",
                    AccessTokenPolicy.class, errorList));
        }
        do {
            Thread.sleep(500);
        } while (executor.getActiveCount() > 0);
    } finally {
        executor.shutdown();
    }
    if (!errorList.isEmpty()) {
        for (Throwable t : errorList)
            t.printStackTrace();
        Assert.fail("At least one thread failed");
    }
}