Example usage for java.util.concurrent Executors newFixedThreadPool

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

Introduction

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

Prototype

public static ExecutorService newFixedThreadPool(int nThreads) 

Source Link

Document

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

Usage

From source file:com.netflix.conductor.core.events.EventProcessor.java

@Inject
public EventProcessor(ExecutionService es, MetadataService ms, ActionProcessor ap, Configuration config,
        ObjectMapper om) {/* w ww .j  a  v  a2s .c  o m*/
    this.es = es;
    this.ms = ms;
    this.ap = ap;
    this.om = om;

    int executorThreadCount = config.getIntProperty("workflow.event.processor.thread.count", 2);
    if (executorThreadCount > 0) {
        this.executors = Executors.newFixedThreadPool(executorThreadCount);
        refresh();
        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> refresh(), 60, 60, TimeUnit.SECONDS);
    } else {
        logger.warn("Event processing is DISABLED.  executorThreadCount set to {}", executorThreadCount);
    }
}

From source file:com.clonescriptscrapper.crawler.CloneScriptScrapper.java

public void crawledCategories() throws URISyntaxException, IOException, InterruptedException, Exception {

    String url = "http://www.clonescriptdirectory.com/";

    //       Document doc = Jsoup.parse(fetchPage(new URI(url)));
    String response = "";
    response = new GetRequestHandler().doGetRequest(new URL(url));

    Document doc = Jsoup.parse(response);

    //        System.out.println("---" + doc);
    Elements ele = doc.select("table[class=categories] tbody tr td a");
    for (Element ele1 : ele) {

        objCategories = new Categories();

        String title = ele1.text();
        String href = ele1.attr("href");
        System.out.println("Title : " + ele1.text());
        System.out.println("Href : " + ele1.attr("href"));

        objCategories.setCategoryName(title);
        objCategories.setCategoryUrl(href);

        objCloneScriptDirectoryDaoImpl.insertCategoriesData(objCategories);
    }/*from  w  w  w . jav  a 2  s  .c o  m*/

    List<Future<String>> list = new ArrayList<Future<String>>();
    ExecutorService executor = Executors.newFixedThreadPool(5);

    List<Categories> listCatogories = objCloneScriptDirectoryDaoImpl.getCategoriesDataList();

    for (Categories listCatogory : listCatogories) {

        try {
            objCloneScriptDirectoryDaoImpl.updateCategoriesData(objCategories);
            Callable worker = new CrawlingEachUrlData(listCatogory, objCloneScriptDirectoryDaoImpl);
            Future<String> future = executor.submit(worker);
            list.add(future);
        } catch (Exception exx) {
            System.out.println(exx);
        }

    }

    for (Future<String> fut : list) {
        try {
            //print the return value of Future, notice the output delay in console
            // because Future.get() waits for task to get completed
            System.out.println(new Date() + "::" + fut.get());
        } catch (InterruptedException | ExecutionException ep) {
            ep.printStackTrace();
        }
    }
    //shut down the executor service now
    executor.shutdown();

    //            objcrawlingUrlData.crawlingUrlData(href);
}

From source file:com.siva.javamultithreading.MultiThreadExecutor.java

/**
 * This is sample.// w  w  w  .j  a v  a 2  s.  c o m
 */
private static void startProcess() {

    ExecutorService threadPool = Executors.newFixedThreadPool(4);
    CompletionService<HSSFWorkbook> pool = new ExecutorCompletionService<>(threadPool);
    // Excel creation through multiple threads
    long startTime = System.currentTimeMillis();
    pool.submit(new ExcelChunkSheetWriter(0, 1000));
    pool.submit(new ExcelChunkSheetWriter(1001, 20000));
    pool.submit(new ExcelChunkSheetWriter(2, 3000));
    pool.submit(new ExcelChunkSheetWriter(3, 40000));
    pool.submit(new ExcelChunkSheetWriter(4, 50000));

    HSSFWorkbook hSSFWorkbook = null;
    HSSFWorkbook book = new HSSFWorkbook();
    HSSFSheet sheet = book.createSheet("Report");

    try {
        for (int i = 0; i < 5; i++) {
            hSSFWorkbook = pool.take().get();
            System.out.println(
                    "sheet row count : sheet.PhysicalNumberOfRows() = " + sheet.getPhysicalNumberOfRows());
            int currentCount = sheet.getPhysicalNumberOfRows();
            int incomingCount = hSSFWorkbook.getSheetAt(0).getPhysicalNumberOfRows();
            if ((currentCount + incomingCount) > 60000) {
                sheet = book.createSheet("Report" + i);
            }
            ExcelUtil.copySheets(book, sheet, hSSFWorkbook.getSheetAt(0));
        }
    } catch (InterruptedException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ExecutionException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        writeFile(book, new FileOutputStream("Report.xls"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    /*
     FileOutputStream fos = new FileOutputStream("all.zip");
     ZipOutputStream zos = new ZipOutputStream(fos);
     for (int i = 0; i < 5; i++) {
     try {
     hSSFWorkbook = pool.take().get();                
     ZipEntry ze = new ZipEntry("Excel" + i + ".xls");
     zos.putNextEntry(ze);
     hSSFWorkbook.write(zos);
     zos.closeEntry();
     } catch (InterruptedException ex) {
     Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
     }           
     }
     zos.close();
     */
    long endTime = System.currentTimeMillis();
    System.out.println("Time taken: " + (endTime - startTime) + " ms");
    threadPool.shutdown();
}

From source file:com.clonephpscrapper.crawler.ClonePhpScrapper.java

public void crawledCategories() throws URISyntaxException, IOException, InterruptedException, Exception {

    String url = "http://clonephp.com/";

    //       Document doc = Jsoup.parse(fetchPage(new URI(url)));
    String response = "";
    response = new GetRequestHandler().doGetRequest(new URL(url));

    Document doc = Jsoup.parse(response);

    Elements ele = doc.select("table[class=dir] tbody tr td table[class=dir_cat] tbody tr th a");//.first();

    for (Element ele1 : ele) {
        objCategories = new Categories();

        String categoryName = ele1.text();
        String categoryUrl = "http://clonephp.com/" + ele1.attr("href");

        System.out.println("CATEGORY_NAME : " + categoryName);
        System.out.println("CATEGORY_URL  : " + categoryUrl);

        objCategories.setCategoryName(categoryName);
        objCategories.setCategoryUrl(categoryUrl);

        objClonePhpDaoImpl.insertCategoriesData(objCategories);

        //            objCrawlingEachUrlData.crawlingUrlData(categoryUrl);
    }/*from   w w  w  . j av a2 s.c o m*/

    List<Future<String>> list = new ArrayList<Future<String>>();
    ExecutorService executor = Executors.newFixedThreadPool(5);

    List<Categories> listCatogories = objClonePhpDaoImpl.getCategoriesDataList();

    for (Categories listCatogory : listCatogories) {

        try {
            Callable worker = new CrawlingEachUrlData(listCatogory, objClonePhpDaoImpl);
            Future<String> future = executor.submit(worker);
            list.add(future);
        } catch (Exception exx) {
            System.out.println(exx);
        }

    }

    for (Future<String> fut : list) {
        try {
            //print the return value of Future, notice the output delay in console
            // because Future.get() waits for task to get completed
            System.out.println(new Date() + "::" + fut.get());
        } catch (InterruptedException | ExecutionException ep) {
            ep.printStackTrace();
        }
    }
    //shut down the executor service now
    executor.shutdown();

}

From source file:com.espertech.esper.multithread.TestMTContextListenerDispatch.java

private void tryPerformanceDispatch(int numThreads, int numRepeats) throws Exception {
    MyListener listener = new MyListener();
    engine.getEPAdministrator().getStatement("select").addListener(listener);

    List<Object>[] events = new ArrayList[numThreads];
    int eventId = 0;
    for (int threadNum = 0; threadNum < numThreads; threadNum++) {
        events[threadNum] = new ArrayList<Object>();
        for (int eventNum = 0; eventNum < numRepeats; eventNum++) {
            // range: 1 to 1000
            int partition = (int) (Math.random() * 50);
            eventId++;/*from   w  w w. jav a 2s .com*/
            events[threadNum].add(new SupportBean(new Integer(partition).toString(), eventId));
        }
    }

    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future futures[] = new Future[numThreads];
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < numThreads; i++) {
        Callable callable = new SendEventCallable(i, engine, events[i].iterator());
        futures[i] = threadPool.submit(callable);
    }
    for (Future future : futures) {
        assertEquals(true, future.get());
    }
    long delta = System.currentTimeMillis() - startTime;

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    // print those events not received
    for (List<Object> eventList : events) {
        for (Object event : eventList) {
            if (!listener.getBeans().contains(event)) {
                log.info("Expected event was not received, event " + event);
            }
        }
    }

    assertEquals(numRepeats * numThreads, listener.getBeans().size());
    assertTrue("delta=" + delta, delta < 500);
}

From source file:com.gs.collections.impl.jmh.AnagramBagTest.java

@Setup
public void setUp() {
    this.executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}

From source file:edu.illinois.ncsa.versus.engine.impl.ComprehensiveEngine.java

public ComprehensiveEngine() {
    newFixedThreadPool = Executors.newFixedThreadPool(EXECUTION_THREADS);
}

From source file:rk.java.compute.cep.ComputeService.java

public ComputeService(final int numberOfTickSources, final int threadPoolSize, IPriceEventSink eventBus) {
    this(new LinkedBlockingQueue<IPriceTick>(), numberOfTickSources, eventBus);
    executorService = Executors.newFixedThreadPool(threadPoolSize);
    ecs = new ExecutorCompletionService<StopWatch>(executorService);
}

From source file:gdsc.smlm.ij.plugins.MedianFilter.java

public void run(ImageProcessor ip) {
    long start = System.currentTimeMillis();

    ImageStack stack = imp.getImageStack();

    final int width = stack.getWidth();
    final int height = stack.getHeight();
    size = width * height;//from  w w  w . j  a  v  a2  s. co m
    float[][] imageStack = new float[stack.getSize()][];
    float[] mean = new float[imageStack.length];

    // Get the mean for each frame and normalise the data using the mean
    ExecutorService threadPool = Executors.newFixedThreadPool(Prefs.getThreads());
    List<Future<?>> futures = new LinkedList<Future<?>>();

    counter = 0;
    IJ.showStatus("Calculating means...");
    for (int n = 1; n <= stack.getSize(); n++) {
        futures.add(threadPool.submit(new ImageNormaliser(stack, imageStack, mean, n)));
    }

    // Finish processing data
    Utils.waitForCompletion(futures);

    futures = new LinkedList<Future<?>>();

    counter = 0;
    IJ.showStatus("Calculating medians...");
    for (int i = 0; i < size; i += blockSize) {
        futures.add(
                threadPool.submit(new ImageGenerator(imageStack, mean, i, FastMath.min(i + blockSize, size))));
    }

    // Finish processing data
    Utils.waitForCompletion(futures);

    if (Utils.isInterrupted())
        return;

    if (subtract) {
        counter = 0;
        IJ.showStatus("Subtracting medians...");
        for (int n = 1; n <= stack.getSize(); n++) {
            futures.add(threadPool.submit(new ImageFilter(stack, imageStack, n)));
        }

        // Finish processing data
        Utils.waitForCompletion(futures);
    }

    // Update the image
    ImageStack outputStack = new ImageStack(stack.getWidth(), stack.getHeight(), stack.getSize());
    for (int n = 1; n <= stack.getSize(); n++) {
        outputStack.setPixels(imageStack[n - 1], n);
    }

    imp.setStack(outputStack);
    imp.updateAndDraw();

    IJ.showTime(imp, start, "Completed");
    long milliseconds = System.currentTimeMillis() - start;
    Utils.log(TITLE + " : Radius %d, Interval %d, Block size %d = %s, %s / frame", radius, interval, blockSize,
            Utils.timeToString(milliseconds), Utils.timeToString((double) milliseconds / imp.getStackSize()));
}

From source file:eu.serco.dhus.plugin.olcil1eo.OlciL1EoPlugin.java

public OlciL1EoPlugin() {
    executor = Executors.newFixedThreadPool(MAX_THREADS_NUMBER);
    map = new HashMap<String, String>();
    map.put("OL_1_EFR___", "OL_1_EO");
    map.put("OL_1_ERR___", "OL_1_EO");
    map.put("OL_1_RAC___", "OL_1_RAC");
    map.put("OL_1_SPC___", "OL_1_SPC");
    map.put("OL_2_WFR___", "OL_2");
    map.put("OL_2_WRR___", "OL_2");
    map.put("OL_2_LFR___", "OL_2");
    map.put("OL_2_LRR___", "OL_2");

    try {/*from   w ww.jav  a 2 s.c  om*/

        loadTaskTables();
        loadSelectionRulesProperties();
        externalDHuSUrl = ConfigurationManager.getExternalDHuSHost();
        hashedString = ConfigurationManager.getHashedConnectionString();
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (SAXException saxe) {
        saxe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}