List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
From source file:PoolSize.java
public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); System.out.println(executor.getPoolSize()); }
From source file:Main.java
public static void main(String[] args) throws Exception { BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(48); ThreadPoolExecutor testExecutor = new ThreadPoolExecutor(6, 10, 1, TimeUnit.SECONDS, blockingQueue); List<Future<String>> futures = new ArrayList<>(); for (int i = 0; i < 20; i++) { Future<String> testFuture = testExecutor.submit(new MyCallable(i)); futures.add(testFuture);// w ww . j a va2 s . c om } for (Future<String> testFuture : futures) { System.out.println("Output Returned is : " + testFuture.get()); } }
From source file:MainClass.java
public static void main(String[] args) { int nTasks = 5; long n = 1000L; int tpSize = 10; ThreadPoolExecutor tpe = new ThreadPoolExecutor(tpSize, tpSize, 50000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); Task[] tasks = new Task[nTasks]; for (int i = 0; i < nTasks; i++) { tasks[i] = new Task(n, "Task " + i); tpe.execute(tasks[i]);/* w w w. j a va 2s . co m*/ } tpe.shutdown(); }
From source file:Test.java
public static void main(String[] args) throws InterruptedException { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); for (int i = 0; i < 10; i++) { final int localI = i; queue.add(new Runnable() { public void run() { doExpensiveOperation(localI); }// w w w. j a va 2 s .com }); } ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1000, TimeUnit.MILLISECONDS, queue); executor.prestartAllCoreThreads(); executor.shutdown(); executor.awaitTermination(100000, TimeUnit.SECONDS); }
From source file:ReplaceWorker.java
public static void main(String[] args) { map.put("key", 1); Main test = new Main(); ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 10, 100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000)); for (int i = 0; i < 100; i++) { threadPool.submit(new MergeWorker()); }/*from w ww.j a va 2s . c o m*/ awaitTermination(threadPool); System.out.println(test.map.get("key")); map.put("key", 1); threadPool = new ThreadPoolExecutor(10, 10, 100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000)); for (int i = 0; i < 100; i++) { threadPool.submit(new ReplaceWorker()); } awaitTermination(threadPool); System.out.println(test.map.get("key")); }
From source file:httpscheduler.HttpScheduler.java
/** * @param args the command line arguments * @throws java.lang.Exception//from w w w .j a v a2s.c om */ public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Invalid command line parameters for worker"); System.exit(-1); } int fixedExecutorSize = 4; //Creating fixed size executor ThreadPoolExecutor taskCommExecutor = new ThreadPoolExecutor(fixedExecutorSize, fixedExecutorSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); // Used for late binding JobMap jobMap = new JobMap(); // Set port number int port = Integer.parseInt(args[0]); // Set worker mode String mode = args[1].substring(2); // Set up the HTTP protocol processor HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate()) .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl()) .build(); // Set up request handlers UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); // Different handlers for late binding and generic cases if (mode.equals("late")) reqistry.register("*", new LateBindingRequestHandler(taskCommExecutor, jobMap)); else reqistry.register("*", new GenericRequestHandler(taskCommExecutor, mode)); // Set up the HTTP service HttpService httpService = new HttpService(httpproc, reqistry); SSLServerSocketFactory sf = null; // create a thread to listen for possible client available connections Thread t; if (mode.equals("late")) t = new LateBindingRequestListenerThread(port, httpService, sf); else t = new GenericRequestListenerThread(port, httpService, sf); System.out.println("Request Listener Thread created"); t.setDaemon(false); t.start(); // main thread should wait for the listener to exit before shutdown the // task executor pool t.join(); // shutdown task executor pool and wait for any taskCommExecutor thread // still running taskCommExecutor.shutdown(); while (!taskCommExecutor.isTerminated()) { } System.out.println("Finished all task communication executor threads"); System.out.println("Finished all tasks"); }
From source file:com.stehno.sanctuary.core.Sanctuary.java
public static void main(String[] args) throws Exception { log.info("Starting run..."); JdbcTemplate jdbcTemplate = new JdbcTemplate(createDataSource()); // FIXME: AndroidLocalStore LocalStore localStore = new JdbcLocalStore(jdbcTemplate); localStore.init();/*from w w w.j a v a 2s . c om*/ // FIXME: FileSystemRemoteStore, S3RemoteStore, AndroidS3LocalStore? // FIXME: remote keys RemoteStore remoteStore = new S3RemoteStore("yourkey", "yourotherkey"); remoteStore.init(); DirectoryScanner scanner = new DefaultDirectoryScanner(localStore); ExecutorService executor = new ThreadPoolExecutor(2, 2, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>()); FileArchiver archiver = new DefaultFileArchiver(executor, localStore, remoteStore); ChangeSet changeSet = scanner.scanDirectory(WORKING_DIR); // FIXME: allow review of changeset here (confirm/cancel) log.info("Changes: " + changeSet); MessageSet messageSet = archiver.archiveChanges(changeSet); // FIXME: allow review of messages here log.info("Messages" + messageSet); remoteStore.destroy(); localStore.destroy(); }
From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java
public static void main(String[] args) throws Exception { // Let's use some colors :) // AnsiConsole.systemInstall(); CommandLineParser cliParser = new BasicParser(); CommandLine cli = null;// w w w . j a v a 2 s.co m try { cli = cliParser.parse(OPTIONS, args); } catch (ParseException e) { printHelp(); } if (!cli.hasOption("s")) { printHelp(); } String sourcePattern; if (cli.hasOption("p")) { sourcePattern = cli.getOptionValue("p"); } else { sourcePattern = DEFAULT_SOURCE_PATTERN; } String defaultAnswer; if (cli.hasOption("default-answer")) { defaultAnswer = cli.getOptionValue("default-answer"); } else { defaultAnswer = DEFAULT_DEFAULT_PROMPT_ANSWER; } boolean defaultAnswerYes = defaultAnswer.equalsIgnoreCase("y"); boolean quiet = cli.hasOption("q"); boolean testWrite = cli.hasOption("t"); Path sourceDirectory = Paths.get(cli.getOptionValue("s")).toAbsolutePath(); // Since we use IO we will have some blocking threads hanging around int threadCount = Runtime.getRuntime().availableProcessors() * 2; ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); BlockingQueue<WidgetVarLocation> foundUsages = new LinkedBlockingQueue<>(); BlockingQueue<WidgetVarLocation> unusedOrAmbiguous = new LinkedBlockingQueue<>(); BlockingQueue<WidgetVarLocation> skippedUsages = new LinkedBlockingQueue<>(); List<Future<?>> futures = new ArrayList<>(); findWidgetVars(sourceDirectory, sourcePattern, threadPool).forEach(widgetVarLocation -> { // We can't really find usages of widget vars that use EL expressions :( if (widgetVarLocation.widgetVar.contains("#")) { unusedOrAmbiguous.add(widgetVarLocation); return; } try { FileActionVisitor visitor = new FileActionVisitor(sourceDirectory, sourcePattern, sourceFile -> futures.add(threadPool.submit((Callable<?>) () -> { findWidgetVarUsages(sourceFile, widgetVarLocation, foundUsages, skippedUsages, unusedOrAmbiguous); return null; }))); Files.walkFileTree(sourceDirectory, visitor); } catch (IOException ex) { throw new RuntimeException(ex); } }); awaitAll(futures); new TreeSet<>(skippedUsages).forEach(widgetUsage -> { int startIndex = widgetUsage.columnNr; int endIndex = startIndex + widgetUsage.widgetVar.length(); String relativePath = widgetUsage.location.toAbsolutePath().toString() .substring(sourceDirectory.toString().length()); String previous = replace(widgetUsage.line, startIndex, endIndex, Ansi.ansi().bold().fg(Ansi.Color.RED).a(widgetUsage.widgetVar).reset().toString()); System.out.println("Skipped " + relativePath + " at line " + widgetUsage.lineNr + " and col " + widgetUsage.columnNr + " for widgetVar '" + widgetUsage.widgetVar + "'"); System.out.println("\t" + previous); }); Map<WidgetVarLocation, List<WidgetVarLocation>> written = new HashMap<>(); new TreeSet<>(foundUsages).forEach(widgetUsage -> { WidgetVarLocation key = new WidgetVarLocation(null, widgetUsage.location, widgetUsage.lineNr, -1, null); List<WidgetVarLocation> writtenList = written.get(key); int existing = writtenList == null ? 0 : writtenList.size(); int startIndex = widgetUsage.columnNr; int endIndex = startIndex + widgetUsage.widgetVar.length(); String relativePath = widgetUsage.location.toAbsolutePath().toString() .substring(sourceDirectory.toString().length()); String next = replace(widgetUsage.line, startIndex, endIndex, Ansi.ansi().bold().fg(Ansi.Color.RED) .a("PF('" + widgetUsage.widgetVar + "')").reset().toString()); System.out .println(relativePath + " at line " + widgetUsage.lineNr + " and col " + widgetUsage.columnNr); System.out.println("\t" + next); System.out.print("Replace (Y/N)? [" + (defaultAnswerYes ? "Y" : "N") + "]: "); String input; if (quiet) { input = ""; System.out.println(); } else { try { do { input = in.readLine(); } while (input != null && !input.isEmpty() && !"y".equalsIgnoreCase(input) && !"n".equalsIgnoreCase(input)); } catch (IOException ex) { throw new RuntimeException(ex); } } if (input == null) { System.out.println("Aborted!"); } else if (input.isEmpty() && defaultAnswerYes || !input.isEmpty() && !"n".equalsIgnoreCase(input)) { System.out.println("Replaced!"); System.out.print("\t"); if (writtenList == null) { writtenList = new ArrayList<>(); written.put(key, writtenList); } writtenList.add(widgetUsage); List<String> lines; try { lines = Files.readAllLines(widgetUsage.location); } catch (IOException ex) { throw new RuntimeException(ex); } try (OutputStream os = testWrite ? new ByteArrayOutputStream() : Files.newOutputStream(widgetUsage.location); PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) { String line; for (int i = 0; i < lines.size(); i++) { int lineNr = i + 1; line = lines.get(i); if (lineNr == widgetUsage.lineNr) { int begin = widgetUsage.columnNr + (testWrite ? 0 : existing * 6); int end = begin + widgetUsage.widgetVar.length(); String newLine = replace(line, begin, end, "PF('" + widgetUsage.widgetVar + "')", false); if (testWrite) { System.out.println(newLine); } else { pw.println(newLine); } } else { if (!testWrite) { pw.println(line); } } } } catch (IOException ex) { throw new RuntimeException(ex); } } else { System.out.println("Skipped!"); } }); new TreeSet<>(unusedOrAmbiguous).forEach(widgetUsage -> { int startIndex = widgetUsage.columnNr; int endIndex = startIndex + widgetUsage.widgetVar.length(); String relativePath = widgetUsage.location.toAbsolutePath().toString() .substring(sourceDirectory.toString().length()); String previous = replace(widgetUsage.line, startIndex, endIndex, Ansi.ansi().bold().fg(Ansi.Color.RED).a(widgetUsage.widgetVar).reset().toString()); System.out.println("Skipped unused or ambiguous " + relativePath + " at line " + widgetUsage.lineNr + " and col " + widgetUsage.columnNr); System.out.println("\t" + previous); }); threadPool.shutdown(); }
From source file:com.betfair.cougar.test.socket.app.SocketCompatibilityTestingApp.java
public static void main(String[] args) throws Exception { Parser parser = new PosixParser(); Options options = new Options(); options.addOption("r", "repo", true, "Repository type to search: local|central"); options.addOption("c", "client-concurrency", true, "Max threads to allow each client tester to run tests, defaults to 10"); options.addOption("t", "test-concurrency", true, "Max client testers to run concurrently, defaults to 5"); options.addOption("m", "max-time", true, "Max time (in minutes) to allow tests to complete, defaults to 10"); options.addOption("v", "version", false, "Print version and exit"); options.addOption("h", "help", false, "This help text"); CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption("h")) { System.out.println(options); System.exit(0);//from w ww . j a va2 s . c o m } if (commandLine.hasOption("v")) { System.out.println("How the hell should I know?"); System.exit(0); } // 1. Find all testers in given repos List<RepoSearcher> repoSearchers = new ArrayList<>(); for (String repo : commandLine.getOptionValues("r")) { if ("local".equals(repo.toLowerCase())) { repoSearchers.add(new LocalRepoSearcher()); } else if ("central".equals(repo.toLowerCase())) { repoSearchers.add(new CentralRepoSearcher()); } else { System.err.println("Unrecognized repo: " + repo); System.err.println(options); System.exit(1); } } int clientConcurrency = 10; if (commandLine.hasOption("c")) { try { clientConcurrency = Integer.parseInt(commandLine.getOptionValue("c")); } catch (NumberFormatException nfe) { System.err.println( "client-concurrency is not a valid integer: '" + commandLine.getOptionValue("c") + "'"); System.exit(1); } } int testConcurrency = 5; if (commandLine.hasOption("t")) { try { testConcurrency = Integer.parseInt(commandLine.getOptionValue("t")); } catch (NumberFormatException nfe) { System.err.println( "test-concurrency is not a valid integer: '" + commandLine.getOptionValue("t") + "'"); System.exit(1); } } int maxMinutes = 10; if (commandLine.hasOption("m")) { try { maxMinutes = Integer.parseInt(commandLine.getOptionValue("m")); } catch (NumberFormatException nfe) { System.err.println("max-time is not a valid integer: '" + commandLine.getOptionValue("m") + "'"); System.exit(1); } } Properties clientProps = new Properties(); clientProps.setProperty("client.concurrency", String.valueOf(clientConcurrency)); File baseRunDir = new File(System.getProperty("user.dir") + "/run"); baseRunDir.mkdirs(); File tmpDir = new File(baseRunDir, "jars"); tmpDir.mkdirs(); List<ServerRunner> serverRunners = new ArrayList<>(); List<ClientRunner> clientRunners = new ArrayList<>(); for (RepoSearcher searcher : repoSearchers) { List<File> jars = searcher.findAndCache(tmpDir); for (File f : jars) { ServerRunner serverRunner = new ServerRunner(f, baseRunDir); System.out.println("Found tester: " + serverRunner.getVersion()); serverRunners.add(serverRunner); clientRunners.add(new ClientRunner(f, baseRunDir, clientProps)); } } // 2. Start servers and collect ports System.out.println(); System.out.println("Starting " + serverRunners.size() + " servers..."); for (ServerRunner server : serverRunners) { server.startServer(); } System.out.println(); List<TestCombo> tests = new ArrayList<>(serverRunners.size() * clientRunners.size()); for (ServerRunner server : serverRunners) { for (ClientRunner client : clientRunners) { tests.add(new TestCombo(server, client)); } } System.out.println("Enqueued " + tests.size() + " test combos to run..."); long startTime = System.currentTimeMillis(); // 3. Run every client against every server, collecting results BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(serverRunners.size() * clientRunners.size()); ThreadPoolExecutor service = new ThreadPoolExecutor(testConcurrency, testConcurrency, 5000, TimeUnit.MILLISECONDS, workQueue); service.prestartAllCoreThreads(); workQueue.addAll(tests); while (!workQueue.isEmpty()) { Thread.sleep(1000); } service.shutdown(); service.awaitTermination(maxMinutes, TimeUnit.MINUTES); long endTime = System.currentTimeMillis(); long totalTimeSecs = Math.round((endTime - startTime) / 1000.0); for (ServerRunner server : serverRunners) { server.shutdownServer(); } System.out.println(); System.out.println("======="); System.out.println("Results"); System.out.println("-------"); // print a summary int totalTests = 0; int totalSuccess = 0; for (TestCombo combo : tests) { String clientVer = combo.getClientVersion(); String serverVer = combo.getServerVersion(); String results = combo.getClientResults(); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode node = mapper.reader().readTree(results); JsonNode resultsArray = node.get("results"); int numTests = resultsArray.size(); int numSuccess = 0; for (int i = 0; i < numTests; i++) { if ("success".equals(resultsArray.get(i).get("result").asText())) { numSuccess++; } } totalSuccess += numSuccess; totalTests += numTests; System.out.println(clientVer + "/" + serverVer + ": " + numSuccess + "/" + numTests + " succeeded - took " + String.format("%2f", combo.getRunningTime()) + " seconds"); } System.out.println("-------"); System.out.println( "Overall: " + totalSuccess + "/" + totalTests + " succeeded - took " + totalTimeSecs + " seconds"); FileWriter out = new FileWriter("results.json"); PrintWriter pw = new PrintWriter(out); // 4. Output full results pw.println("{\n \"results\": ["); for (TestCombo combo : tests) { combo.emitResults(pw, " "); } pw.println(" ],"); pw.println(" \"servers\": ["); for (ServerRunner server : serverRunners) { server.emitInfo(pw, " "); } pw.println(" ],"); pw.close(); }
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 ww w . j a v a 2 s. c o 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); }