List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:be.panako.cli.Store.java
@Override public void run(final String... args) { int processors = availableProcessors(); int counter = 0; final ExecutorService executor = Executors.newFixedThreadPool(processors); final List<File> files = this.getFilesFromArguments(args); boolean extractMetaData = hasArgument("-m", args) || hasArgument("--meta-data", args); if (files.size() > 1) { String msg = "Processing " + files.size() + " files on " + processors + " seperate threads."; System.out.println(msg);//ww w.ja v a 2 s . c o m LOG.info("Store task started. " + msg); } System.out.println("Audiofile;Audio duration;Fingerprinting duration;ratio"); for (File file : files) { counter++; //executor.submit(new StoreTask(file, counter, files.size())); StoreTask task = new StoreTask(file, counter, files.size(), extractMetaData); task.run(); } try { //do not accept more tasks. executor.shutdown(); //wait for tasks to finish executor.awaitTermination(300, java.util.concurrent.TimeUnit.DAYS); //System.exit(0); } catch (Exception e) { e.printStackTrace(); } /*catch (InterruptedException e1) { //Thread was interrupted e1.printStackTrace(); LOG.severe("Did not finish all tasks, thread was interrupted!"); }*/ }
From source file:com.pinterest.rocksplicator.controller.tasks.LoadSSTTask.java
@Override public void process(Context ctx) throws Exception { final String clusterName = ctx.getCluster(); final String segment = getParameter().getSegment(); ClusterBean clusterBean = ZKUtil.getClusterConfig(zkClient, clusterName); if (clusterBean == null) { LOG.error("Failed to get config for cluster {}.", clusterName); ctx.getTaskQueue().failTask(ctx.getId(), "Failed to read cluster config from zookeeper."); return;//from ww w. j a v a 2 s . c om } SegmentBean segmentBean = clusterBean.getSegments().stream().filter(s -> s.getName().equals(segment)) .findAny().orElse(null); if (segmentBean == null) { String errMsg = String.format("Segment %s not in cluster %s.", segment, clusterName); LOG.error(errMsg); ctx.getTaskQueue().failTask(ctx.getId(), errMsg); return; } final ExecutorService executor = Executors.newFixedThreadPool(getParameter().getConcurrency()); try { // first pass load sst to masters doLoadSST(executor, segmentBean, Role.MASTER); LOG.info("First pass done."); // second pass load sst to slaves doLoadSST(executor, segmentBean, Role.SLAVE); LOG.info("Second pass done."); } catch (InterruptedException | ExecutionException ex) { LOG.error("Failed to load sst to cluster {}.", clusterName, ex); ctx.getTaskQueue().failTask(ctx.getId(), "Failed to load sst, error=" + ex.getMessage()); return; } executor.shutdown(); executor.shutdownNow(); ctx.getTaskQueue().finishTask(ctx.getId(), "Finished loading sst to " + clusterName); }
From source file:com.dell.doradus.db.s3.AmazonS3Service.java
private void connect() { String accessKey = getParamString("s3-access-key"); String secretKey = getParamString("s3-secret-key"); String bucket = getParamString("s3-bucket"); AWSCredentials awsCredentials = new AWSCredentials(accessKey, secretKey); S3Service s3service = new RestS3Service(awsCredentials); Jets3tProperties props = s3service.getJetS3tProperties(); String port = getParamString("s3-endpoint-http-port"); String httpsOnly = getParamString("s3-https-only"); String endpoint = getParamString("s3-endpoint"); if (port != null) props.setProperty("s3service.s3-endpoint-http-port", port); if (httpsOnly != null) props.setProperty("s3service.https-only", httpsOnly); if (endpoint != null) props.setProperty("s3service.s3-endpoint", endpoint); //props.setProperty("s3service.s3-endpoint-http-port", "10453"); //props.setProperty("s3service.https-only", "false"); //props.setProperty("s3service.s3-endpoint", "localhost"); props.setProperty("s3service.disable-dns-buckets", "true"); CredentialsProvider credentials = s3service.getCredentialsProvider(); s3service = new RestS3Service(awsCredentials, "Doradus", credentials, props); Object str_threads = getParamString("s3-threads"); int threads = str_threads == null ? 1 : Integer.parseInt(str_threads.toString()); s3_executor = Executors.newFixedThreadPool(threads); m_connection = new AmazonConnection(s3service, bucket); }
From source file:org.olegz.uuid.TimeBasedUUIDGeneratorTests.java
@Test public void performanceTestAsynch() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(100); StopWatch stopWatch = new StopWatch(); stopWatch.start();/*from w ww.ja va 2s . c o m*/ for (int i = 0; i < 1000000; i++) { executor.execute(new Runnable() { public void run() { TimeBasedUUIDGenerator.generateId(); } }); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); stopWatch.stop(); System.out.println("Generated 1000000 UUID (async) via TimeBasedUUIDGenerator.generateId(): in " + stopWatch.getTotalTimeSeconds() + " seconds"); executor = Executors.newFixedThreadPool(100); stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i < 1000000; i++) { executor.execute(new Runnable() { public void run() { UUID.randomUUID(); } }); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); stopWatch.stop(); System.out.println("Generated 1000000 UUID (async) via UUID.randomUUID(): in " + stopWatch.getTotalTimeSeconds() + " seconds"); }
From source file:com.marklogic.contentpump.LocalJobRunner.java
public LocalJobRunner(Job job, CommandLine cmdline, Command cmd) { this.job = job; this.cmd = cmd; threadCount = DEFAULT_THREAD_COUNT;/*from w w w . j a v a2s.c om*/ if (cmdline.hasOption(THREAD_COUNT)) { threadCount = Integer.parseInt(cmdline.getOptionValue(THREAD_COUNT)); } if (threadCount > 1) { pool = Executors.newFixedThreadPool(threadCount); if (LOG.isDebugEnabled()) { LOG.debug("Thread pool size: " + threadCount); } } if (cmdline.hasOption(THREADS_PER_SPLIT)) { threadsPerSplit = Integer.parseInt(cmdline.getOptionValue(THREADS_PER_SPLIT)); } Configuration conf = job.getConfiguration(); minThreads = conf.getInt(CONF_MIN_THREADS, minThreads); jobComplete = new AtomicBoolean(); startTime = System.currentTimeMillis(); }
From source file:com.intuit.tank.vmManager.environment.amazon.CloudwatchInstance.java
/** * // www .ja va 2s. c om * @param request * @param vmRegion */ public CloudwatchInstance(VMRegion vmRegion) { // In case vmRegion is passed as null, use default region from settings file if (vmRegion == null) { vmRegion = config.getVmManagerConfig().getDefaultRegion(); } try { CloudCredentials creds = config.getVmManagerConfig().getCloudCredentials(CloudProvider.amazon); AWSCredentials credentials = new BasicAWSCredentials(creds.getKeyId(), creds.getKey()); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setMaxConnections(2); if (StringUtils.isNotBlank(creds.getProxyHost())) { try { clientConfig.setProxyHost(creds.getProxyHost()); if (StringUtils.isNotBlank(creds.getProxyPort())) { clientConfig.setProxyPort(Integer.valueOf(creds.getProxyPort())); } } catch (NumberFormatException e) { logger.error("invalid proxy setup."); } } if (StringUtils.isNotBlank(creds.getKeyId()) && StringUtils.isNotBlank(creds.getKey())) { asynchCloudWatchClient = new AmazonCloudWatchAsyncClient(credentials, clientConfig, Executors.newFixedThreadPool(2)); asyncSnsClient = new AmazonSNSAsyncClient(credentials, clientConfig, Executors.newFixedThreadPool(2)); } else { asynchCloudWatchClient = new AmazonCloudWatchAsyncClient(clientConfig); asyncSnsClient = new AmazonSNSAsyncClient(clientConfig); } asynchCloudWatchClient.setRegion(Region.getRegion(Regions.fromName(vmRegion.getRegion()))); asyncSnsClient.setRegion(Region.getRegion(Regions.fromName(vmRegion.getRegion()))); } catch (Exception ex) { logger.error(ex.getMessage()); throw new RuntimeException(ex); } }
From source file:com.splout.db.dnode.DNode.java
/** * Initialize the DNode service - the important thing here is to instantiate a * multi-threaded Thrift server. Everything is based on the given * {@link SploutConfiguration} by constructor. *//*from ww w . j a v a 2s . c o m*/ @SuppressWarnings({ "unchecked", "rawtypes" }) public void init() throws Exception { DNodeService.Processor processor = new DNodeService.Processor(this); TNonblockingServerTransport serverTransport = null; boolean init = false; int retries = 0; int thriftPort; do { thriftPort = config.getInt(DNodeProperties.PORT); try { serverTransport = new TNonblockingServerSocket(thriftPort); init = true; } catch (org.apache.thrift.transport.TTransportException e) { if (!config.getBoolean(DNodeProperties.PORT_AUTOINCREMENT)) { throw e; } config.setProperty(DNodeProperties.PORT, thriftPort + 1); retries++; } } while (!init && retries < 100); handler.init(config); THsHaServer.Args args = new THsHaServer.Args(serverTransport); args.executorService(Executors.newFixedThreadPool(config.getInt(DNodeProperties.SERVING_THREADS))); args.processor(processor); server = new THsHaServer(args); // We instantiate a long-living serving thread that will use the Thrift // server. servingThread = new Thread("Serving Thread") { public void run() { try { server.serve(); } catch (Throwable t) { t.printStackTrace(); } } }; servingThread.start(); log.info("Thrift server started on port: " + thriftPort); if (handler instanceof DNodeHandler && !config.getBoolean(DNodeProperties.STREAMING_API_DISABLE)) { // Instantiate a TCP server for serving streaming data if not disabled by config // Follow also the same incrementing port approach if specified so by config init = false; retries = 0; do { streamer = new TCPStreamer(); int tcpPort = config.getInteger(DNodeProperties.STREAMING_PORT, 8888); try { streamer.start(config, (DNodeHandler) handler); log.info("TCP streamer server started on port: " + streamer.getTcpPort()); init = true; } catch (BindException e) { if (!config.getBoolean(DNodeProperties.PORT_AUTOINCREMENT)) { throw e; } config.setProperty(DNodeProperties.STREAMING_PORT, tcpPort + 1); retries++; } } while (!init && retries < 100); } handler.giveGreenLigth(); }
From source file:fr.mby.opa.pics.web.controller.PicsController.java
@RequestMapping(value = "rebuildThumbnails/{width}/{height}/{format}", method = RequestMethod.GET) public String rebuildThumbnails(@PathVariable final Integer width, @PathVariable final Integer height, @PathVariable final String format, final HttpServletRequest request, final HttpServletResponse response) throws Exception { final Integer selectedWidth = (width != null) ? width : 800; final Integer selectedHeight = (height != null) ? height : 200; final String selectedFormat = (format != null) ? format : "jpg"; final ExecutorService rebuildThumbnailsExecutor = Executors .newFixedThreadPool(Runtime.getRuntime().availableProcessors()); Long since = 0L;//from w ww . j av a2s . co m List<Picture> pictures = null; do { pictures = this.pictureDao.findAllPictures(since); final Collection<Future<Void>> futures = new ArrayList<>(pictures.size()); for (final Picture picture : pictures) { final Future<Void> future = rebuildThumbnailsExecutor.submit(new Callable<Void>() { @Override public Void call() throws Exception { final BinaryImage generated = PicsController.this.pictureService.generateThumbnail(picture, selectedWidth, selectedHeight, true, selectedFormat); // Set the old Id to update final BinaryImage thumbnailToUpdate = picture.getThumbnail(); thumbnailToUpdate.setData(generated.getData()); thumbnailToUpdate.setFormat(generated.getFormat()); thumbnailToUpdate.setWidth(generated.getWidth()); thumbnailToUpdate.setHeight(generated.getHeight()); picture.setThumbnail(generated); picture.setThumbnailWidth(generated.getWidth()); picture.setThumbnailHeight(generated.getHeight()); picture.setThumbnailFormat(generated.getFormat()); picture.setThumbnailSize(generated.getData().length); PicsController.this.pictureDao.updatePicture(picture); // Free memory picture.setThumbnail(null); picture.setImage(null); return null; } }); since = picture.getOriginalTime().getTime(); futures.add(future); } for (final Future<Void> future : futures) { future.get(); } } while (pictures != null && pictures.size() > 0); return "index"; }
From source file:info.pancancer.arch3.containerProvisioner.ContainerProvisionerThreads.java
public void startThreads() throws InterruptedException { ExecutorService pool = Executors.newFixedThreadPool(DEFAULT_THREADS); ProcessVMOrders processVMOrders = new ProcessVMOrders(this.configFile, this.options.has(this.endlessSpec)); ProvisionVMs provisionVMs = new ProvisionVMs(this.configFile, this.options.has(this.endlessSpec), this.options.has(testSpec)); CleanupVMs cleanupVMs = new CleanupVMs(this.configFile, this.options.has(this.endlessSpec)); List<Future<?>> futures = new ArrayList<>(); futures.add(pool.submit(processVMOrders)); futures.add(pool.submit(provisionVMs)); futures.add(pool.submit(cleanupVMs)); try {/* w w w . j av a 2s . c o m*/ for (Future<?> future : futures) { future.get(); } } catch (InterruptedException | ExecutionException ex) { log.error(ex.toString()); throw new RuntimeException(ex); } finally { pool.shutdown(); } }
From source file:eu.eexcess.europeana.recommender.PartnerConnector.java
@Override public Document queryPartner(PartnerConfiguration partnerConfiguration, SecureUserProfile userProfile, PartnerdataLogger logger) throws IOException { // Configure// w w w . j a v a 2s . c o m ExecutorService threadPool = Executors.newFixedThreadPool(10); // ClientConfig config = new DefaultClientConfig(); // config.getClasses().add(JacksonJsonProvider.class); // final Client client = new Client(PartnerConfigurationEnum.CONFIG.getClientJacksonJson()); queryGenerator = PartnerConfigurationEnum.CONFIG.getQueryGenerator(); String query = getQueryGenerator().toQuery(userProfile); long start = System.currentTimeMillis(); Map<String, String> valuesMap = new HashMap<String, String>(); valuesMap.put("query", URLParamEncoder.encode(query)); valuesMap.put("apiKey", partnerConfiguration.apiKey); // add API key // searchEndpoint: "http://www.europeana.eu/api/v2/search.json?wskey=${apiKey}&query=${query}" int numResultsRequest = 10; if (userProfile.numResults != null && userProfile.numResults != 0) numResultsRequest = userProfile.numResults; valuesMap.put("numResults", numResultsRequest + ""); String searchRequest = StrSubstitutor.replace(partnerConfiguration.searchEndpoint, valuesMap); WebResource service = client.resource(searchRequest); ObjectMapper mapper = new ObjectMapper(); Builder builder = service.accept(MediaType.APPLICATION_JSON); EuropeanaResponse response = builder.get(EuropeanaResponse.class); if (response.items.size() > numResultsRequest) response.items = response.items.subList(0, numResultsRequest); PartnerdataTracer.dumpFile(this.getClass(), partnerConfiguration, response.toString(), "service-response", PartnerdataTracer.FILETYPE.JSON, logger); client.destroy(); if (makeDetailRequests) { HashMap<EuropeanaDoc, Future<Void>> futures = new HashMap<EuropeanaDoc, Future<Void>>(); final HashMap<EuropeanaDoc, EuropeanaDocDetail> docDetails = new HashMap<EuropeanaDoc, EuropeanaDocDetail>(); final PartnerConfiguration partnerConfigLocal = partnerConfiguration; final String eexcessRequestId = logger.getActLogEntry().getRequestId(); for (int i = 0; i < response.items.size(); i++) { final EuropeanaDoc item = response.items.get(i); Future<Void> future = threadPool.submit(new Callable<Void>() { @Override public Void call() throws Exception { EuropeanaDocDetail details = null; try { details = fetchDocumentDetails(item.id, partnerConfigLocal, eexcessRequestId); } catch (EEXCESSDataTransformationException e) { log.log(Level.INFO, "Error getting item with id" + item.id, e); return null; } docDetails.put(item, details); return null; } }); futures.put(item, future); } for (EuropeanaDoc doc : futures.keySet()) { try { futures.get(doc).get(start + 15 * 500 - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { log.log(Level.WARNING, "Detail thread for " + doc.id + " did not responses in time", e); } //item.edmConcept.addAll(details.concepts); // item.edmConcept = details.concepts; TODO: copy into doc // item.edmCountry = details.edmCountry; // item.edmPlace = details.places; } } long end = System.currentTimeMillis(); long startXML = System.currentTimeMillis(); Document newResponse = null; try { newResponse = this.transformJSON2XML(mapper.writeValueAsString(response)); } catch (EEXCESSDataTransformationException e) { // TODO logger log.log(Level.INFO, "Error Transforming Json to xml", e); } long endXML = System.currentTimeMillis(); System.out.println("millis " + (endXML - startXML) + " " + (end - start)); threadPool.shutdownNow(); return newResponse; }