Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService execute.

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:net.wequick.small.Bundle.java

private static void loadBundles(List<Bundle> bundles) {
    sPreloadBundles = bundles;// ww w  .j  a va2 s.c o m

    // Prepare bundle
    for (Bundle bundle : bundles) {
        bundle.prepareForLaunch();
    }

    // Handle I/O
    if (sIOActions != null) {
        ExecutorService executor = Executors.newFixedThreadPool(sIOActions.size());
        for (Runnable action : sIOActions) {
            executor.execute(action);
        }
        executor.shutdown();
        try {
            if (!executor.awaitTermination(LOADING_TIMEOUT_MINUTES, TimeUnit.MINUTES)) {
                throw new RuntimeException(
                        "Failed to load bundles! (TIMEOUT > " + LOADING_TIMEOUT_MINUTES + "minutes)");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sIOActions = null;
    }

    // Notify `postSetUp' to all launchers
    for (BundleLauncher launcher : sBundleLaunchers) {
        launcher.postSetUp();
    }

    // Free all unused temporary variables
    for (Bundle bundle : bundles) {
        if (bundle.parser != null) {
            bundle.parser.close();
            bundle.parser = null;
        }
        bundle.mBuiltinFile = null;
        bundle.mExtractPath = null;
    }
}

From source file:org.apache.accumulo.server.util.VerifyTabletAssignments.java

private static void checkTable(final String user, final String pass, String table, HashSet<KeyExtent> check,
        boolean verbose)
        throws AccumuloException, AccumuloSecurityException, TableNotFoundException, InterruptedException {

    if (check == null)
        System.out.println("Checking table " + table);
    else//from   w w w. ja  v  a 2s .com
        System.out.println("Checking table " + table + " again, failures " + check.size());

    Map<KeyExtent, String> locations = new TreeMap<KeyExtent, String>();
    SortedSet<KeyExtent> tablets = new TreeSet<KeyExtent>();

    MetadataTable.getEntries(HdfsZooInstance.getInstance(),
            new AuthInfo(user, ByteBuffer.wrap(pass.getBytes()), HdfsZooInstance.getInstance().getInstanceID()),
            table, false, locations, tablets);

    final HashSet<KeyExtent> failures = new HashSet<KeyExtent>();

    for (KeyExtent keyExtent : tablets)
        if (!locations.containsKey(keyExtent))
            System.out.println(" Tablet " + keyExtent + " has no location");
        else if (verbose)
            System.out.println(" Tablet " + keyExtent + " is located at " + locations.get(keyExtent));

    Map<String, List<KeyExtent>> extentsPerServer = new TreeMap<String, List<KeyExtent>>();

    for (Entry<KeyExtent, String> entry : locations.entrySet()) {
        List<KeyExtent> extentList = extentsPerServer.get(entry.getValue());
        if (extentList == null) {
            extentList = new ArrayList<KeyExtent>();
            extentsPerServer.put(entry.getValue(), extentList);
        }

        if (check == null || check.contains(entry.getKey()))
            extentList.add(entry.getKey());
    }

    ExecutorService tp = Executors.newFixedThreadPool(20);

    for (final Entry<String, List<KeyExtent>> entry : extentsPerServer.entrySet()) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    checkTabletServer(user, ByteBuffer.wrap(pass.getBytes()), entry, failures);
                } catch (Exception e) {
                    System.err.println("Failure on ts " + entry.getKey() + " " + e.getMessage());
                    e.printStackTrace();
                    failures.addAll(entry.getValue());
                }
            }

        };

        tp.execute(r);
    }

    tp.shutdown();

    while (!tp.awaitTermination(1, TimeUnit.HOURS)) {
    }

    if (failures.size() > 0)
        checkTable(user, pass, table, failures, verbose);
}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void printRdf(final CommandLine cmd, final Map<String, Double> stats) {
    final File outDir = getOutputDirectory(cmd);
    final RDFWriter writer = getWriter(cmd);
    final ProgressMonitor monitor = getProgressMonitor();
    Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();
    ExecutorService pool = null;
    if (outDir != null) {
        pool = getThreadPool(cmd);//from   w ww.j a v a2 s .  co  m
    } else {
        // if output is going to the STDOUT then we need to do process in
        // sequential mode.
        pool = Executors.newSingleThreadExecutor();
    }

    final Object lock = new Object();

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                OutputStream out = System.out;
                PdbXmlParser parser = new PdbXmlParser();
                PdbRdfModel model = null;
                try {
                    if (cmd.hasOption("detailLevel")) {
                        try {
                            DetailLevel detailLevel = Enum.valueOf(DetailLevel.class,
                                    cmd.getOptionValue("detailLevel"));
                            model = parser.parse(input, new PdbRdfModel(), detailLevel);
                        } catch (IllegalArgumentException e) {
                            LOG.fatal("Invalid argument value for detailLevel option", e);
                            System.exit(1);
                        }
                    } else {
                        model = parser.parse(input, new PdbRdfModel());
                    }
                    // add the input file information
                    model.addInputFileInformation();
                    // add the outputFile information();
                    model.addRDFFileInformation();
                    if (outDir != null) {
                        File directory = new File(outDir, model.getPdbId().substring(1, 3));
                        synchronized (lock) {
                            if (!directory.exists()) {
                                directory.mkdir();
                            }
                        }
                        File file = new File(directory, model.getPdbId() + ".rdf.gz");
                        out = new GZIPOutputStream(new FileOutputStream(file));
                    }
                    if (cmd.hasOption("format")) {
                        if (cmd.getOptionValue("format").equalsIgnoreCase("NQUADs")) {
                            Dataset ds = TDBFactory.createDataset();
                            ds.addNamedModel(model.getDatasetResource().toString(), model);
                            StringWriter sw = new StringWriter();
                            RDFDataMgr.write(sw, ds, Lang.NQUADS);

                            out.write(sw.toString().getBytes(Charset.forName("UTF-8")));
                            ds.close();

                        }
                    }

                    writer.write(model, out, null);

                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    String id = null;
                    if (model != null) {
                        id = model.getPdbId();
                    }
                    LOG.error("Unable to parse input for PDB: " + id, e);
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        LOG.error("Unable to close output stream", e);
                    }
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void load(CommandLine cmd, final Map<String, Double> stats) {
    String username = "dba";
    String password = "dba";
    String host = "localhost";
    int port = 1111;
    DetailLevel detailLevel = null;/*from  ww w. j  a  va2s.co m*/
    if (cmd.hasOption("detailLevel")) {
        try {
            detailLevel = Enum.valueOf(DetailLevel.class, cmd.getOptionValue("detailLevel"));
        } catch (IllegalArgumentException e) {
            LOG.fatal("Invalid argument value for detailLevel option", e);
            System.exit(1);
        }
    }
    final DetailLevel f_detailLevel = detailLevel;

    if (cmd.hasOption("username")) {
        username = cmd.getOptionValue("username");
    }
    if (cmd.hasOption("password")) {
        password = cmd.getOptionValue("password");
    }
    if (cmd.hasOption("host")) {
        host = cmd.getOptionValue("host");
    }
    if (cmd.hasOption("port")) {
        try {
            port = Integer.parseInt(cmd.getOptionValue("port"));
        } catch (NumberFormatException e) {
            LOG.fatal("Invalid port number: " + cmd.getOptionValue("port"));
            System.exit(1);
        }
    }

    final VirtuosoDaoFactory factory = new VirtuosoDaoFactory(host, port, username, password);
    ExecutorService pool = getThreadPool(cmd);

    final ProgressMonitor monitor = getProgressMonitor();
    final Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();

    if (monitor != null) {
        monitor.setProgress(0, inputSize);
    }

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            public void run() {
                PdbXmlParser parser = new PdbXmlParser();
                UriBuilder uriBuilder = new UriBuilder();
                PdbRdfModel model = null;
                try {
                    model = new VirtPdbRdfModel(factory, Bio2RdfPdbUriPattern.PDB_GRAPH, uriBuilder,
                            factory.getTripleStoreDao());
                    if (f_detailLevel != null) {
                        parser.parse(input, model, f_detailLevel);
                    } else {
                        parser.parse(input, model);
                    }
                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    LOG.error("Uanble to parse input for pdb=" + (model != null ? model.getPdbId() : "null"),
                            e);
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:ejp.examples.MultiThreadedWithConnectionPooling.java

static void execute(final DatabaseManager dbm) throws DatabaseException, InterruptedException {
    long time = System.currentTimeMillis();
    ExecutorService exec = Executors.newFixedThreadPool(100);

    System.out.println("\n\nWorking ...");

    Runnable runnable = new Runnable() {
        public void run() {
            for (int t = 0; t < 100; t++) {
                try {
                    new UpdateManager(dbm) {
                        public void run() throws DatabaseException {
                            for (int j = 0; j < 100; j++) {
                                saveObject(new Dog(String.valueOf(Count.get()), Count.get()));
                                Count.count();
                            }/* w  w  w .  j av a2 s . co m*/
                        }
                    }.executeBatchUpdates();
                } catch (DatabaseException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    for (int i = 0; i < 100; i++) {
        exec.execute(runnable);
    }

    exec.shutdown();
    exec.awaitTermination(100, TimeUnit.SECONDS);
    time = (System.currentTimeMillis() - time) / 1000;

    System.out.println("\n\n" + Count.count + " dogs added to database in " + time + " seconds");

    Long count = ((Collection<Long>) dbm.executeQuery(new ArrayList<Long>(), true, "select count(*) from dog"))
            .toArray(new Long[1])[0];
    System.out.println("select count(*) from dog = " + count);
}

From source file:com.laudandjolynn.mytv.Main.java

/**
 * ?/*from w ww  .ja  v a2  s.  c  om*/
 * 
 * @param data
 */
private static void runCrawlTask(final MyTvData data, final TvService tvService) {
    CrawlEventListener listener = null;
    final String today = DateUtils.today();
    final ExecutorService executorService = Executors.newFixedThreadPool(Constant.CPU_PROCESSOR_NUM,
            new BasicThreadFactory.Builder().namingPattern("Mytv_Crawl_Program_Table_%d").build());
    if (!data.isProgramCrawlerInited()) {
        listener = new CrawlEventListenerAdapter() {
            @Override
            public void itemFound(CrawlEvent event) {
                if (event instanceof TvStationFoundEvent) {
                    final TvStation item = (TvStation) ((TvStationFoundEvent) event).getItem();
                    if (!tvService.isInMyTv(item) || CrawlAction.getIntance().isInQuerying(item, today)) {
                        return;
                    }
                    executorService.execute(new Runnable() {

                        @Override
                        public void run() {
                            CrawlAction.getIntance().queryProgramTable(item, today);
                        }
                    });
                }
            }
        };
    }

    // ???
    logger.info("It is trying to find some proxyies.");
    MyTvProxyManager.getInstance().prepareProxies(new ConfigProxy());
    logger.info("found " + MyTvProxyManager.getInstance().getProxySize() + " proxies.");

    if (!data.isStationCrawlerInited()) {
        // ?
        tvService.crawlAllTvStation(listener);

        // ??
        String[] weeks = DateUtils.getWeek(new Date(), "yyyy-MM-dd");
        List<TvStation> stationList = tvService.getDisplayedTvStation();
        for (String date : weeks) {
            if (date.compareTo(today) >= 1) {
                crawlAllProgramTable(stationList, executorService, date, tvService);
            }
        }
        executorService.shutdown();
        data.writeData(null, Constant.XML_TAG_STATION, "true");
        data.writeData(null, Constant.XML_TAG_PROGRAM, "true");
    }
}

From source file:it.geosolutions.tools.io.file.Copy.java

/**
 * /*from   ww  w.  java2s .  co m*/
 * @param ex
 * @param source
 * @param destination
 * @param seconds
 * @return
 * @throws RejectedExecutionException
 *             - if this task cannot be accepted for execution.
 * @throws IllegalArgumentException
 *             - if executor is null or terminated.
 */
public static FutureTask<File> asynchFileCopyToNFS(final ExecutorService ex, final File source,
        final File destination, final int seconds) throws RejectedExecutionException, IllegalArgumentException {
    if (ex == null || ex.isTerminated()) {
        throw new IllegalArgumentException(
                "Unable to run asynchronously using a terminated or null ThreadPoolExecutor");
    }

    final Callable<File> call = new Callable<File>() {
        public File call() throws Exception {
            return Copy.copyFileToNFS(source, destination, seconds);
        }
    };
    //
    final FutureTask<File> futureFile = new FutureTask<File>(call);
    ex.execute(futureFile);
    return futureFile;
    // return ex.submit(call);
}

From source file:com.comcast.cats.vision.concurrent.RemoteWorkerTest.java

public void example() throws InterruptedException {
    CatsEventDispatcher dispatcher = new CatsEventDispatcherImpl();

    Settop settop = new MockSettop() {
        private static final long serialVersionUID = 1L;

        public boolean pressKey(RemoteCommand command) {
            LOGGER.info("pressKey");
            return true;
        }//from ww  w . j a  va  2 s  .  co m
    };

    dispatcher.addListener(this, CatsEventType.REMOTE_RESPONSE);

    RemoteEvent evt = new RemoteEvent(ActionType.PRESS, RemoteCommand.GUIDE, "10", null);
    ExecutorService executor = Executors.newFixedThreadPool(5);

    executor.execute(new PressKeyWorker(settop, evt, dispatcher));
    executor.awaitTermination(5, TimeUnit.SECONDS);
}

From source file:com.google.cloud.bigtable.hbase.ManyThreadDriver.java

private static void runTest(String projectId, String instanceId, final String tableName) throws Exception {
    Configuration configuration = new Configuration();
    configuration.set("hbase.client.connection.impl", "com.google.cloud.bigtable.hbase1_0.BigtableConnection");
    configuration.set("google.bigtable.project.id", projectId);
    configuration.set("google.bigtable.instance.id", instanceId);
    try (Connection connection = ConnectionFactory.createConnection(configuration)) {
        Admin admin = connection.getAdmin();

        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        descriptor.addFamily(new HColumnDescriptor("cf"));
        try {//from   w w w.ja  v  a 2  s .  c o m
            admin.createTable(descriptor);
        } catch (IOException ignore) {
            // Soldier on, maybe the table already exists.
        }

        final byte[] value = Bytes.toBytes(RandomStringUtils
                .randomAlphanumeric(Integer.parseInt(System.getProperty("valueSize", "1024"))));

        int numThreads = Integer.parseInt(System.getProperty("numThreads", "1000"));
        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        for (int i = 0; i < numThreads; i++) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        final Table table = connection.getTable(TableName.valueOf(tableName));

                        while (true) {
                            // Workload: two reads and a write.
                            table.get(new Get(Bytes.toBytes(key())));
                            table.get(new Get(Bytes.toBytes(key())));
                            Put p = new Put(Bytes.toBytes(key()));
                            p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col"), value);
                            table.put(p);
                        }
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
            };
            executor.execute(r);
        }

        // TODO Make a parameter
        executor.awaitTermination(48, TimeUnit.HOURS);
    }
}

From source file:org.grycap.gpf4med.StudyManager.java

public void create(final String graph, final ImmutableList<URL> urls) {
    checkArgument(StringUtils.isNotBlank(graph), "Uninitialized or invalid graph");
    checkArgument(urls != null, "Uninitialized URLs");
    // find a suitable graph connector
    final GraphConnector connector = GraphConnectorManager.INSTANCE.getConnector(graph);
    checkState(connector != null, "Unsupported graph: " + graph);
    // release any previous graph and set the total number of submitted reports
    destroy();//from  w  w  w.j  a  v a  2s . c  o  m
    GraphDatabaseHandler.INSTANCE.setConnector(connector);
    Statistics.INSTANCE.setTotalSubmitted(urls.size());
    // start asynchronously load
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
        @Override
        public void run() {
            DocumentFetcher.INSTANCE.fecth(urls);
        }
    });
}