Example usage for java.util.concurrent ThreadPoolExecutor setMaximumPoolSize

List of usage examples for java.util.concurrent ThreadPoolExecutor setMaximumPoolSize

Introduction

In this page you can find the example usage for java.util.concurrent ThreadPoolExecutor setMaximumPoolSize.

Prototype

public void setMaximumPoolSize(int maximumPoolSize) 

Source Link

Document

Sets the maximum allowed number of threads.

Usage

From source file:org.apache.hama.graph.GraphJobRunner.java

/**
 * Loads vertices into memory of each peer.
 *//*w  w w  . j  ava 2s  .c  o  m*/
@SuppressWarnings("unchecked")
private void loadVertices(BSPPeer<Writable, Writable, Writable, Writable, GraphJobMessage> peer)
        throws IOException, SyncException, InterruptedException {

    for (int i = 0; i < peer.getNumPeers(); i++) {
        partitionMessages.put(i, new GraphJobMessage());
    }

    VertexInputReader<Writable, Writable, V, E, M> reader = (VertexInputReader<Writable, Writable, V, E, M>) ReflectionUtils
            .newInstance(conf.getClass(Constants.RUNTIME_PARTITION_RECORDCONVERTER, VertexInputReader.class));

    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    executor.setMaximumPoolSize(conf.getInt(DEFAULT_THREAD_POOL_SIZE, 64));
    executor.setRejectedExecutionHandler(retryHandler);

    KeyValuePair<Writable, Writable> next = null;

    while ((next = peer.readNext()) != null) {
        Vertex<V, E, M> vertex = GraphJobRunner.<V, E, M>newVertexInstance(VERTEX_CLASS);

        boolean vertexFinished = false;
        try {
            vertexFinished = reader.parseVertex(next.getKey(), next.getValue(), vertex);
        } catch (Exception e) {
            throw new IOException("Parse exception occured: " + e);
        }

        if (!vertexFinished) {
            continue;
        }

        Runnable worker = new Parser(vertex);
        executor.execute(worker);

    }

    executor.shutdown();
    executor.awaitTermination(60, TimeUnit.SECONDS);

    Iterator<Entry<Integer, GraphJobMessage>> it;
    it = partitionMessages.entrySet().iterator();
    while (it.hasNext()) {
        Entry<Integer, GraphJobMessage> e = it.next();
        it.remove();
        GraphJobMessage msg = e.getValue();
        msg.setFlag(GraphJobMessage.PARTITION_FLAG);
        peer.send(getHostName(e.getKey()), msg);
    }

    peer.sync();

    executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    executor.setMaximumPoolSize(conf.getInt(DEFAULT_THREAD_POOL_SIZE, 64));
    executor.setRejectedExecutionHandler(retryHandler);

    GraphJobMessage msg;
    while ((msg = peer.getCurrentMessage()) != null) {
        executor.execute(new AddVertex(msg));
    }

    executor.shutdown();
    executor.awaitTermination(60, TimeUnit.SECONDS);

    LOG.info(vertices.size() + " vertices are loaded into " + peer.getPeerName());
}

From source file:org.geoserver.web.Start.java

public static void main(String[] args) {
    // don't even think of serving more than XX requests in parallel...
    // we have a limit in our processing and memory capacities
    ThreadPoolExecutor tp = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    tp.setMaximumPoolSize(50);

    Server server = null;//from   w ww .jav a2 s.c  o m
    ServerConnector conn = null;
    try {
        server = new Server(new ExecutorThreadPool(tp));

        // TODO pass properties file
        File properties = null;
        if (args.length == 1) {
            String propertiesFileName = args[0];
            if (!propertiesFileName.isEmpty()) {
                properties = new File(propertiesFileName);
            }
        } else {
            properties = new File("src/test/resources/jetty.properties");
        }
        Properties prop = loadProperties(properties);

        for (Object key : prop.keySet()) {
            String property = System.getProperty(key.toString());
            String envProp = System.getenv(key.toString());
            if (property != null) {
                prop.put(key, property);
            } else if (envProp != null) {
                prop.put(key, envProp);
            }
        }

        // load properties into system env
        setSystemProperties(prop);

        server.setHandler(configureContext(prop));

        conn = configureConnection(prop, server);

        server.setConnectors(new Connector[] { conn });

        server.start();

        // use this to test normal stop behavior, that is, to check stuff
        // that
        // need to be done on container shutdown (and yes, this will make
        // jetty stop just after you started it...)
        // jettyServer.stop();
    } catch (Throwable e) {
        log.error("Could not start the Jetty server: " + e.getMessage(), e);

        if (server != null) {
            try {
                server.stop();
            } catch (Exception e1) {
                log.error("Unable to stop the Jetty server:" + e1.getMessage(), e1);
            }
        }
        if (conn != null) {
            try {
                conn.stop();
            } catch (Exception e1) {
                log.error("Unable to stop the connection:" + e1.getMessage(), e1);
            }
        }
    }
}