List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:com.sangupta.httptools.DownloadUrlCommand.java
@Override public void run() { File file = new File(this.urlFile); if (file == null || !file.exists()) { System.out.println("URL file cannot be found."); return;// w w w . j a v a 2 s. c om } if (!file.isFile()) { System.out.println("URL file does not represent a valid file."); return; } if (this.numThreads <= 0 || this.numThreads > 50) { System.out.println("Number of assigned threads should be between 1 and 50"); return; } outputDir = new File(this.outputFolder); if (outputDir.exists() && !outputDir.isDirectory()) { System.out.println("Output folder does not represent a valid directory"); return; } if (!outputDir.exists()) { outputDir.mkdirs(); } // try and parse and read all URLs int line = 1; try { LineIterator iterator = FileUtils.lineIterator(file); while (iterator.hasNext()) { ++line; String readURL = iterator.next(); createURLTask(readURL); } } catch (IOException e) { System.out.println("Unable to read URLs from the file at line: " + line); return; } // all set - create number of threads // and start fetching ExecutorService service = Executors.newFixedThreadPool(this.numThreads); final long start = System.currentTimeMillis(); for (Runnable runnable : this.downloadTasks) { service.submit(runnable); } // intialize some variables this.numTasks = this.downloadTasks.size(); this.downloadTasks.clear(); if (this.numTasks > 1000) { this.splitFolders = true; } // shutdown shutdownAndAwaitTermination(service); final long end = System.currentTimeMillis(); // everything done System.out.println(this.downloadTasks.size() + " urls downloaded in " + (end - start) + " millis."); }
From source file:com.alibaba.otter.shared.arbitrate.zookeeper.DistributedLockTest.java
@Test protected void test_lock() { ExecutorService exeucotr = Executors.newCachedThreadPool(); final int count = 50; final CountDownLatch latch = new CountDownLatch(count); final DistributedLock[] nodes = new DistributedLock[count]; for (int i = 0; i < count; i++) { final DistributedLock node = new DistributedLock(dir); nodes[i] = node;// w ww . j av a 2 s . c o m exeucotr.submit(new Runnable() { public void run() { try { node.lock(); Thread.sleep(100 + RandomUtils.nextInt(100)); System.out.println("id: " + node.getId() + " is leader: " + node.isOwner()); } catch (InterruptedException e) { want.fail(); } catch (KeeperException e) { want.fail(); } finally { latch.countDown(); try { node.unlock(); } catch (KeeperException e) { want.fail(); } } } }); } try { latch.await(); } catch (InterruptedException e) { want.fail(); } exeucotr.shutdown(); }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java
@Test public void test2Clients() throws Exception { CuratorFramework client1 = null;/*from w w w .j a v a 2 s . c o m*/ CuratorFramework client2 = null; try { client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client1.start(); client2.start(); final InterProcessLock mutexForClient1 = makeLock(client1); final InterProcessLock mutexForClient2 = makeLock(client2); final CountDownLatch latchForClient1 = new CountDownLatch(1); final CountDownLatch latchForClient2 = new CountDownLatch(1); final CountDownLatch acquiredLatchForClient1 = new CountDownLatch(1); final CountDownLatch acquiredLatchForClient2 = new CountDownLatch(1); final AtomicReference<Exception> exceptionRef = new AtomicReference<Exception>(); ExecutorService service = Executors.newCachedThreadPool(); Future<Object> future1 = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { try { mutexForClient1.acquire(10, TimeUnit.SECONDS); acquiredLatchForClient1.countDown(); latchForClient1.await(10, TimeUnit.SECONDS); mutexForClient1.release(); } catch (Exception e) { exceptionRef.set(e); } return null; } }); Future<Object> future2 = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { try { mutexForClient2.acquire(10, TimeUnit.SECONDS); acquiredLatchForClient2.countDown(); latchForClient2.await(10, TimeUnit.SECONDS); mutexForClient2.release(); } catch (Exception e) { exceptionRef.set(e); } return null; } }); while (!mutexForClient1.isAcquiredInThisProcess() && !mutexForClient2.isAcquiredInThisProcess()) { Thread.sleep(1000); Assert.assertFalse(future1.isDone() && future2.isDone()); } Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() != mutexForClient2.isAcquiredInThisProcess()); Thread.sleep(1000); Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() || mutexForClient2.isAcquiredInThisProcess()); Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() != mutexForClient2.isAcquiredInThisProcess()); Exception exception = exceptionRef.get(); if (exception != null) { throw exception; } if (mutexForClient1.isAcquiredInThisProcess()) { latchForClient1.countDown(); Assert.assertTrue(acquiredLatchForClient2.await(10, TimeUnit.SECONDS)); Assert.assertTrue(mutexForClient2.isAcquiredInThisProcess()); } else { latchForClient2.countDown(); Assert.assertTrue(acquiredLatchForClient1.await(10, TimeUnit.SECONDS)); Assert.assertTrue(mutexForClient1.isAcquiredInThisProcess()); } } finally { IOUtils.closeQuietly(client1); IOUtils.closeQuietly(client2); } }
From source file:com.alibaba.otter.shared.arbitrate.zookeeper.DistributedLockTest.java
@Test protected void test_try_lock() { ExecutorService exeucotr = Executors.newCachedThreadPool(); final int count = 50; final CountDownLatch latch = new CountDownLatch(count); final DistributedLock[] nodes = new DistributedLock[count]; for (int i = 0; i < count; i++) { final DistributedLock node = new DistributedLock(dir); nodes[i] = node;/*from www . j a v a2s. c o m*/ exeucotr.submit(new Runnable() { public void run() { try { while (node.tryLock() == false) { Thread.sleep(100 + RandomUtils.nextInt(100)); } System.out.println("id: " + node.getId() + " is leader: " + node.isOwner()); } catch (InterruptedException e) { want.fail(); } catch (KeeperException e) { want.fail(); } finally { latch.countDown(); try { node.unlock(); } catch (KeeperException e) { want.fail(); } } } }); } try { latch.await(); } catch (InterruptedException e) { want.fail(); } exeucotr.shutdown(); }
From source file:eu.freme.bpt.service.EPublishing.java
@Override public void run(FailurePolicy failurePolicy, int nrThreads, Callback callback) { logger.info("Running service EPublishing"); ExecutorService executorService = Executors.newFixedThreadPool(nrThreads); Unirest.setTimeouts(30000, 300000); // TODO: configurable? // iterate over zip files File[] zipFiles = inputDirectory.listFiles((dir, name) -> { return name.endsWith(".zip"); });//from ww w . j a v a 2s . co m for (final File zipFile : zipFiles) { executorService.submit(() -> { File jsonFile = new File(zipFile.getParentFile(), zipFile.getName().replace(".zip", ".json")); if (jsonFile.exists()) { File outputFile = new File(outputDirectory, zipFile.getName().replace(".zip", ".epub")); try { String json = new String(Files.readAllBytes(jsonFile.toPath()), StandardCharsets.UTF_8); HttpResponse<InputStream> response = Unirest.post(endpoint).field("htmlZip", zipFile) .field("metadata", json).asBinary(); if (response.getStatus() == 200) { logger.debug("Request alright."); try (InputStream responseInput = response.getBody(); OutputStream out = new FileOutputStream(outputFile)) { IOUtils.copy(responseInput, out); callback.onTaskComplete(zipFile, outputFile); } //Files.write(outputFile.toPath(), IOUtils.toByteArray()) } else { String body = IOUtils.toString(response.getBody()); String msg = "Error response from service " + endpoint + ": Status " + response.getStatus() + ": " + response.getStatusText() + " - " + body; logger.error(msg); callback.onTaskFails(zipFile, outputFile, msg); if (!failurePolicy.check()) { System.exit(3); } } } catch (IOException e) { logger.error("Error while reading json file: {}", jsonFile, e); callback.onTaskFails(zipFile, outputFile, "Error while reading json file: " + jsonFile + " " + e.getMessage()); if (!failurePolicy.check()) { System.exit(3); } } catch (UnirestException e) { logger.error("Request to {} failed." + endpoint, e); callback.onTaskFails(zipFile, outputFile, "Request to " + endpoint + " failed. " + e.getMessage()); if (!failurePolicy.check()) { System.exit(3); } } } else { String msg = "Missing metatada file " + jsonFile + " for input file " + zipFile; logger.error(msg); callback.onTaskFails(zipFile, null, msg); if (!failurePolicy.check()) { System.exit(3); } } }); } }
From source file:com.stb.async.ParallelExecutionProcess.java
/** * * @param processList/*from www. jav a2 s .co m*/ */ public void initiateDecode(List processList) { Date startTime = new java.util.Date(); System.out.println("Start Work" + startTime); ExecutorService es = Executors.newFixedThreadPool(10); Collections.sort(processList, new ProcessCompare()); List<Future> futures = new ArrayList<>(); for (Iterator it = processList.iterator(); it.hasNext();) { Process e = (Process) it.next(); workerId = processList.indexOf(e); System.out.println("* Start Decode process " + processList.indexOf(e)); futures.add(es.submit(() -> { new DecodedSTBProcesses((Process) processList.get(ParallelExecutionProcess.workerId)).doWork(); return null; })); } es.shutdown(); System.out.println( "... The Process is under execution! Using CPU core which are available, wait while work is being done...."); int ctr = 0; for (Future future : futures) { try { future.get(); // blocking call, explicitly waiting for the response from a specific task, not necessarily the first task that is completed System.out.println("** Response of process " + ++ctr + " is in."); } catch (InterruptedException | ExecutionException e) { } } Date endTime = new java.util.Date(); System.out.println("End work at " + endTime); System.out.println("Total decoding took " + new Double(0.001 * (endTime.getTime() - startTime.getTime())) + " seconds"); System.exit(0); }
From source file:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java
@Test public void testRestoreFromBinlog() throws Exception { MysqlReplicationApplier applier = null; try (Connection conn = DriverManager.getConnection(jdbc, "root", "")) { // Write some rows, so we have binlog entries PreparedStatement pstmt = conn.prepareStatement("INSERT INTO simple VALUES(?, ?)"); for (int i = 0; i < 10; i++) { pstmt.setInt(1, i);/* ww w . j a va 2s . c o m*/ pstmt.setInt(2, i); pstmt.execute(); } // Copy the binlog somewhere Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("SHOW BINARY LOGS"); rset.next(); String binlogName = rset.getString("Log_name"); rset = stmt.executeQuery("SELECT @@datadir"); rset.next(); String dataDir = rset.getString("@@datadir"); File copyFile = new File(System.getProperty("java.io.tmpdir"), TestMysqlReplicationApplier.class.getName()); FileUtils.copyFile(new File(dataDir + binlogName), copyFile); // Clear everything in MySQL resetMysql(); // Get input stream, skipping and checking binlog magic number InputStream inputStream = new FileInputStream(copyFile); byte[] magic = new byte[MySQLConstants.BINLOG_MAGIC.length]; int bytesRead = inputStream.read(magic); Assert.assertEquals(bytesRead, MySQLConstants.BINLOG_MAGIC.length); Assert.assertTrue(CodecUtils.equals(magic, MySQLConstants.BINLOG_MAGIC)); // Restore from binlog PoolingDataSource<PoolableConnection> dataSource = getDataSource(); applier = new MysqlReplicationApplier(inputStream, dataSource); ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; } }); executorService.submit(applier); // Poll until we have restored long startTime = System.currentTimeMillis(); long currentTime = startTime; do { stmt = conn.createStatement(); rset = stmt.executeQuery("SELECT COUNT(*) FROM test.simple"); rset.next(); long count = rset.getLong(1); if (count == 10) { return; } Thread.sleep(1000); currentTime = System.currentTimeMillis(); } while (currentTime - startTime < 10000); } finally { if (applier != null) { applier.shutdown(); } } Assert.fail("Timed out when polling"); }
From source file:werecloud.api.bean.VMWareMacCache.java
private void initializeMACCache() throws RemoteException, InterruptedException { long start = System.currentTimeMillis(); ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine"); ExecutorService exec = Executors.newFixedThreadPool(loadThreads); try {// w ww . j a v a2 s . c o m for (final ManagedEntity me : mes) { exec.submit(new Runnable() { @Override public void run() { VirtualMachine vm = (VirtualMachine) me; addVirtualMachine(vm); } }); } } finally { exec.shutdown(); } exec.awaitTermination(1, TimeUnit.HOURS); logger.info("{} MAC addresses added and took {} milliseconds.", macAddresses.size(), System.currentTimeMillis() - start); }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphore.java
@Test public void testThreads() throws Exception { final int THREAD_QTY = 10; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/* www . j a v a 2 s. c o m*/ try { final InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", 1); ExecutorService service = Executors.newFixedThreadPool(THREAD_QTY); for (int i = 0; i < THREAD_QTY; ++i) { service.submit(new Callable<Object>() { @Override public Object call() throws Exception { Lease lease = semaphore.acquire(); try { Thread.sleep(1); } finally { lease.close(); } return null; } }); } service.shutdown(); Assert.assertTrue(service.awaitTermination(10, TimeUnit.SECONDS)); } finally { client.close(); } }
From source file:io.undertow.server.handlers.JDBCLogDatabaseTestCase.java
@Test public void testLogLotsOfThreadsToDatabase() throws IOException, InterruptedException, ExecutionException, SQLException { JDBCLogHandler logHandler = new JDBCLogHandler(HELLO_HANDLER, DefaultServer.getWorker(), "combined", ds); CompletionLatchHandler latchHandler; DefaultServer//from w w w .ja v a 2 s .c o m .setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, logHandler)); ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); try { final List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < NUM_THREADS; ++i) { final int threadNo = i; futures.add(executor.submit(new Runnable() { @Override public void run() { TestHttpClient client = new TestHttpClient(); try { for (int i = 0; i < NUM_REQUESTS; ++i) { HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path"); HttpResponse result = client.execute(get); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertEquals("Hello", response); } } catch (IOException e) { throw new RuntimeException(e); } finally { client.getConnectionManager().shutdown(); } } })); } for (Future<?> future : futures) { future.get(); } } finally { executor.shutdown(); } latchHandler.await(); logHandler.awaitWrittenForTest(); Connection conn = null; Statement statement = null; try { conn = ds.getConnection(); statement = conn.createStatement(); ResultSet resultDatabase = conn.createStatement().executeQuery("SELECT COUNT(*) FROM PUBLIC.ACCESS;"); resultDatabase.next(); Assert.assertEquals(resultDatabase.getInt(1), NUM_REQUESTS * NUM_THREADS); } finally { if (statement != null) { statement.close(); } if (conn != null) { conn.close(); } } }