List of usage examples for java.util.concurrent ThreadPoolExecutor getActiveCount
public int getActiveCount()
From source file:com.xerox.amazonws.sdb.Domain.java
/** * Gets attributes of items specified in the query string. This method threads off the * get requests and aggregates the responses. * * @param queryString the filter statement * @param listener class that will be notified when items are ready * @throws SDBException wraps checked exceptions *//* w w w . j a v a 2s .c o m*/ public void listItemsAttributes(String queryString, ItemListener listener) throws SDBException { ThreadPoolExecutor pool = getThreadPoolExecutor(); pool.setRejectedExecutionHandler(new RejectionHandler()); String nextToken = ""; Counter running = new Counter(0); do { try { QueryResult result = listItems(queryString, nextToken, 250); List<Item> items = result.getItemList(); for (Item i : items) { while (pool.getActiveCount() == pool.getMaximumPoolSize()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } } synchronized (running) { running.increment(); } pool.execute(new AttrWorker(i, running, null, listener)); Thread.yield(); } nextToken = result.getNextToken(); } catch (SDBException ex) { System.out.println("Query '" + queryString + "' Failure: "); ex.printStackTrace(); } } while (nextToken != null && nextToken.trim().length() > 0); while (true) { if (running.getValue() == 0) { break; } try { Thread.sleep(500); } catch (InterruptedException ex) { } } if (this.executor == null) { pool.shutdown(); } }
From source file:org.opennms.newts.gsod.ImportRunner.java
private Observable<Boolean> parMap(Observable<List<Sample>> samples, MetricRegistry metrics, Func1<List<Sample>, Boolean> insert) { final Timer waitTime = metrics.timer("wait-time"); @SuppressWarnings("serial") final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>( m_maxThreadQueueSize == 0 ? m_threadCount * 3 : m_maxThreadQueueSize) { @Override//from www . j a v a 2s. c o m public boolean offer(Runnable r) { try (Context time = waitTime.time()) { this.put(r); return true; } catch (InterruptedException e) { throw Exceptions.propagate(e); } } @Override public boolean add(Runnable r) { try (Context time = waitTime.time()) { this.put(r); return true; } catch (InterruptedException e) { throw Exceptions.propagate(e); } } }; final ThreadPoolExecutor executor = new ThreadPoolExecutor(m_threadCount, m_threadCount, 0L, TimeUnit.MILLISECONDS, workQueue); metrics.register("active-threads", new Gauge<Integer>() { @Override public Integer getValue() { return executor.getActiveCount(); } }); metrics.register("pool-size", new Gauge<Integer>() { @Override public Integer getValue() { return executor.getPoolSize(); } }); metrics.register("largest-pool-size", new Gauge<Integer>() { @Override public Integer getValue() { return executor.getLargestPoolSize(); } }); metrics.register("work-queue-size", new Gauge<Integer>() { @Override public Integer getValue() { return workQueue.size(); } }); return parMap(samples, executor, metrics, insert); }
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 {//from ww w . j a v a 2 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 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(/* w ww .j av a 2 s . co m*/ 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"); } }
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 {/*from ww w. j av a 2s . co 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: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 w w .j a v a 2s . co 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: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/* w w w.j a v a 2 s . com*/ 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:net.tjado.jcdbe.jcdbe.java
public static void main(String[] args) throws Exception { // print banner System.out.println("------------------------------------------"); System.out.println("| Java Central DataBase Engine v0.4 beta |"); System.out.println("------------------------------------------"); // path to the ini configuration String configPath = workingDir + "/config/jcdbe.ini"; // If the first CLI argument doesn't begin with "-" it must be a config file. // We need to do this, to have the possibility to specify other ini config files // The ini config file is necessary for the input/output classes, which again specify the // required CLI arguments if (args.length != 0 && !args[0].startsWith("-")) { configPath = args[0];/*from w ww . j a va 2s. co m*/ // remove first argument from CLI arguments array, so it won't be validated by CLI argument // parser args = Arrays.copyOfRange(args, 1, args.length); } // initialize the ini configuration to get required parameters Ini config = initConfig(configPath); // initialize Logger log.init(log4jPropertyFile); // setting jdbc property file DatabaseOracle.setPropertyFile(jdbcPropertyeFile); // declare the input/output classes Input input = (Input) Class.forName(inputClass).getDeclaredMethod("getInstance").invoke(null, (Object[]) null); Output output = (Output) Class.forName(outputClass).getDeclaredMethod("getInstance").invoke(null, (Object[]) null); // declare options and parser for the CLI arguments Options options = new Options(); CommandLineParser parser = new PosixParser(); // add "help" CLI argument options.addOption("h", "help", false, "print this usage information"); // add further CLI arguments by the input/output classes input.setCLI(options); output.setCLI(options); CommandLine cli = null; try { // parse the CLI arguments cli = parser.parse(options, args); if (cli.hasOption("help") || cli.getOptions().length == 0) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar jcdbe.jar [options]", options); System.exit(1); } } catch (ParseException e) { System.out.println(e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar jcdbe.jar [options]", options); System.exit(1); } // output engine config after initializing of Logger log.debug("[CONFIG] Working Directory: " + workingDir); log.debug("[CONFIG] Input class: " + inputClass); log.debug("[CONFIG] Output class: " + outputClass); log.debug("[CONFIG] JDBC URL prefix: " + jdbcPrefix); log.debug("[CONFIG] Java Library Path: " + System.getProperty("java.library.path")); log.debug("[CONFIG] Oracle SDU size: " + sduSize); log.debug("[CONFIG] Max. threads: " + threadMax); log.debug("[CONFIG] Max. running threads: " + threadRun); log.debug("[CONFIG] Thread idle timeout: " + threadTTL); log.debug("[CONFIG] Advanced Debugging: " + advDebugging); // validate Input arguments input.validateParameters(cli, config); // validate Output arguments output.validateParameters(cli, config); // start benchmark time measureTimeStart(); log.info("[INPUT] Initialization"); // run input init and check if it was successfull.... if (!input.init()) { log.fatal("Error during input init..."); System.exit(2); } log.info("[OUTPUT] Initialization"); // run output init and check if it was successfull.... if (!output.init()) { log.fatal("[OUTPUT] Error during output init..."); System.exit(3); } // init thread pool workQueue = new ArrayBlockingQueue<Runnable>(99999); ThreadPoolExecutor threads = new ThreadPoolExecutor(threadRun, threadMax, threadTTL, TimeUnit.SECONDS, workQueue); // get DatabaseList object which will manage all database infos (url, username, pw, status...) DatabaseList dbList = input.getDatabaseList(); if (dbList.size() == 0) { log.info("[QUEUE] database list is empty... nothing do to."); System.exit(1); } // get all SQL queries to execute // Integer = Query ID // String = SQL Text Map<Integer, String> queries = input.getQueries(); log.info("[QUEUE] Starting Threads"); // loop thru dbList to create & execute/queue all threads for (Integer id : dbList) { try { // create new runnable instance DatabaseThreadSlave slaveThread = new DatabaseThreadSlave(id, dbList, queries, output, jdbcPrefix, sduSize); // insert runnable instance into dbList dbList.setThread(id, slaveThread); // add runnable instance into thread pool queue threads.execute(slaveThread); } catch (Exception e) { advDebug(e); log.warn("Exception in thread-starter loop (DBID: " + id + "): " + e.getMessage()); } } // // waiting for all threads to complete // // the timeout handling will be done completely over JDBC // see docs for more information // while (!dbList.isFinished() || threads.getActiveCount() > 0) { Thread.sleep(500); } log.info("[QUEUE] Shutting down all threads"); threads.shutdown(); Thread.sleep(2000); log.info("[INPUT] close input..."); input.close(); log.info("[OUTPUT] close output..."); output.close(); // end time-benchmark and output measureTimeEnd(); // rc=0 System.exit(0); }
From source file:org.jumpmind.symmetric.service.impl.NodeCommunicationService.java
public int getAvailableThreads(CommunicationType communicationType) { ThreadPoolExecutor service = getExecutor(communicationType); return service.getMaximumPoolSize() - service.getActiveCount(); }
From source file:org.openhab.io.transport.modbus.internal.ModbusManagerImpl.java
private void logTaskQueueInfo() { synchronized (pollMonitorLogger) { ScheduledExecutorService scheduledThreadPoolExecutor = this.scheduledThreadPoolExecutor; if (scheduledThreadPoolExecutor == null) { return; }//from ww w .jav a 2s. c om // Avoid excessive spamming with queue monitor when many tasks are executed if (System.currentTimeMillis() - lastQueueMonitorLog < MONITOR_QUEUE_INTERVAL_MILLIS) { return; } lastQueueMonitorLog = System.currentTimeMillis(); pollMonitorLogger.trace("<POLL MONITOR>"); this.scheduledPollTasks.forEach((task, future) -> { pollMonitorLogger.trace( "POLL MONITOR: scheduled poll task. FC: {}, start {}, length {}, done: {}, canceled: {}, delay: {}. Full task {}", task.getRequest().getFunctionCode(), task.getRequest().getReference(), task.getRequest().getDataLength(), future.isDone(), future.isCancelled(), future.getDelay(TimeUnit.MILLISECONDS), task); }); if (scheduledThreadPoolExecutor instanceof ThreadPoolExecutor) { ThreadPoolExecutor executor = ((ThreadPoolExecutor) scheduledThreadPoolExecutor); pollMonitorLogger.trace( "POLL MONITOR: scheduledThreadPoolExecutor queue size: {}, remaining space {}. Active threads {}", executor.getQueue().size(), executor.getQueue().remainingCapacity(), executor.getActiveCount()); if (executor.getQueue().size() >= WARN_QUEUE_SIZE) { pollMonitorLogger.warn( "Many ({}) tasks queued in scheduledThreadPoolExecutor! This might be sign of bad design or bug in the binding code.", executor.getQueue().size()); } } pollMonitorLogger.trace("</POLL MONITOR>"); } }