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:de.citec.csra.highlight.HighlightService.java

public static void main(String[] args)
        throws InitializeException, RSBException, InterruptedException, ParseException {

    Options opts = new Options();
    opts.addOption("scope", true, "RSB scope for highlight targets.\nDefault: '" + scope + "'");
    opts.addOption("server", true, "RSB server for configuration, e.g., tokens.\nDefault: '" + cfg + "'");
    opts.addOption("help", false, "Print this help and exit");

    String footer = null;//w  w  w .j  av  a2s  .c o m
    //      String footer = "\nThe following sub-scopes are registered automatically:\n"
    //            + "\n.../preset for color presets:\n" + Arrays.toString(ColorConfig.values())
    //            + "\n.../color for color values:\n" + "HSV (comma separated)"
    //            + "\n.../power for power states:\n" + Arrays.toString(PowerState.State.values())
    //            + "\n.../history for history commands:\n" + Arrays.toString(ColorHistory.values());

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(opts, args);
    if (cmd.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("csra-highlight-service [OPTION...]", "where OPTION includes:", opts, footer);
        System.exit(0);
    }

    if (System.getenv().containsKey(SCOPEVAR)) {
        scope = System.getenv(SCOPEVAR);
    }

    String s = cmd.getOptionValue("scope");
    if (s != null) {
        scope = s;
    }
    scope = scope.replaceAll("/$", "");

    String c = cmd.getOptionValue("cfg");
    if (c != null) {
        cfg = c;
    }
    cfg = cfg.replaceAll("/$", "");

    Defaults.loadDefaults();
    ExecutorService exec = Executors.newFixedThreadPool(2);

    exec.submit(() -> {
        try {
            ConfigServer cfgServer = new ConfigServer(cfg);
            cfgServer.execute();
        } catch (RSBException ex) {
            LOG.log(Level.SEVERE, "Config server failed", ex);
        }
    });

    exec.submit(() -> {
        try {
            TaskServer server = new TaskServer(scope, new HighlightTaskHandler());
            server.execute();
        } catch (RSBException | InterruptedException ex) {
            LOG.log(Level.SEVERE, "Task server failed", ex);
        }
    });

}

From source file:org.openbaton.monitoring.interfaces.MonitoringPluginCaller.java

public static void main(String[] args)
        throws IOException, TimeoutException, NotFoundException, ExecutionException, InterruptedException {

    ExecutorService executor = Executors.newFixedThreadPool(3);

    class Exec implements Callable<Object> {

        @Override/*from   ww  w . j  av  a2 s  . c  om*/
        public Object call() throws Exception {
            List<String> hosts = new ArrayList<>();
            hosts.add("hostname1");
            hosts.add("hostname2");
            return null /* new MonitoringPluginCaller("zabbix").getMeasurementResults(hosts, new ArrayList<String>(), "")*/;
        }
    }

    long time = new Date().getTime();
    System.out.println("Time ---> " + time);
    Future fut1 = executor.submit(new Exec());
    Future fut2 = executor.submit(new Exec());

    System.out.println("2nd call");
    System.out.println(fut2.get());

    System.out.println("1st call");
    System.out.println(fut1.get());
    System.out.println("Time ---> " + (new Date().getTime() - time) / 1000);
}

From source file:co.mitro.core.util.RPCLogReplayer.java

public static void main(String[] args) throws IOException, InterruptedException, KeyManagementException,
        UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {

    List<Request> requests = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 0; i < args.length; ++i) {
        String filename = args[i];
        System.err.println("Reading file: " + filename);
        JsonRecordReader rr = JsonRecordReader.MakeFromFilename(filename);
        JsonRecordReader.JsonLog log;/*w w  w  .j  a  va2  s  .c o  m*/
        try {
            while (null != (log = rr.readJson())) {
                if (Strings.isNullOrEmpty(log.payload.transactionId) && !log.payload.implicitBeginTransaction) {
                    // read only transaction
                    requests.add(new Request(log.metadata.endpoint, log.payload));
                }
            }
        } catch (EOFException e) {
            System.err.println("unexpected end of file; skipping");
        }
    }

    // run the simulation for a while.
    long scaling = 1000;
    double requestsPerMs = 353. / 9805199;
    long START_MS = 0;
    // run for 20 min
    long END_MS = 20 * 60 * 1000;
    long now = START_MS;
    int count = 0;
    while (now < END_MS) {
        double toSleep = nextExp(requestsPerMs * scaling);
        now += toSleep;
        ++count;
        Thread.sleep((long) toSleep);
        executor.execute(new SendQuery(requests.get(RNG.nextInt(requests.size()))));
        System.out.println("count: " + count + "\t time:" + now + "\t rate:" + (double) count / now);
    }
    executor.awaitTermination(1, TimeUnit.MINUTES);

}

From source file:friendsandfollowers.FilesThreaderFriendsIDs.java

public static void main(String[] args) throws InterruptedException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length < 4)) {
        System.err.println("4 Parameters are required plus one optional " + "parameter to launch a Job.");
        System.err.println("First: String 'INPUT: /path/to/files/'");
        System.err.println("Second: String 'OUTPUT: /output/path/'");
        System.err.println("Third: (int) Total Number Of Jobs");
        System.err.println("Fourth: (int) Number of seconds to pause");
        System.err//from   w w  w  .j  av  a 2  s  .  c om
                .println("Fifth: (int) Number of ids to fetch. " + "Provide number which increment by 5000 eg "
                        + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids.");
        System.err.println("Example: fileToRun /input/path/ " + "/output/path/ 10 2 75000");
        System.exit(-1);
    }

    // to write output in a file
    ThreadPrintStream.replaceSystemOut();
    try {
        INPUT = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    try {
        OUTPUT = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(-1);
    }

    try {
        TOTAL_JOBS_STR = StringEscapeUtils.escapeJava(args[2]);
    } catch (Exception e) {
        System.err.println("Argument" + args[2] + " must be an integer.");
        System.exit(-1);
    }

    try {
        PAUSE_STR = StringEscapeUtils.escapeJava(args[3]);
    } catch (Exception e) {
        System.err.println("Argument" + args[3] + " must be an integer.");
        System.exit(-1);
    }

    if (args.length == 5) {
        try {
            IDS_TO_FETCH = StringEscapeUtils.escapeJava(args[4]);
        } catch (Exception e) {
            System.err.println("Argument" + args[4] + " must be an integer.");
            System.exit(-1);
        }
    }

    try {
        PAUSE = Integer.parseInt(args[3]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[3] + " must be an integer.");
        System.exit(-1);
    }

    try {
        TOTAL_JOBS = Integer.parseInt(TOTAL_JOBS_STR);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + TOTAL_JOBS_STR + " must be an integer.");
        System.exit(-1);
    }

    System.out.println("Going to launch jobs. " + "Please see logs files to track jobs.");

    ExecutorService threadPool = Executors.newFixedThreadPool(TOTAL_JOBS);

    for (int i = 0; i < TOTAL_JOBS; i++) {

        final String JOB_NO = Integer.toString(i);

        threadPool.submit(new Runnable() {

            public void run() {

                try {

                    // Creating a text file where System.out.println()
                    // will send its output for this thread.
                    String name = Thread.currentThread().getName();
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(name + "-logs.txt");
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                        System.exit(0);
                    }

                    // Create a PrintStream that will write to the new file.
                    PrintStream stream = new PrintStream(new BufferedOutputStream(fos));
                    // Install the PrintStream to be used as 
                    // System.out for this thread.
                    ((ThreadPrintStream) System.out).setThreadOut(stream);
                    // Output three messages to System.out.
                    System.out.println(name);
                    System.out.println();
                    System.out.println();

                    FilesThreaderFriendsIDs.execTask(INPUT, OUTPUT, TOTAL_JOBS_STR, JOB_NO, PAUSE_STR,
                            IDS_TO_FETCH);

                } catch (IOException | ClassNotFoundException | SQLException | JSONException e) {

                    // e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            }
        });

        helpers.pause(PAUSE);
    }
    threadPool.shutdown();
}

From source file:friendsandfollowers.FilesThreaderFollowersIDs.java

public static void main(String[] args) throws InterruptedException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length < 4)) {
        System.err.println("4 Parameters are required plus one optional " + "parameter to launch a Job.");
        System.err.println("First: String 'INPUT: /path/to/files/'");
        System.err.println("Second: String 'OUTPUT: /output/path/'");
        System.err.println("Third: (int) Total Number Of Jobs");
        System.err.println("Fourth: (int) Number of seconds to pause");
        System.err/*  w w w  . jav  a 2 s  .c  o m*/
                .println("Fifth: (int) Number of ids to fetch. " + "Provide number which increment by 5000 eg "
                        + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids.");
        System.err.println("Example: fileToRun /input/path/ " + "/output/path/ 10 2 75000");
        System.exit(-1);
    }

    // to write output in a file
    ThreadPrintStream.replaceSystemOut();
    try {
        INPUT = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    try {
        OUTPUT = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(-1);
    }

    try {
        TOTAL_JOBS_STR = StringEscapeUtils.escapeJava(args[2]);
    } catch (Exception e) {
        System.err.println("Argument" + args[2] + " must be an integer.");
        System.exit(-1);
    }

    try {
        PAUSE_STR = StringEscapeUtils.escapeJava(args[3]);
    } catch (Exception e) {
        System.err.println("Argument" + args[3] + " must be an integer.");
        System.exit(-1);
    }

    if (args.length == 5) {
        try {
            IDS_TO_FETCH = StringEscapeUtils.escapeJava(args[4]);
        } catch (Exception e) {
            System.err.println("Argument" + args[4] + " must be an integer.");
            System.exit(-1);
        }
    }

    try {
        PAUSE = Integer.parseInt(args[3]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[3] + " must be an integer.");
        System.exit(-1);
    }

    try {
        TOTAL_JOBS = Integer.parseInt(TOTAL_JOBS_STR);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + TOTAL_JOBS_STR + " must be an integer.");
        System.exit(-1);
    }

    System.out.println("Going to launch jobs. " + "Please see logs files to track jobs.");

    ExecutorService threadPool = Executors.newFixedThreadPool(TOTAL_JOBS);

    for (int i = 0; i < TOTAL_JOBS; i++) {

        final String JOB_NO = Integer.toString(i);

        threadPool.submit(new Runnable() {

            public void run() {

                try {

                    // Creating a text file where System.out.println()
                    // will send its output for this thread.
                    String name = Thread.currentThread().getName();
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(name + "-logs.txt");
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                        System.exit(0);
                    }

                    // Create a PrintStream that will write to the new file.
                    PrintStream stream = new PrintStream(new BufferedOutputStream(fos));

                    //                         Install the PrintStream to be used as 
                    //                         System.out for this thread.
                    ((ThreadPrintStream) System.out).setThreadOut(stream);

                    // Output three messages to System.out.
                    System.out.println(name);
                    System.out.println();
                    System.out.println();

                    FilesThreaderFollowersIDs.execTask(INPUT, OUTPUT, TOTAL_JOBS_STR, JOB_NO, PAUSE_STR,
                            IDS_TO_FETCH);

                } catch (IOException | ClassNotFoundException | SQLException | JSONException e) {

                    // e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            }
        });

        helpers.pause(PAUSE);
    }
    threadPool.shutdown();
}

From source file:fr.iphc.grid.jobmanager.JobManager.java

/**
 * @param args//from   www .ja  v a2s .c o  m
 */
public static void main(String[] args) throws Exception {
    JobManager command = new JobManager();
    CommandLine line = command.parse(args);
    ArrayList<File> JdlList = new ArrayList<File>();
    Global.getOutputexecutor = Executors.newFixedThreadPool(10);
    Initialize init = new Initialize();
    String SetupFile = "setup_vigrid.xml";

    if (line.hasOption(OPT_SETUP)) {
        SetupFile = line.getOptionValue(OPT_SETUP);
    }
    if ((new File(SetupFile).isFile())) {
        init.GlobalSetup(SetupFile);
    }
    // Init Job
    if (line.hasOption(OPT_JOB)) {
        File file = new File(line.getOptionValue(OPT_JOB));
        if ((file.isFile())) {
            JdlList.add(file);
        } else {
            System.err.println("The file " + file + " doesn't exist");
            System.exit(-1);
        }
    } else {
        File file = new File(line.getOptionValue(OPT_FILEJOB));
        if ((file.isFile())) {
            JdlList = init.InitJdl(file);
        } else {
            System.err.println("The file " + file + " doesn't exist");
            System.exit(-1);
        }
    }
    if (line.hasOption(OPT_WAIT)) {
        Global.TIMEOUTWAIT = Integer.parseInt(line.getOptionValue(OPT_WAIT));
    }
    if (line.hasOption(OPT_RUN)) {
        Global.TIMEOUTRUN = Integer.parseInt(line.getOptionValue(OPT_RUN));
    }
    if (line.hasOption(OPT_END)) {
        Global.TIMEOUTEND = Integer.parseInt(line.getOptionValue(OPT_END));
    }
    if (line.hasOption(OPT_LOGDISPLAY)) {
        Global.SEUILDISPLAYLOG = Float.parseFloat(line.getOptionValue(OPT_LOGDISPLAY));
    }
    init.InitJob(JdlList);
    // Init Url Ce
    if (line.hasOption(OPT_QUEUE)) {
        Global.file = new File(line.getOptionValue(OPT_QUEUE));
    }
    if (line.hasOption(OPT_BAD)) {
        Global.BadCe = new File(line.getOptionValue(OPT_BAD));
    }
    if (line.hasOption(OPT_OPTIMIZETIMEOUTRUN)) {
        Global.OPTTIMEOUTRUN = false;
    }
    if (line.hasOption(OPT_CWD)) {
        File theDir = new File(line.getOptionValue(OPT_CWD));
        if (!theDir.exists()) {
            if (!theDir.mkdirs()) {
                System.err.println("Working directory create failed: " + line.getOptionValue(OPT_CWD));
                System.exit(-1);
            }
        }
        Global.Cwd = line.getOptionValue(OPT_CWD);
    } else {
        Global.Cwd = System.getProperty("user.dir");
    }
    if (!(new File(Global.Cwd)).canWrite()) {
        System.err.println(" Write permission denied : " + Global.Cwd);
        System.exit(-1);
    }
    System.out.println("Current working directory : " + Global.Cwd);
    Date start = new Date();
    init.PrintGlobalSetup();
    init.InitUrl(Global.file);
    init.InitSosCe();
    init.rmLoadFailed(Global.Cwd + "/loadFailed.txt");
    System.out.println("CE: " + Global.ListUrl.size() + " Nb JOB: " + Global.ListJob.size() + " " + new Date());
    if (Global.ListJob.size() < 6) { // pour obtenir rapport de 0.8
        Global.OPTTIMEOUTRUN = false;
    }
    // check if we can connect to the grid
    try {
        SessionFactory.createSession(true);
    } catch (NoSuccessException e) {
        System.err.println("Could not connect to the grid at all (" + e.getMessage() + ")");
        System.err.println("Aborting");
        System.exit(0);

    }
    // Launch Tread Job
    JobThread st = new JobThread(Global.ListJob, Global.ListUrl);
    st.start();
    LoggingThread logst = new LoggingThread(Global.ListJob, Global.ListUrl, Global.SEUILDISPLAYLOG);
    logst.start();
    // create Thread Hook intercept kill +CNTL+C
    Thread hook = new Thread() {
        public void run() {
            try {
                for (Jdl job : Global.ListJob) {
                    if (job.getJobId() != null) {
                        JobThread.jobCancel(job.getJobId());
                    }
                }
            } catch (Exception e) {
                System.err.println("Thread Hook:\n" + e.getMessage());
            }
            // give it a change to display final job state
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    Runtime.getRuntime().addShutdownHook(hook);

    //      Integer timer = 180 * 60 * 1000;
    Date now = new Date();

    //      Boolean Fin = false;
    while ((!Global.END) && ((now.getTime() - start.getTime()) < Global.TIMEOUTEND * 60 * 1000)) { // TOEND
        // en
        // minutes
        now = new Date();
        // int mb = 1024*1024;
        // Getting the runtime reference from system
        // Runtime runtime = Runtime.getRuntime();
        // System.out.println("##### Heap utilization statistics [MB]
        // #####");
        // Print used memory
        // System.out.println("Used Memory:"
        // + (runtime.totalMemory() - runtime.freeMemory()) / mb);

        // Print free memory
        // System.out.println("Free Memory:"
        // + runtime.freeMemory() / mb);

        // Print total available memory
        // System.out.println("Total Memory:" + runtime.totalMemory() / mb);

        // Print Maximum available memory
        // System.out.println("Max Memory:" + runtime.maxMemory() / mb);
        // // System.out.println("NB: "+nb_end);
        // if ((float)(runtime.totalMemory() -
        // runtime.freeMemory())/(float)runtime.maxMemory() > (float)0.3){
        // System.out.println ("GC: "+(float)(runtime.totalMemory() -
        // runtime.freeMemory())/runtime.maxMemory());
        // System.gc();
        // };
        sleep(15 * 1000); // in ms

        // System.gc();
        // Fin=true;
        // for (Jdl job : Global.ListJob) {
        // if (job.getJob() != null) {
        // System.out.println("JOB: "+job.getId()+"\t"+job.getStatus());
        // if (job.getStatus().compareTo("END")==0){
        // ((JobImpl) job.getJob()).postStagingAndCleanup();
        // System.out.println("END JOB: "+job.getId());
        // job.setStatus("END");
        // }
        // if (job.getStatus().compareTo("END")!=0){
        // Fin=false;
        // }
        // System.out.println("JOB: "+job.getId()+"\t"+job.getStatus() +
        // "\t"+job.getFail()+"\t"+job.getNodeCe());
        // }
        // }
        // while ((Global.END==0) && ((new
        // Date().getTime()-start.getTime())<timer)){
    }
    // Boolean end_load=false;
    // while (!end_load){
    // end_load=true;
    // for(Jdl job:Global.ListJob){
    // if (job.getStatus().equals("LOAD")){
    // end_load=false;
    // }
    // }
    // }
    System.out.println("END JOB: " + now);
    st.halt();
    logst.halt();
    Iterator<Url> k = Global.ListUrl.iterator();
    while (k.hasNext()) {
        Url url = k.next();
        System.out.println("URL: " + url.getUrl());
    }
    Iterator<Jdl> m = Global.ListJob.iterator();
    while (m.hasNext()) {
        Jdl job = m.next();
        System.out.println(
                "JOB: " + job.getId() + "\t" + job.getFail() + "\t" + job.getStatus() + "\t" + job.getNodeCe());
    }
    System.out.println(start + " " + new Date());
    System.exit(0);
}

From source file:com.mapr.synth.Synth.java

public static void main(String[] args)
        throws IOException, CmdLineException, InterruptedException, ExecutionException {
    final Options opts = new Options();
    CmdLineParser parser = new CmdLineParser(opts);
    try {//from ww  w.  j  a  va  2s .c  o m
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println("Usage: " + "[ -count <number>G|M|K ] " + "-schema schema-file "
                + "[-quote DOUBLE_QUOTE|BACK_SLASH|OPTIMISTIC] " + "[-format JSON|TSV|CSV|XML ] "
                + "[-threads n] " + "[-output output-directory-name] ");
        throw e;
    }
    Preconditions.checkArgument(opts.threads > 0 && opts.threads <= 2000,
            "Must have at least one thread and no more than 2000");

    if (opts.threads > 1) {
        Preconditions.checkArgument(!"-".equals(opts.output),
                "If more than on thread is used, you have to use -output to set the output directory");
    }

    File outputDir = new File(opts.output);
    if (!"-".equals(opts.output)) {
        if (!outputDir.exists()) {
            Preconditions.checkState(outputDir.mkdirs(),
                    String.format("Couldn't create output directory %s", opts.output));
        }
        Preconditions.checkArgument(outputDir.exists() && outputDir.isDirectory(),
                String.format("Couldn't create directory %s", opts.output));
    }

    if (opts.schema == null) {
        throw new IllegalArgumentException("Must specify schema file using [-schema filename] option");
    }
    final SchemaSampler sampler = new SchemaSampler(opts.schema);
    final AtomicLong rowCount = new AtomicLong();

    final List<ReportingWorker> tasks = Lists.newArrayList();
    int limit = (opts.count + opts.threads - 1) / opts.threads;
    int remaining = opts.count;
    for (int i = 0; i < opts.threads; i++) {

        final int count = Math.min(limit, remaining);
        remaining -= count;

        tasks.add(new ReportingWorker(opts, sampler, rowCount, count, i));
    }

    final double t0 = System.nanoTime() * 1e-9;
    ExecutorService pool = Executors.newFixedThreadPool(opts.threads);
    ScheduledExecutorService blinker = Executors.newScheduledThreadPool(1);
    final AtomicBoolean finalRun = new AtomicBoolean(false);

    final PrintStream sideLog = new PrintStream(new FileOutputStream("side-log"));
    Runnable blink = new Runnable() {
        public double oldT;
        private long oldN;

        @Override
        public void run() {
            double t = System.nanoTime() * 1e-9;
            long n = rowCount.get();
            System.err.printf("%s\t%d\t%.1f\t%d\t%.1f\t%.3f\n", finalRun.get() ? "F" : "R", opts.threads,
                    t - t0, n, n / (t - t0), (n - oldN) / (t - oldT));
            for (ReportingWorker task : tasks) {
                ReportingWorker.ThreadReport r = task.report();
                sideLog.printf("\t%d\t%.2f\t%.2f\t%.2f\t%.1f\t%.1f\n", r.fileNumber, r.threadTime, r.userTime,
                        r.wallTime, r.rows / r.threadTime, r.rows / r.wallTime);
            }
            oldN = n;
            oldT = t;
        }
    };
    if (!"-".equals(opts.output)) {
        blinker.scheduleAtFixedRate(blink, 0, 10, TimeUnit.SECONDS);
    }
    List<Future<Integer>> results = pool.invokeAll(tasks);

    int total = 0;
    for (Future<Integer> result : results) {
        total += result.get();
    }
    Preconditions.checkState(total == opts.count, String
            .format("Expected to generate %d lines of output, but actually generated %d", opts.count, total));
    pool.shutdownNow();
    blinker.shutdownNow();
    finalRun.set(true);
    sideLog.close();
    blink.run();
}

From source file:alluxio.cli.MiniBenchmark.java

/**
 * @param args there are no arguments needed
 * @throws Exception if error occurs during tests
 *///  w ww.ja  v a2  s .  c  om
public static void main(String[] args) throws Exception {
    if (!parseInputArgs(args)) {
        usage();
        System.exit(-1);
    }
    if (sHelp) {
        usage();
        System.exit(0);
    }

    CommonUtils.warmUpLoop();

    for (int i = 0; i < sIterations; ++i) {
        final AtomicInteger count = new AtomicInteger(0);
        final CyclicBarrier barrier = new CyclicBarrier(sConcurrency);
        ExecutorService executorService = Executors.newFixedThreadPool(sConcurrency);
        final AtomicLong runtime = new AtomicLong(0);
        for (int j = 0; j < sConcurrency; ++j) {
            switch (sType) {
            case READ:
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            readFile(barrier, runtime, count.addAndGet(1));
                        } catch (Exception e) {
                            LOG.error("Failed to read file.", e);
                            System.exit(-1);
                        }
                    }
                });
                break;
            case WRITE:
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            writeFile(barrier, runtime, count.addAndGet(1));
                        } catch (Exception e) {
                            LOG.error("Failed to write file.", e);
                            System.exit(-1);
                        }
                    }
                });
                break;
            default:
                throw new RuntimeException("Unsupported type.");
            }
        }
        executorService.shutdown();
        Preconditions.checkState(executorService.awaitTermination(1, TimeUnit.HOURS));
        double time = runtime.get() * 1.0 / sConcurrency / Constants.SECOND_NANO;
        System.out.printf("Iteration: %d; Duration: %f seconds; Aggregated throughput: %f GB/second.%n", i,
                time, sConcurrency * 1.0 * sFileSize / time / Constants.GB);
    }
}

From source file:com.continuuity.loom.runtime.DummyProvisioner.java

public static void main(final String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "host", true, "Loom server to connect to");
    options.addOption("p", "port", true, "Loom server port to connect to");
    options.addOption("c", "concurrency", true, "default concurrent threads");
    options.addOption("f", "failurePercent", true, "% of the time a provisioner should fail its task");
    options.addOption("o", "once", false, "whether or not only one task should be taken before exiting");
    options.addOption("d", "taskDuration", true, "number of milliseconds it should take to finish a task");
    options.addOption("s", "sleepMs", true,
            "number of milliseconds a thread will sleep before taking another task");
    options.addOption("n", "numTasks", true,
            "number of tasks to try and take from the queue.  Default is infinite.");
    options.addOption("t", "tenant", true, "tenant id to use.");

    try {//from  w w w.  j a va  2  s .  c  om
        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(options, args);
        host = cmd.hasOption('h') ? cmd.getOptionValue('h') : "localhost";
        port = cmd.hasOption('p') ? Integer.valueOf(cmd.getOptionValue('p')) : 55054;
        concurrency = cmd.hasOption('c') ? Integer.valueOf(cmd.getOptionValue('c')) : 5;
        failurePercent = cmd.hasOption('f') ? Integer.valueOf(cmd.getOptionValue('f')) : 0;
        runOnce = cmd.hasOption('o');
        taskMs = cmd.hasOption('d') ? Long.valueOf(cmd.getOptionValue('d')) : 1000;
        sleepMs = cmd.hasOption('s') ? Long.valueOf(cmd.getOptionValue('s')) : 1000;
        numTasks = cmd.hasOption('n') ? Integer.valueOf(cmd.getOptionValue('n')) : -1;
        tenant = cmd.hasOption('t') ? cmd.getOptionValue('t') : "loom";
    } catch (ParseException e) {
        LOG.error("exception parsing input arguments.", e);
        return;
    }
    if (concurrency < 1) {
        LOG.error("invalid concurrency level {}.", concurrency);
        return;
    }

    if (runOnce) {
        new Provisioner("dummy-0", tenant, host, port, failurePercent, taskMs, sleepMs, 1).runOnce();
    } else {
        LOG.info(String.format(
                "running with %d threads, connecting to %s:%d using tenant %s, with a failure rate of"
                        + "%d percent, task time of %d ms, and sleep time of %d ms between fetches",
                concurrency, host, port, tenant, failurePercent, taskMs, sleepMs));
        pool = Executors.newFixedThreadPool(concurrency);

        try {
            int tasksPerProvisioner = numTasks >= 0 ? numTasks / concurrency : -1;
            int extra = numTasks < 0 ? 0 : numTasks % concurrency;
            pool.execute(new Provisioner("dummy-0", tenant, host, port, failurePercent, taskMs, sleepMs,
                    tasksPerProvisioner + extra));
            for (int i = 1; i < concurrency; i++) {
                pool.execute(new Provisioner("dummy-" + i, tenant, host, port, failurePercent, taskMs, sleepMs,
                        tasksPerProvisioner));
            }
        } catch (Exception e) {
            LOG.error("Caught exception, shutting down now.", e);
            pool.shutdownNow();
        }
        pool.shutdown();
    }
}

From source file:jsonclient.JsonClient.java

public static void main(String args[]) {

    int EPS = 1;/*from  w  w w  .  j a v a2s  . com*/
    List<Place> countries = new ArrayList<Place>();
    List<Thread> threads = new ArrayList<Thread>();
    Country country;
    countries = getCountries();
    //CountrySearcher countrySearcher = null;
    Iterator<Place> itrCountry = countries.iterator();
    ExecutorService exec = Executors.newFixedThreadPool(4);

    while (itrCountry.hasNext()) {
        country = new Country(itrCountry.next());
        CountrySearcher cs = new CountrySearcher(country, EPS);
        threads.add(cs.thread);
        exec.execute(cs);
    }

    exec.shutdown();

    for (Thread th : threads)
        try {
            th.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(JsonClient.class.getName()).log(Level.SEVERE, null, ex);
        }

}