List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:ubic.gemma.loader.util.fetcher.FtpFetcher.java
/** * @param future//ww w . j a va 2s . c o m * @param expectedSize * @param outputFileName * @param seekFileName * @return */ protected Collection<LocalFile> doTask(FutureTask<Boolean> future, long expectedSize, String seekFileName, String outputFileName) { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(future); executor.shutdown(); try { File outputFile = new File(outputFileName); boolean ok = waitForDownload(future, expectedSize, outputFile); if (!ok) { // cancelled, probably. log.info("Download failed, was it cancelled?"); return null; } else if (future.get().booleanValue()) { if (log.isInfoEnabled()) log.info("Done: local file is " + outputFile); LocalFile file = fetchedFile(seekFileName, outputFile.getAbsolutePath()); Collection<LocalFile> result = new HashSet<LocalFile>(); result.add(file); return result; } } catch (ExecutionException e) { throw new RuntimeException("Couldn't fetch " + seekFileName, e); } catch (InterruptedException e) { log.warn("Interrupted: Couldn't fetch " + seekFileName, e); return null; } catch (CancellationException e) { log.info("Cancelled"); return null; } throw new RuntimeException("Couldn't fetch file for " + seekFileName); }
From source file:org.apache.activemq.store.jdbc.JDBCCleanupLimitedPoolTest.java
@Test public void testNoDeadlockOnXaPoolExhaustion() throws Exception { final CountDownLatch done = new CountDownLatch(1); final CountDownLatch doneCommit = new CountDownLatch(1000); final ActiveMQXAConnectionFactory factory = new ActiveMQXAConnectionFactory( broker.getTransportConnectorByScheme("tcp").getPublishableConnectString()); ExecutorService executorService = Executors.newCachedThreadPool(); // some contention over pool of 2 for (int i = 0; i < 3; i++) { executorService.execute(new Runnable() { @Override// ww w . j a v a 2 s . co m public void run() { try { ActiveMQXAConnection conn = (ActiveMQXAConnection) factory.createXAConnection(); conn.start(); XASession sess = conn.createXASession(); while (done.getCount() > 0 && doneCommit.getCount() > 0) { Xid xid = createXid(); sess.getXAResource().start(xid, XAResource.TMNOFLAGS); MessageProducer producer = sess.createProducer(sess.createQueue("test")); producer.send(sess.createTextMessage("test")); sess.getXAResource().end(xid, XAResource.TMSUCCESS); sess.getXAResource().prepare(xid); sess.getXAResource().commit(xid, false); doneCommit.countDown(); } conn.close(); } catch (Exception ignored) { ignored.printStackTrace(); } } }); } executorService.execute(new Runnable() { @Override public void run() { try { while (!done.await(10, TimeUnit.MILLISECONDS) && doneCommit.getCount() > 0) { jdbcPersistenceAdapter.cleanup(); } } catch (Exception ignored) { } } }); executorService.shutdown(); boolean allComplete = executorService.awaitTermination(40, TimeUnit.SECONDS); done.countDown(); assertTrue("all complete", allComplete); executorService.shutdownNow(); assertTrue("xa tx done", doneCommit.await(10, TimeUnit.SECONDS)); }
From source file:org.carewebframework.vista.mbroker.PollingThread.java
/** * Creates and starts a polling thread for the specified session. If an executor service has * been configured, thread execution will be delegated to that service. Otherwise, it will be * started directly./*from w ww . java2s . c o m*/ * * @param session Broker session associated with the thread. */ public PollingThread(BrokerSession session) { super(); setName("MBrokerPollingDaemon-" + getId()); this.sessionRef = new WeakReference<BrokerSession>(session); ExecutorService executor = session.getExecutorService(); if (executor != null) { executor.execute(this); } else { start(); } }
From source file:ubic.gemma.loader.util.fetcher.FtpArchiveFetcher.java
/** * @param newDir/*from w w w. j a va 2 s .com*/ * @param seekFile */ protected void unPack(final File toUnpack) { FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() { @Override @SuppressWarnings("synthetic-access") public Boolean call() { File extractedFile = new File(FileTools.chompExtension(toUnpack.getAbsolutePath())); /* * Decide if an existing file is plausibly usable. Err on the side of caution. */ if (allowUseExisting && extractedFile.canRead() && extractedFile.length() >= toUnpack.length() && !FileUtils.isFileNewer(toUnpack, extractedFile)) { log.warn("Expanded file exists, skipping re-expansion: " + extractedFile); return Boolean.TRUE; } if (expander != null) { expander.setSrc(toUnpack); expander.setDest(toUnpack.getParentFile()); expander.perform(); } else if (toUnpack.getAbsolutePath().toLowerCase().endsWith("zip")) { try { FileTools.unZipFiles(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } else { // gzip. try { FileTools.unGzipFile(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } return Boolean.TRUE; } }); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(future); executor.shutdown(); StopWatch s = new StopWatch(); s.start(); while (!future.isDone() && !future.isCancelled()) { try { Thread.sleep(INFO_UPDATE_INTERVAL); } catch (InterruptedException ie) { future.cancel(true); return; } log.info("Unpacking archive ... " + Math.floor(s.getTime() / 1000.0) + " seconds elapsed"); } }
From source file:com.brienwheeler.lib.concurrent.ExecutorsTest.java
@Test public void testNewSingleThreadExecutorShutdownClean() throws InterruptedException { NamedThreadFactory threadFactory = new NamedThreadFactory(THREAD_FACTORY_NAME); ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory); Assert.assertFalse(executor.isShutdown()); Assert.assertFalse(executor.isTerminated()); executor.execute(new NullRunnable()); executor.shutdown();/*w ww . j av a2 s . c o m*/ Assert.assertTrue(executor.isShutdown()); executor.awaitTermination(10, TimeUnit.MILLISECONDS); Assert.assertTrue(executor.isTerminated()); }
From source file:io.anserini.IndexerCW09B.java
public int indexWithThreads(int numThreads) throws IOException, InterruptedException { System.out.println(//from w w w. j ava 2 s .c o m "Indexing with " + numThreads + " threads to directory '" + indexPath.toAbsolutePath() + "'..."); final Directory dir = FSDirectory.open(indexPath); final IndexWriterConfig iwc = new IndexWriterConfig(analyzer()); iwc.setSimilarity(new BM25Similarity()); iwc.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE); iwc.setRAMBufferSizeMB(256.0); iwc.setUseCompoundFile(false); iwc.setMergeScheduler(new ConcurrentMergeScheduler()); final IndexWriter writer = new IndexWriter(dir, iwc); final ExecutorService executor = Executors.newFixedThreadPool(numThreads); for (Path f : discoverWarcFiles(docDir)) executor.execute(new IndexerThread(writer, f)); //add some delay to let some threads spawn by scheduler Thread.sleep(30000); executor.shutdown(); // Disable new tasks from being submitted try { // Wait for existing tasks to terminate while (!executor.awaitTermination(5, TimeUnit.MINUTES)) { Thread.sleep(1000); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted executor.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } int numIndexed = writer.maxDoc(); try { writer.commit(); } finally { writer.close(); } return numIndexed; }
From source file:com.vaushell.superpipes.tools.http.ImageExtractor.java
/** * Return the biggest image URI of this webpage. * * @param rootURI Webpage URI//from w ww. j a v a 2s . c o m * @return Biggest image * @throws IOException */ public BufferedImage extractBiggest(final URI rootURI) throws IOException { final List<URI> imagesURIs = new ArrayList<>(); HttpEntity responseEntity = null; try { // Exec request final HttpGet get = new HttpGet(rootURI); try (final CloseableHttpResponse response = client.execute(get)) { final StatusLine sl = response.getStatusLine(); if (sl.getStatusCode() != 200) { throw new IOException(sl.getReasonPhrase()); } responseEntity = response.getEntity(); try (final InputStream is = responseEntity.getContent()) { final Document doc = Jsoup.parse(is, "UTF-8", rootURI.toString()); final Elements elts = doc.select("img"); if (elts != null) { for (final Element elt : elts) { final String src = elt.attr("src"); if (src != null && !src.isEmpty()) { try { imagesURIs.add(rootURI.resolve(src)); } catch (final IllegalArgumentException ex) { // Ignore wrong encoded URI } } } } } } } finally { if (responseEntity != null) { EntityUtils.consume(responseEntity); } } final BufferedImage[] images = new BufferedImage[imagesURIs.size()]; final ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < imagesURIs.size(); ++i) { final int num = i; service.execute(new Runnable() { @Override public void run() { try { images[num] = HTTPhelper.loadPicture(client, imagesURIs.get(num)); } catch (final IOException ex) { images[num] = null; } } }); } service.shutdown(); try { service.awaitTermination(1L, TimeUnit.DAYS); } catch (final InterruptedException ex) { // Ignore } BufferedImage biggest = null; int biggestSize = Integer.MIN_VALUE; for (int i = 0; i < imagesURIs.size(); ++i) { if (images[i] != null) { final int actualSize = images[i].getWidth() * images[i].getHeight(); if (actualSize > biggestSize) { biggest = images[i]; biggestSize = actualSize; } } } return biggest; }
From source file:org.openhab.binding.plclogo.internal.discovery.PLCDiscoveryService.java
@Override protected void startScan() { stopScan();/*from w w w . ja v a2s . c o m*/ logger.debug("Start scan for LOGO! bridge"); Enumeration<NetworkInterface> devices = null; try { devices = NetworkInterface.getNetworkInterfaces(); } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } Set<String> addresses = new TreeSet<>(); while ((devices != null) && devices.hasMoreElements()) { NetworkInterface device = devices.nextElement(); try { if (!device.isUp() || device.isLoopback()) { continue; } } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } for (InterfaceAddress iface : device.getInterfaceAddresses()) { InetAddress address = iface.getAddress(); if (address instanceof Inet4Address) { String prefix = String.valueOf(iface.getNetworkPrefixLength()); SubnetUtils utilities = new SubnetUtils(address.getHostAddress() + "/" + prefix); addresses.addAll(Arrays.asList(utilities.getInfo().getAllAddresses())); } } } ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (String address : addresses) { try { executor.execute(new Runner(address)); } catch (RejectedExecutionException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } } try { executor.awaitTermination(CONNECTION_TIMEOUT * addresses.size(), TimeUnit.MILLISECONDS); } catch (InterruptedException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } executor.shutdown(); stopScan(); }
From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java
@Test public void testBothLock() throws Exception { for (int i = 0; i < 100; i++) { final JdbcLockRegistry registry1 = this.registry; final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class); final List<String> locked = new ArrayList<String>(); final CountDownLatch latch = new CountDownLatch(2); ExecutorService pool = Executors.newFixedThreadPool(2); pool.execute(() -> { Lock lock = registry1.obtain("foo"); try { lock.lockInterruptibly(); locked.add("1"); latch.countDown();/* w ww. ja v a2s.c om*/ } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } finally { try { lock.unlock(); } catch (Exception e2) { // ignore } } }); pool.execute(() -> { Lock lock = registry2.obtain("foo"); try { lock.lockInterruptibly(); locked.add("2"); latch.countDown(); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } finally { try { lock.unlock(); } catch (Exception e2) { // ignore } } }); assertTrue(latch.await(10, TimeUnit.SECONDS)); // eventually they both get the lock and release it assertTrue(locked.contains("1")); assertTrue(locked.contains("2")); } }
From source file:org.gytheio.messaging.benchmark.BenchmarkRunner.java
/** * Initializes a Qpid-based AMQP endpoint with no object marshaling with given * brokerUrl, endpoint, and messageConsumer. * /* w w w .ja v a 2 s.c o m*/ * @param brokerUrl * @param endpoint * @param messageConsumer * @return the Gytheio message producer */ protected MessageProducer initializeAmqpDirectEndpoint(final String brokerUrl, final String endpointSend, final String endpointReceive, final MessageConsumer messageConsumer) { AmqpDirectEndpoint amqpEndpoint = null; if (messageConsumer != null) { amqpEndpoint = AmqpNodeBootstrapUtils.createEndpoint(messageConsumer, brokerUrl, null, null, endpointSend, endpointReceive); // Start listener ExecutorService executorService = Executors.newCachedThreadPool(); executorService.execute(amqpEndpoint.getListener()); // Wait for listener initialization while (!amqpEndpoint.isInitialized()) { try { Thread.sleep(100); } catch (InterruptedException e) { } } } return amqpEndpoint; }