List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:SimpExec.java
public static void main(String args[]) { CountDownLatch cdl = new CountDownLatch(5); CountDownLatch cdl2 = new CountDownLatch(5); CountDownLatch cdl3 = new CountDownLatch(5); CountDownLatch cdl4 = new CountDownLatch(5); ExecutorService es = Executors.newFixedThreadPool(2); es.execute(new MyThread(cdl, "A")); es.execute(new MyThread(cdl2, "B")); es.execute(new MyThread(cdl3, "C")); es.execute(new MyThread(cdl4, "D")); try {//from ww w. j a v a 2 s . c om cdl.await(); cdl2.await(); cdl3.await(); cdl4.await(); } catch (InterruptedException exc) { System.out.println(exc); } es.shutdown(); }
From source file:Main.java
public static void main(final String[] args) throws Exception { Random RND = new Random(); ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); List<Future<String>> results = new ArrayList<>(10); for (int i = 0; i < 10; i++) { results.add(es.submit(new TimeSliceTask(RND.nextInt(10), TimeUnit.SECONDS))); }/*ww w .j a v a2 s.c o m*/ es.shutdown(); while (!results.isEmpty()) { Iterator<Future<String>> i = results.iterator(); while (i.hasNext()) { Future<String> f = i.next(); if (f.isDone()) { System.out.println(f.get()); i.remove(); } } } }
From source file:WordLengthCallable.java
public static void main(String[] args) throws Exception { int THREAD_COUNT = 4; ExecutorService execService = Executors.newFixedThreadPool(THREAD_COUNT); CompletionService<Integer> completionService = new ExecutorCompletionService<>(execService); for (int i = 0; i < THREAD_COUNT; i++) { completionService.submit(new WordLengthCallable()); }/*from ww w . j a va2s .c om*/ execService.shutdown(); while (!execService.isTerminated()) { int result = completionService.take().get().intValue(); System.out.println("Result is: " + result); } Thread.sleep(1000); System.out.println("done!"); }
From source file:kymr.github.io.future.LoadTest.java
public static void main(String[] args) throws InterruptedException { ExecutorService es = Executors.newFixedThreadPool(100); RestTemplate rt = new RestTemplate(); String url = "http://localhost:8080/dr"; StopWatch main = new StopWatch(); main.start();//from w w w .j av a2 s. c o m for (int i = 0; i < 100; i++) { es.execute(() -> { int idx = counter.addAndGet(1); log.info("Thread {}", idx); StopWatch sw = new StopWatch(); sw.start(); rt.getForObject(url, String.class); sw.stop(); log.info("Elapsed: {} -> {}", idx, sw.getTotalTimeSeconds()); }); } es.shutdown(); es.awaitTermination(100, TimeUnit.SECONDS); main.stop(); log.info("Total: {}", main.getTotalTimeSeconds()); }
From source file:CallableTask.java
public static void main(String[] args) throws Exception { // Get an executor with three threads in its thread pool ExecutorService exec = Executors.newFixedThreadPool(3); CallableTask task = new CallableTask(1); // Submit the callable task to executor Future<Integer> submittedTask = exec.submit(task); Integer result = submittedTask.get(); System.out.println("Task's total sleep time: " + result + " seconds"); exec.shutdown();/* ww w. jav a 2 s .c o m*/ }
From source file:RunnableTask.java
public static void main(String[] args) { final int THREAD_COUNT = 3; final int LOOP_COUNT = 3; final int TASK_COUNT = 5; // Get an executor with three threads in its thread pool ExecutorService exec = Executors.newFixedThreadPool(THREAD_COUNT); // Create five tasks and submit them to the executor for (int i = 1; i <= TASK_COUNT; i++) { RunnableTask task = new RunnableTask(i, LOOP_COUNT); exec.submit(task);// w w w .j a va 2 s. c o m } exec.shutdown(); }
From source file:com.apress.prospringintegration.customadapters.inbound.eventdriven.fsmon.DirectoryMonitorClient.java
public static void main(String[] args) throws Throwable { File[] files = { new File(new File(SystemUtils.getUserHome(), "Desktop"), "test2"), new File(new File(SystemUtils.getUserHome(), "Desktop"), "test1") }; Executor ex = Executors.newFixedThreadPool(10); final LinuxInotifyDirectoryMonitor monitor = new LinuxInotifyDirectoryMonitor(); monitor.setExecutor(ex);//from ww w. j a v a 2s. c o m monitor.afterPropertiesSet(); final DirectoryMonitor.FileAddedListener fileAddedListener = new DirectoryMonitor.FileAddedListener() { @Override public void fileAdded(File dir, String fn) { System.out .println("A new file in " + dir.getAbsolutePath() + " called " + fn + " has been noticed"); } }; for (File f : files) { monitor.monitor(f, fileAddedListener); } }
From source file:example.rhino.DynamicScopesWithHandlebars.java
public static void main(String[] args) { Context cx = Context.enter(); try {//from ww w .ja v a 2 s .c om String source = handlebars(); Script script = cx.compileString(source, "handlebars", 1, null); System.out.println("Running the script in a single thread"); runScripts(cx, script, Executors.newSingleThreadExecutor()); int nThreads = Runtime.getRuntime().availableProcessors(); System.out.format("Running the script in %d thread\n", nThreads); runScripts(cx, script, Executors.newFixedThreadPool(nThreads)); } catch (IOException e) { System.err.println("No handlebars file found"); } finally { Context.exit(); } }
From source file:MyResult.java
public static void main(String[] args) throws Exception { // Get an executor with three threads in its thread pool ExecutorService exec = Executors.newFixedThreadPool(3); // Completed task returns an object of the TaskResult class ExecutorCompletionService<MyResult> completionService = new ExecutorCompletionService<>(exec); for (int i = 1; i <= 5; i++) { SleepingTask task = new SleepingTask(i, 3); completionService.submit(task);//from w w w. ja v a2 s . c o m } for (int i = 1; i <= 5; i++) { Future<MyResult> completedTask = completionService.take(); MyResult result = completedTask.get(); System.out.println("Completed a task - " + result); } exec.shutdown(); }
From source file:locking.LockingExample.java
public static void main(String[] args) throws Exception { // all of the useful sample code is in ExampleClientThatLocks.java // FakeLimitedResource simulates some external resource that can only be access by one process at a time final FakeLimitedResource resource = new FakeLimitedResource(); ExecutorService service = Executors.newFixedThreadPool(QTY); final TestingServer server = new TestingServer(); try {//from ww w .j a v a 2 s. c o m for (int i = 0; i < QTY; ++i) { final int index = i; Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); try { client.start(); ExampleClientThatLocks example = new ExampleClientThatLocks(client, PATH, resource, "Client " + index); for (int j = 0; j < REPETITIONS; ++j) { example.doWork(10, TimeUnit.SECONDS); } } catch (Throwable e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(client); } return null; } }; service.submit(task); } service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } finally { IOUtils.closeQuietly(server); } }