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:com.alibaba.otter.shared.arbitrate.zookeeper.DistributedReentrantLockTest.java

@Test
protected void test_lock() {
    ExecutorService exeucotr = Executors.newCachedThreadPool();
    final int count = 50;
    final CountDownLatch latch = new CountDownLatch(count);

    final DistributedReentrantLock lock = new DistributedReentrantLock(dir);
    for (int i = 0; i < count; i++) {
        exeucotr.submit(new Runnable() {

            public void run() {
                try {
                    Thread.sleep(1000);
                    lock.lock();/*from  w w w  .j a va  2  s.co  m*/
                    Thread.sleep(100 + RandomUtils.nextInt(100));

                    System.out.println("id: " + lock.getId() + " is leader: " + lock.isOwner());
                } catch (InterruptedException e) {
                    want.fail();
                } catch (KeeperException e) {
                    want.fail();
                } finally {
                    latch.countDown();
                    try {
                        lock.unlock();
                    } catch (KeeperException e) {
                        want.fail();
                    }
                }

            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }

    exeucotr.shutdown();
}

From source file:jcuda.jcublas.kernel.TestMultipleThreads.java

@Test
public void testMultipleThreads() throws InterruptedException {
    int numThreads = 10;
    final INDArray array = Nd4j.rand(3000, 3000);
    final INDArray expected = array.dup().mmul(array).mmul(array).div(array).div(array);
    final AtomicInteger correct = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(numThreads);

    ExecutorService executors = Executors.newCachedThreadPool();

    for (int x = 0; x < numThreads; x++) {
        executors.execute(new Runnable() {
            @Override// w  w  w  .j  a  v  a  2 s .c  o  m
            public void run() {
                try {
                    int total = 10;
                    int right = 0;
                    for (int x = 0; x < total; x++) {
                        StopWatch watch = new StopWatch();
                        watch.start();
                        INDArray actual = array.dup().mmul(array).mmul(array).div(array).div(array);
                        watch.stop();
                        System.out.println("MMUL took " + watch.getTime());
                        if (expected.equals(actual))
                            right++;
                    }

                    if (total == right)
                        correct.incrementAndGet();
                } finally {
                    latch.countDown();
                }

            }
        });
    }

    latch.await();

    assertEquals(numThreads, correct.get());

}

From source file:com.microsoft.azure.servicebus.samples.deadletterqueue.DeadletterQueue.java

public void run(String connectionString) throws Exception {

    CompletableFuture<Void> receiveTask;
    CompletableFuture<Void> fixUpTask;
    IMessageSender sendClient;/*from w  ww.ja v  a  2 s .com*/

    sendClient = ClientFactory.createMessageSenderFromConnectionStringBuilder(
            new ConnectionStringBuilder(connectionString, "BasicQueue"));

    // max delivery-count scenario
    this.sendMessagesAsync(sendClient, 1).join();
    this.exceedMaxDelivery(connectionString, "BasicQueue").join();

    // fix-up scenario
    this.sendMessagesAsync(sendClient, Integer.MAX_VALUE);
    ExecutorService executorService = Executors.newCachedThreadPool();
    receiveTask = this.receiveMessagesAsync(connectionString, "BasicQueue", executorService);
    fixUpTask = this.PickUpAndFixDeadletters(connectionString, "BasicQueue", sendClient, executorService);

    // wait for ENTER or 10 seconds elapsing
    waitForEnter(10);

    receiveTask.cancel(true);
    fixUpTask.cancel(true);

    CompletableFuture.allOf(sendClient.closeAsync(), receiveTask.exceptionally(t -> {
        if (t instanceof CancellationException) {
            return null;
        }
        throw new RuntimeException((Throwable) t);
    }), fixUpTask.exceptionally(t -> {
        if (t instanceof CancellationException) {
            return null;
        }
        throw new RuntimeException((Throwable) t);
    })).join();

    executorService.shutdown();
}

From source file:fr.inria.edelweiss.kgdqp.core.CentralizedInferrencing.java

public static void main(String args[])
        throws ParseException, EngineException, InterruptedException, IOException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//from www  . j  a  v a 2  s .c o m
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    /////////////////
    Graph graph = Graph.create();
    QueryProcess exec = QueryProcess.create(graph);

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    //        Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    //        Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option locOpt = new Option("c", "centralized", false, "performs centralized inferences");
    Option dataOpt = new Option("l", "load", true, "data file or directory to be loaded");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    //        options.addOption(queryOpt);
    //        options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);
    //        options.addOption(locOpt);
    options.addOption(dataOpt);

    String header = "Corese/KGRAM rule engine experiment command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    }

    if (cmd.hasOption("l")) {
        String[] dataPaths = cmd.getOptionValues("l");
        for (String path : dataPaths) {
            Load ld = Load.create(graph);
            ld.load(path);
            logger.info("Loaded " + path);
        }
    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            logger.info("Loading " + r.getAbsolutePath());
            ld.load(r.getAbsolutePath());
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(exec);
    ruleEngine.setOptimize(true);
    ruleEngine.setConstructResult(true);
    ruleEngine.setTrace(true);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : ");
        //            System.out.println("-------");
        //            System.out.println(rule);
        //            System.out.println("");
        //            if (! rule.toLowerCase().contains("sameas")) {
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
        //            }
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        //            System.out.println("******************************");
        //            System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        //            System.out.println("Rule engine running for " + sw.getTime() + " ms");
        //            System.out.println("Federated graph size : " + graph.size());
        System.out.println(sw.getTime() + " , " + graph.size());
        Thread.sleep(5000);
    }

    logger.info("Federated graph size : " + graph.size());
    //        logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    //        TripleFormat f = TripleFormat.create(graph, true);
    //        f.write("/tmp/gAll.ttl");

}

From source file:net.brtly.monkeyboard.shell.ShellRunner.java

private ShellRunner() {
    _commandPool = Executors.newCachedThreadPool();
}

From source file:com.ljn.msg.lengthfield.Server.java

public void run() {
    // Configure the server.
    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    // Set up the pipeline factory.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();

            int maxFrameLength = 8192;
            int lengthFieldOffset = 0;
            int lengthAdjustment = 0;
            int lengthFieldLength = 1;
            int initialBytesToStrip = 1;
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(maxFrameLength, lengthFieldOffset,
                    lengthFieldLength, lengthAdjustment, initialBytesToStrip));
            pipeline.addLast("stringDecoder", new StringDecoder(Charsets.UTF_8));
            pipeline.addLast("serverHandler", new ServerHandler());

            return pipeline;
        }//from  ww w.jav a2 s.  co  m
    });

    // Bind and start to accept incoming connections.
    bootstrap.bind(new InetSocketAddress(port));
}

From source file:net.jmhertlein.mcanalytics.api.APISocket.java

public APISocket(PrintWriter out, BufferedReader in) {
    nextID = 0;/*  www  .j ava  2 s  .  co  m*/
    requests = new ConcurrentHashMap<>();
    this.out = out;
    this.in = in;
    workers = Executors.newCachedThreadPool();
}

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

public void testRun() throws Exception {
    idCounter = new AtomicLong(0);
    executorService = Executors.newCachedThreadPool();
    noActionUpdateListener = new NoActionUpdateListener();

    Configuration epConfig = new Configuration();
    epConfig.addEventType(SupportBean.class);
    epConfig.getEngineDefaults().getThreading()
            .setInsertIntoDispatchLocking(ConfigurationEngineDefaults.Threading.Locking.SUSPEND);

    final EPServiceProvider epServiceProvider = EPServiceProviderManager.getDefaultProvider(epConfig);
    epServiceProvider.initialize();//from  ww w. j av a2s .  c om

    epAdministrator = epServiceProvider.getEPAdministrator();
    epRuntime = epServiceProvider.getEPRuntime();

    epAdministrator.startAllStatements();

    String epl = "insert into Stream1 select count(*) as cnt from SupportBean.win:time(7 sec)";
    createEPL(epl, noActionUpdateListener);
    epl = epl + " output every 10 seconds";
    createEPL(epl, noActionUpdateListener);

    SendEventRunnable sendTickEventRunnable = new SendEventRunnable(10000);
    start(sendTickEventRunnable, 4);

    // Adjust here for long-running test
    Thread.sleep(3000);

    executorService.shutdown();
    executorService.awaitTermination(1, TimeUnit.SECONDS);
}

From source file:be.solidx.hot.test.data.jdbc.TestPyAsyncCollectionApi.java

@PostConstruct
public void init() {
    asyncDB = new PyAsyncDB(db, Executors.newCachedThreadPool(), Executors.newSingleThreadExecutor());
}

From source file:net.sheehantech.cherry.pool.KeyedPooledPushClient.java

public KeyedPooledPushClient(Map<K, SSLContext> sslContexts, Map<K, String> gateways, Map<K, Integer> ports) {
    super(sslContexts, gateways, ports);
    executor = Executors.newCachedThreadPool();
}