Example usage for java.util.concurrent Executors newCachedThreadPool

List of usage examples for java.util.concurrent Executors newCachedThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newCachedThreadPool.

Prototype

public static ExecutorService newCachedThreadPool() 

Source Link

Document

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

Usage

From source file:nextflow.fs.dx.DxUploadOutputStream.java

/**
 * Initialize the uploader output stream for the specified file
 *
 * @param fileId The target DnaNexus file
 * @param remote The DnaNexus API wrapper object
 * @param maxForks Maximum number of parallel upload jobs allowed (default: 5)
 *//*from  w w  w  .j a  v  a 2  s.c  o  m*/
public DxUploadOutputStream(String fileId, DxApi remote, int maxForks) {

    this.fileId = fileId;
    this.queue = new ArrayBlockingQueue<>(maxForks);
    this.phaser = new Phaser();
    this.remote = remote;
    this.buf = allocate();
    this.executor = Executors.newCachedThreadPool();
    checkCapacity();
    start();
}

From source file:com.linkedin.pinot.controller.helix.SegmentStatusChecker.java

/**
 * Constructs the segment status checker.
 * @param pinotHelixResourceManager The resource checker used to interact with Helix
 * @param config The controller configuration object
 *//*from   ww  w .jav a2  s.com*/
public SegmentStatusChecker(PinotHelixResourceManager pinotHelixResourceManager, ControllerConf config,
        ControllerMetrics metricsRegistry) {
    _pinotHelixResourceManager = pinotHelixResourceManager;
    _helixAdmin = pinotHelixResourceManager.getHelixAdmin();
    _config = config;
    _segmentStatusIntervalSeconds = config.getStatusCheckerFrequencyInSeconds();
    _waitForPushTimeSeconds = config.getStatusCheckerWaitForPushTimeInSeconds();
    _metricsRegistry = metricsRegistry;
    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    _tableSizeReader = new TableSizeReader(Executors.newCachedThreadPool(), httpConnectionManager,
            _pinotHelixResourceManager);
}

From source file:com.collective.celos.ci.mode.TestTask.java

private List<Future> submitTestRuns() {
    ExecutorService executor = Executors.newCachedThreadPool();
    List<Future> futures = Lists.newArrayList();
    for (final TestRun testRun : testRuns) {
        Callable callable = new Callable<Void>() {
            @Override/*from   w  w  w  .  j a va2  s  .  co  m*/
            public Void call() throws Exception {
                testRun.start();
                return null;
            }
        };
        futures.add(executor.submit(callable));
    }
    return futures;
}

From source file:io.github.mmichaelis.selenium.client.provider.AbstractWebDriverProviderTest.java

@Test
public void retrieving_webdriver_twice_returns_same_instance_on_thread() throws Exception {
    final WebDriverProvider driverProvider = new NoExceptionWebDriverProvider(defaultDriver, otherDriver);
    final Future<WebDriver[]> webDriversFuture = Executors.newCachedThreadPool()
            .submit(new Callable<WebDriver[]>() {
                @Override/*from  w  w  w.ja  v  a 2 s. c  o m*/
                public WebDriver[] call() {
                    return new WebDriver[] { driverProvider.get(), driverProvider.get() };
                }
            });
    final WebDriver[] webDrivers = webDriversFuture.get();
    assertThat(defaultDriver, sameInstance(webDrivers[0]));
    assertThat(defaultDriver, sameInstance(webDrivers[1]));
}

From source file:com.test.HibernateDerbyLockingTest.java

public void testJDBC() throws Exception {
    final DerbyTemplate template = new DerbyTemplate();
    try {// ww w  .  j av a2s .  c o  m
        template.doWithStatement(new IStatementCallback() {
            public void execute(Statement statement) throws Exception {
                statement.execute(
                        "CREATE TABLE TEST(\n" + "ID CHAR(36) NOT NULL,\n" + "NAME VARCHAR(255)\n" + ")");
            }
        });
        template.doWithStatement(new IStatementCallback() {
            public void execute(Statement statement) throws Exception {
                statement.execute("INSERT INTO TEST(ID, NAME) VALUES('12345', 'Bob')");
            }
        });

        final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();

        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<?> submit = executorService.submit(new Callable<Object>() {

            public Object call() throws Exception {
                template.doWithStatement(new IStatementCallback() {
                    public void execute(Statement statement) throws Exception {
                        ResultSet resultSet = statement.executeQuery(
                                "SELECT ID, NAME FROM TEST WHERE ID = '12345' for update with rs");
                        while (resultSet.next()) {
                            String id = resultSet.getString("ID");
                            String name = resultSet.getString("NAME");
                        }
                        try {
                            Thread.sleep(2000);
                        } catch (Throwable t) {
                        }
                        System.out.println("one");
                        queue.add("one");
                        try {
                            Thread.sleep(500);
                        } catch (Throwable t) {
                        }
                    }
                });
                return null;
            }
        });
        Thread.sleep(500);
        Future<?> submit2 = executorService.submit(new Callable<Object>() {
            public Object call() throws Exception {
                template.doWithStatement(new IStatementCallback() {
                    public void execute(Statement statement) throws Exception {
                        ResultSet resultSet = statement.executeQuery(
                                "SELECT ID, NAME FROM TEST WHERE ID = '12345' for update with rr");
                        while (resultSet.next()) {
                            String id = resultSet.getString("ID");
                            String name = resultSet.getString("NAME");
                        }
                        queue.add("two");
                        System.out.println("two");
                    }
                });
                return null;
            }
        });
        submit.get();
        submit2.get();
        assertEquals("one", queue.poll(3, TimeUnit.SECONDS));
        assertEquals("two", queue.poll(3, TimeUnit.SECONDS));
    } finally {
        template.doWithStatement(new IStatementCallback() {
            public void execute(Statement statement) throws Exception {
                statement.execute("DROP TABLE TEST");
            }
        });
    }
}

From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedConceptRetrievalExecutor.java

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    AbstractQuery aquery = TypeUtil.getAbstractQueries(jcas).stream().findFirst().get();
    String queryString = bopQueryStringConstructor.construct(aquery).replaceAll("[^A-Za-z0-9_\\-\"]+", " ");
    LOG.info("Query string: {}", queryString);
    List<ConceptSearchResult> concepts = Collections.synchronizedList(new ArrayList<>());
    ExecutorService es = Executors.newCachedThreadPool();
    for (BioASQUtil.Ontology ontology : BioASQUtil.Ontology.values()) {
        es.execute(() -> {//from ww w.  ja  va 2 s  .c o m
            try {
                concepts.addAll(BioASQUtil.searchOntology(service, jcas, queryString, pages, hits, ontology));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }
    es.shutdown();
    try {
        if (!es.awaitTermination(timeout, TimeUnit.MINUTES)) {
            LOG.warn("Timeout occurs for one or some concept retrieval services.");
        }
    } catch (InterruptedException e) {
        throw new AnalysisEngineProcessException(e);
    }
    Map<String, List<ConceptSearchResult>> onto2concepts = concepts.stream()
            .collect(groupingBy(ConceptSearchResult::getSearchId));
    for (Map.Entry<String, List<ConceptSearchResult>> entry : onto2concepts.entrySet()) {
        List<ConceptSearchResult> results = entry.getValue();
        LOG.info("Retrieved {} concepts from {}", results.size(), entry.getKey());
        if (LOG.isDebugEnabled()) {
            results.stream().limit(3).forEach(c -> LOG.debug(" - {}", TypeUtil.toString(c)));
        }
    }
    TypeUtil.rankedSearchResultsByScore(concepts, limit).forEach(ConceptSearchResult::addToIndexes);
}

From source file:com.linkedin.pinot.integration.tests.DefaultColumnsClusterIntegrationTest.java

protected void setUp(boolean sendSchema) throws Exception {
    // Set up directories.
    FileUtils.deleteQuietly(TMP_DIR);/* w  w  w.  ja  va  2s.co m*/
    Assert.assertTrue(TMP_DIR.mkdirs());
    Assert.assertTrue(SEGMENT_DIR.mkdir());
    Assert.assertTrue(TAR_DIR.mkdir());

    // Start the cluster.
    startZk();
    startController();
    startBroker();
    startServer();

    // Create the table.
    addOfflineTable("mytable", "DaysSinceEpoch", "daysSinceEpoch", -1, "", null, null);

    // Add the schema.
    if (sendSchema) {
        sendSchema();
    }

    // Unpack the Avro files.
    List<File> avroFiles = unpackAvroData(TMP_DIR, SEGMENT_COUNT);

    // Load data into H2.
    ExecutorService executor = Executors.newCachedThreadPool();
    setupH2AndInsertAvro(avroFiles, executor);

    // Create segments from Avro data.
    buildSegmentsFromAvro(avroFiles, executor, 0, SEGMENT_DIR, TAR_DIR, "mytable", false, null);

    // Initialize query generator.
    setupQueryGenerator(avroFiles, executor);

    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.MINUTES);

    // Set up a Helix spectator to count the number of segments that are uploaded and unlock the latch once 12 segments
    // are online.
    CountDownLatch latch = setupSegmentCountCountDownLatch("mytable", SEGMENT_COUNT);

    // Upload the segments.
    for (String segmentName : TAR_DIR.list()) {
        File file = new File(TAR_DIR, segmentName);
        FileUploadUtils.sendSegmentFile("localhost", "8998", segmentName, new FileInputStream(file),
                file.length());
    }

    // Wait for all segments to be ONLINE.
    latch.await();
    waitForSegmentsOnline();
}

From source file:com.github.nethad.clustermeister.provisioning.ec2.AmazonNodeManager.java

public AmazonNodeManager(Configuration configuration) {
    this.configuration = configuration;
    loadConfiguration(configuration);// www  .  ja va 2  s . co m

    this.executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    this.contextManager = new ContextManager(awsWebApiCredentials, executorService);
    this.ec2Facade = new AwsEc2Facade(contextManager);
    this.credentialsManager = new CredentialsManager(configuration, contextManager);
    this.amazonInstanceManager = new AmazonInstanceManager(contextManager, ec2Facade, profiles,
            artifactsToPreload);
}

From source file:com.polyvi.xface.http.XHttpWorker.java

public XHttpWorker() {
    mThreadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    mClientHeaderMap = new HashMap<String, String>();
    initHttpClient();
}

From source file:com.scooter1556.sms.server.io.AdaptiveStreamingProcess.java

public void initialise() {
    // Stop transcode process if one is already running
    if (process != null) {
        process.destroy();//from   www .  j  a va 2  s.  c  om
    }

    // Determine stream directory
    streamDirectory = new File(SettingsService.getInstance().getCacheDirectory().getPath() + "/streams/" + id);

    try {
        if (streamDirectory.exists()) {
            // Wait for process to finish
            if (process != null) {
                process.waitFor();
            }

            FileUtils.cleanDirectory(streamDirectory);
        } else {
            boolean success = streamDirectory.mkdirs();

            if (!success) {
                LogService.getInstance().addLogEntry(Level.ERROR, CLASS_NAME,
                        "Unable to create directory " + streamDirectory.getPath(), null);
                return;
            }
        }

        // Reset flags
        ended = false;

        // Setup post-processing of audio segments if required
        if (postProcessEnabled && audioTranscodes != null && mediaElement != null && transcoder != null) {
            //  Setup thread pool for post-processing segments
            postProcessExecutor = Executors.newCachedThreadPool();

            // Setup directory watcher
            watcher = new DirectoryWatcher.Builder().addDirectories(streamDirectory.getPath())
                    .setPreExistingAsCreated(true).build(new DirectoryWatcher.Listener() {

                        @Override
                        public void onEvent(DirectoryWatcher.Event event, final Path path) {
                            switch (event) {
                            case ENTRY_CREATE:
                                // Check if we are interested in this file
                                if (!FilenameUtils.getExtension(path.toString()).isEmpty()
                                        || !path.getFileName().toString().contains("audio")) {
                                    break;
                                }

                                // Get the information we require
                                String[] segmentData = FilenameUtils.getBaseName(path.getFileName().toString())
                                        .split("-");

                                if (segmentData.length < 3) {
                                    break;
                                }

                                // Variables
                                final int transcode = Integer.parseInt(segmentData[2]);

                                // Retrive transcode format
                                if (audioTranscodes.length < transcode || mediaElement == null) {
                                    break;
                                }

                                // Determine codec
                                AudioTranscode aTranscode = audioTranscodes[transcode];
                                String codec = aTranscode.getCodec();

                                if (codec.equals("copy")) {
                                    codec = MediaUtils.getAudioStreamById(mediaElement.getAudioStreams(),
                                            aTranscode.getId()).getCodec();
                                }

                                final String format = TranscodeUtils.getFormatForAudioCodec(codec);

                                // Transcode
                                postProcessExecutor.submit(new Runnable() {
                                    @Override
                                    public void run() {
                                        postProcess(path.toString(), format);
                                    }
                                });
                                break;

                            case ENTRY_MODIFY:
                                break;

                            case ENTRY_DELETE:
                                break;
                            }
                        }
                    });

            // Start directory watcher
            watcher.start();
        }

        // Start transcoding
        start();
    } catch (Exception ex) {
        if (process != null) {
            process.destroy();
        }

        if (watcher != null) {
            watcher.stop();
        }

        if (postProcessExecutor != null && !postProcessExecutor.isTerminated()) {
            postProcessExecutor.shutdownNow();
        }

        ended = true;

        LogService.getInstance().addLogEntry(Level.ERROR, CLASS_NAME,
                "Error starting adaptive streaming process.", ex);
    }
}