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:be.solidx.hot.test.TestScriptExecutors.java

@SuppressWarnings("rawtypes")
private Collection<Long> multiThreadedTest(final Script script, final int max,
        final ScriptExecutor scriptExecutor) throws InterruptedException {
    final int iterations = 100;
    ExecutorService executor = Executors.newFixedThreadPool(8);
    final ConcurrentHashMap<String, Long> results = new ConcurrentHashMap<String, Long>();
    final ConcurrentHashMap<String, Long> avgs = new ConcurrentHashMap<String, Long>();
    long benchStart = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        Runnable runnable = new Runnable() {
            @SuppressWarnings("unchecked")
            @Override/*w  ww.  j ava 2s .com*/
            public void run() {
                try {
                    long res = 0;
                    Map<String, Object> parameters = new HashMap<String, Object>();
                    parameters.put("i", new Integer(max));
                    parameters.put("n", new Integer(0));

                    //long starting = System.currentTimeMillis();
                    Object object = scriptExecutor.execute(script, parameters);
                    if (object instanceof Bindings) {
                        Bindings bindings = (Bindings) object;
                        res = (Integer) bindings.get("result");
                        bindings.clear();
                    } else if (object instanceof Double) {
                        res = Math.round((Double) object);
                    } else if (object instanceof Long) {
                        res = (long) object;
                    } else
                        res = new Long((Integer) object);
                    long end = System.currentTimeMillis() - avgs.get(this.toString());
                    results.put(UUID.randomUUID().getLeastSignificantBits() + "", res);
                    avgs.put(this.toString(), end);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        avgs.put(runnable.toString(), System.currentTimeMillis());
        executor.submit(runnable);
    }

    while (results.size() < iterations) {
        Thread.sleep(50);
    }
    //Thread.sleep(20000);
    double sum = 0;
    for (Long value : avgs.values()) {
        sum += value;
    }
    System.out.println((sum / (double) iterations) + "");
    System.out.println("==== Time needed for all requests: " + (System.currentTimeMillis() - benchStart));
    results.remove("avg");
    executor = null;
    return results.values();
}

From source file:edu.iu.kmeans.regroupallgather.KMUtil.java

/**
 * Generate data and upload to the data dir.
 * /*from w  w w. j  a va 2  s . co m*/
 * @param numOfDataPoints
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {
        Future<?> f = service
                .submit(new DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));
        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
}

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

private void trySend(int numThreads, int numEventsPerThread, boolean indexShare) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.addEventType("SupportBean", SupportBean.class);
    config.addPlugInAggregationFunction("intListAgg", MyIntListAggregation.class.getName());
    config.getEngineDefaults().getEventMeta()
            .setDefaultEventRepresentation(Configuration.EventRepresentation.MAP); // use Map-type events for testing
    engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();//from   w ww  .  j av a2 s.  c o  m

    // setup statements
    engine.getEPAdministrator().createEPL("create schema UpdateEvent as (uekey string, ueint int)");
    engine.getEPAdministrator().createEPL("create schema WindowSchema as (wskey string, wsint int)");

    String createEpl = "create window MyWindow.win:keepall() as WindowSchema";
    if (indexShare) {
        createEpl = "@Hint('enable_window_subquery_indexshare') " + createEpl;
    }
    EPStatement namedWindow = engine.getEPAdministrator().createEPL(createEpl);

    engine.getEPAdministrator().createEPL("create index ABC on MyWindow(wskey)");
    engine.getEPAdministrator()
            .createEPL("on UpdateEvent mue merge MyWindow mw " + "where uekey = wskey and ueint = wsint "
                    + "when not matched then insert select uekey as wskey, ueint as wsint "
                    + "when matched then delete");
    // note: here all threads use the same string key to insert/delete and different values for the int
    EPStatement targetStatement = engine.getEPAdministrator().createEPL(
            "select (select intListAgg(wsint) from MyWindow mw where wskey = sb.theString) as val from SupportBean sb");

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future<Boolean> future[] = new Future[numThreads];
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool
                .submit(new StmtNamedWindowSubqueryAggCallable(i, engine, numEventsPerThread, targetStatement));
    }

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

    // total up result
    for (int i = 0; i < numThreads; i++) {
        Boolean result = future[i].get();
        assertTrue(result);
    }

    EventBean[] events = EPAssertionUtil.iteratorToArray(namedWindow.iterator());
    assertEquals(0, events.length);
}

From source file:com.k42b3.aletheia.response.html.Images.java

public Images() {
    super();/* w w w  .ja  va2s  .  c  o  m*/

    executorService = Executors.newFixedThreadPool(6);

    // settings
    this.setTitle("Images");
    this.setLocation(100, 100);
    this.setPreferredSize(new Dimension(360, 600));
    this.setMinimumSize(this.getSize());
    this.setResizable(false);
    this.setLayout(new BorderLayout());

    // list
    model = new DefaultListModel<URL>();
    list = new JList<URL>(model);
    list.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            btnDownload.setEnabled(list.getSelectedIndex() != -1);
        }

    });
    list.setCellRenderer(new ImageCellRenderer());

    scp = new JScrollPane(list);
    scp.setBorder(new EmptyBorder(4, 4, 4, 4));

    this.add(scp, BorderLayout.CENTER);

    // buttons
    JPanel panelButtons = new JPanel();

    FlowLayout fl = new FlowLayout();
    fl.setAlignment(FlowLayout.LEFT);

    panelButtons.setLayout(fl);

    btnDownload = new JButton("Download");
    btnDownload.addActionListener(new DownloadHandler());
    btnDownload.setEnabled(false);
    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new CloseHandler());
    lblInfo = new JLabel("");

    panelButtons.add(btnDownload);
    panelButtons.add(btnCancel);
    panelButtons.add(lblInfo);

    this.add(panelButtons, BorderLayout.SOUTH);

    this.pack();
}

From source file:com.DGSD.DGUtils.ImageDownloader.ImageLoader.java

/**
 * This method must be called before any other method is invoked on this class. Please note that
 * when using ImageLoader as part of {@link WebImageView} or {@link WebGalleryAdapter}, then
 * there is no need to call this method, since those classes will already do that for you. This
 * method is idempotent. You may call it multiple times without any side effects.
 *
 * @param context// w  w w . j a  v  a2s.  c  o m
 *            the current context
 */
public static synchronized void initialize(Context context) {
    if (executor == null) {
        executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
    }
    if (imageCache == null) {
        imageCache = new ImageCache(25, expirationInMinutes, DEFAULT_POOL_SIZE);
        imageCache.enableDiskCache(context, ImageCache.DISK_CACHE_SDCARD);
    }
    if (httpClient == null) {
        setupHttpClient();
    }
}

From source file:de.saly.elasticsearch.importer.imap.mailsource.ParallelPollingIMAPMailSource.java

public ParallelPollingIMAPMailSource(final Properties props, final int threadCount, final String user,
        final String password) {
    super();//  ww w  .j a v  a 2 s  .c o m
    this.props = props;
    this.threadCount = threadCount < 1 ? 1 : threadCount;
    this.es = Executors.newFixedThreadPool(threadCount);

    this.user = user;
    this.password = password;
}

From source file:org.neo4j.ogm.integration.TransactionRequestHandlerTest.java

@Test
public void shouldBeAbleToStartMultipleConcurrentLongRunningTransactions() throws InterruptedException {

    SessionFactory sessionFactory = new SessionFactory();
    session = sessionFactory.openSession(neo4jRule.url());

    int numThreads = 100;

    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    CountDownLatch latch = new CountDownLatch(numThreads);

    for (int i = 0; i < numThreads; i++) {
        executor.submit(new TransactionStarter(latch));
    }//w w  w. j a v a 2  s .  co  m
    latch.await(); // pause until the count reaches 0
    System.out.println("all threads running");
    executor.shutdownNow();
}

From source file:com.mnxfst.testing.server.TSMain.java

public void execute(String[] args) {

    Options commandLineOptions = getTSClientCommandLineOptions();
    CommandLine commandLine = null;//from  w w w  .  ja  va  2  s. co m
    try {
        commandLine = parseCommandline(commandLineOptions, args);
    } catch (ParseException e) {
        System.out.println("Failed to parse command-line");
    }

    String hostname = null;
    try {
        hostname = extractStringValue(commandLine, CMD_OPT_HOSTNAME, CMD_OPT_HOSTNAME_SHORT);
    } catch (TSClientConfigurationException e) {
        printHelp(commandLineOptions, "Failed to parse host name from command-line");
        return;
    }

    int port = -1;
    try {
        port = extractIntValue(commandLine, CMD_OPT_PORT, CMD_OPT_PORT_SHORT);
    } catch (TSClientConfigurationException e) {
        printHelp(commandLineOptions, "Failed to parse port from command-line");
        return;
    }

    if (port < 1) {
        printHelp(commandLineOptions, "Failed to parse port from command-line");
        return;
    }

    int threadPoolSize = -1;
    try {
        threadPoolSize = extractIntValue(commandLine, CMD_OPT_THREAD_POOL_SIZE, CMD_OPT_THREAD_POOL_SIZE_SHORT);
    } catch (TSClientConfigurationException e) {
        threadPoolSize = -1;
    }

    System.out.println("ptest-server");
    System.out.println("hostname: " + hostname);
    System.out.println("port: " + port);
    System.out.println("server socket thread pool size: " + threadPoolSize);

    ChannelFactory channelFactory = null;
    if (threadPoolSize > 0)
        channelFactory = new NioServerSocketChannelFactory(Executors.newFixedThreadPool(threadPoolSize),
                Executors.newFixedThreadPool(threadPoolSize));
    else
        channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());

    ServerBootstrap serverBootstrap = new ServerBootstrap(channelFactory);
    serverBootstrap.setPipelineFactory(new TSPipelineFactory(hostname, port));
    serverBootstrap.setOption("child.tcpNoDelay", true);
    serverBootstrap.setOption("child.keepAlive", true);

    serverBootstrap.bind(new InetSocketAddress(port));

}

From source file:com.idocbox.flame.Helios.java

/**
 * fire them!/*from   w  w  w  .  j  a v a  2 s  .  com*/
 * @param ds       data source.
 * @param dsSpliter data source spliter.
 * @param mapper   mapper.
 * @param reducer  reducer.
 * @return
 */
public Collector<Map<K, V>> fire(JobConfig<K, V, T> jobConfig) {

    long start = System.currentTimeMillis();

    Collector<Map<K, V>> resultCollector = null;

    // data source.
    DataSource<T> dataSource = jobConfig.getDataSource();
    // data source spliter.
    DataSourceSpliter<T> dataSourceSpliter = jobConfig.getDataSourceSpliter();
    // mapper worker. root mapper worker.
    MapperWorker<K, V, T> mapperWorker = jobConfig.getMapperWorker();
    // reducer worker. root reducer worker.
    ReducerWorker<K, V> reducerWorker = jobConfig.getReducerWorker();
    // mapper.
    Mapper<K, V, T> mapper = jobConfig.getMapper();
    // reducer.
    Reducer<K, V> reducer = jobConfig.getReducer();
    // keeper.
    Keeper<Collector<Map<K, V>>> keeper = jobConfig.getKeeper();

    // spliting phase.

    //split data source into serveral data source.
    log.info("spliting datasource ...");
    Map<String, DataSource<T>> dsMap = dataSourceSpliter.split(dataSource);

    long m1 = System.currentTimeMillis();
    long cost1 = m1 - start;
    double seconds1 = cost1 / 1000;
    log.info("spliting datasource: cost " + seconds1 + " s");

    // generate worker for mapper.create()
    if (null == dsMap || dsMap.isEmpty()) {
        log.info("Splited data source is empty! exit flame!");
        return null;
    }

    // mapping phase.

    // generate mapper worker.
    log.info("mapping && reducing ...");
    Set<String> dsKeys = dsMap.keySet();
    //mapper thread size.
    int mapperThreadSize = dsKeys.size() > jobConfig.getMaxMapperWorker() ? jobConfig.getMaxMapperWorker()
            : dsKeys.size();
    //create mapper worker thread pool.
    ExecutorService mapperWorkerThreadPool = Executors.newFixedThreadPool(mapperThreadSize);
    int dataSourceSize = 0;
    for (String key : dsKeys) {
        //create mapper worker baby.
        MapperWorker<K, V, T> mapperWorkerBaby = mapperWorker.create(key);

        //assign data source and run the worker.
        DataSource<T> dsUnit = dsMap.get(key);
        if (null != dsUnit) {
            //execute mapper work in thread pool.
            mapperWorkerThreadPool
                    .execute(new MapperWorkerRunable<K, V, T>(mapperWorkerBaby, dsUnit, mapper, keeper));

            dataSourceSize++;
        }
    }
    //shutdown executor service.
    mapperWorkerThreadPool.shutdown();

    // reduce phase.

    //generate reducer worker, assign mapper worker's compute result
    // to reducer worker.

    //mapper thread size.
    //create reducer worker thread pool.
    ExecutorService reducerWorkerThreadPool = Executors.newFixedThreadPool(jobConfig.getMaxReducerWorker());

    //get 2 collector, merge them into one, then passed to reducer.
    Set<ReducerWorker<K, V>> reducerWorkers = new HashSet<ReducerWorker<K, V>>();
    int j = 0;
    int expectedReducTime = dataSourceSize - 1;
    while (true) {//reduce while there is more than one element in set.
        if (mapperWorkerThreadPool.isTerminated()) {
            int count = keeper.count();
            if (count == 0) {//no mapped result.
                log.info("there is no result given by mapper. exit!");
                return null;
            }
        }
        if (j == expectedReducTime) {
            log.info("complete reduce. exit flame.");
            break;
        }

        Set<Collector<Map<K, V>>> collectors = new HashSet<Collector<Map<K, V>>>(2);
        collectors.add(keeper.take());
        collectors.add(keeper.take());

        // get an idle worker.
        ReducerWorker<K, V> reducerWorkerBaby = chooseIdle(reducerWorkers, reducerWorker);

        log.info("reducing, collector size = " + keeper.size());

        reducerWorkerThreadPool
                .execute(new ReducerWorkerRunnable<K, V>(reducerWorkerBaby, collectors, reducer, keeper));

        j++;
    }

    //shutdown reducer worker thread pool.
    reducerWorkerThreadPool.shutdown();

    // collect result phase.
    while (!reducerWorkerThreadPool.isTerminated()) {
        Thread.yield();
    }
    if (null != keeper && keeper.size() == 1) {
        resultCollector = keeper.poll();
    } else {// error occured.
        int size = 0;
        if (null != keeper) {
            size = keeper.size();
        }
        log.info("after reduce, the result collector is not expected! collector size is " + size);
    }

    //return result collector.
    long end = System.currentTimeMillis();
    long cost = end - m1;
    double seconds = cost / 1000;
    log.info("mapping & reducing: cost " + seconds + " s");

    return resultCollector;
}

From source file:io.fabric8.kubernetes.pipeline.BuildImageStepExecution.java

@Override
protected ImageInspect run() throws Exception {
    return workspace.getChannel().call(new MasterToSlaveCallable<ImageInspect, Exception>() {
        @Override//from  w w  w. j a v  a  2 s  .  c  om
        public ImageInspect call() throws Exception {
            ExecutorService executorService = Executors.newFixedThreadPool(2);
            try {
                Future<Boolean> createTarFuture;
                Future<ImageInspect> buildImageFuture;
                try (PipedInputStream pin = new PipedInputStream();
                        PipedOutputStream pout = new PipedOutputStream(pin)) {

                    createTarFuture = executorService.submit(new CreateTarTask(pout));
                    buildImageFuture = executorService.submit(new BuildImageTask(pin));
                }

                //Wait for the two tasks to complete.
                if (!createTarFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS)) {
                    listener.getLogger().println("Failed to create docker image tarball.");
                }

                ImageInspect imageInspect = buildImageFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS);
                if (imageInspect == null) {
                    throw new RuntimeException("Failed to build docker image.");
                } else {
                    return imageInspect;
                }
            } finally {
                executorService.shutdown();
                if (executorService.awaitTermination(30, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            }
        }
    });
}