Example usage for java.util.concurrent ExecutorService submit

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

Introduction

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

Prototype

Future<?> submit(Runnable task);

Source Link

Document

Submits a Runnable task for execution and returns a Future representing that task.

Usage

From source file:ThreadPoolTest.java

public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): ");
    String directory = in.nextLine();
    System.out.print("Enter keyword (e.g. volatile): ");
    String keyword = in.nextLine();

    ExecutorService pool = Executors.newCachedThreadPool();

    MatchCounter counter = new MatchCounter(new File(directory), keyword, pool);
    Future<Integer> result = pool.submit(counter);

    try {/*from  w w w .  j  a v a2 s . com*/
        System.out.println(result.get() + " matching files.");
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }
    pool.shutdown();

    int largestPoolSize = ((ThreadPoolExecutor) pool).getLargestPoolSize();
    System.out.println("largest pool size=" + largestPoolSize);
}

From source file:at.tfr.securefs.client.SecurefsClient.java

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

    SecurefsClient client = new SecurefsClient();
    try {/*  ww  w .j  a v  a2 s . com*/
        client.parse(args);

        if (client.asyncTest) {
            ExecutorService executor = Executors.newFixedThreadPool(client.threads);
            for (int i = 0; i < client.threads; i++) {
                executor.submit(client);
            }
            executor.shutdown();
            executor.awaitTermination(10, TimeUnit.MINUTES);
        } else {
            client.run();
        }

    } catch (Throwable e) {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp(SecurefsClient.class.getSimpleName(), client.options);
        e.printStackTrace();
    }
}

From source file:com.linkedin.pinotdruidbenchmark.DruidThroughput.java

@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    if (args.length != 3 && args.length != 4) {
        System.err.println(//from w  w w . j a  v a2s .  c  om
                "3 or 4 arguments required: QUERY_DIR, RESOURCE_URL, NUM_CLIENTS, TEST_TIME (seconds).");
        return;
    }

    File queryDir = new File(args[0]);
    String resourceUrl = args[1];
    final int numClients = Integer.parseInt(args[2]);
    final long endTime;
    if (args.length == 3) {
        endTime = Long.MAX_VALUE;
    } else {
        endTime = System.currentTimeMillis() + Integer.parseInt(args[3]) * MILLIS_PER_SECOND;
    }

    File[] queryFiles = queryDir.listFiles();
    assert queryFiles != null;
    Arrays.sort(queryFiles);

    final int numQueries = queryFiles.length;
    final HttpPost[] httpPosts = new HttpPost[numQueries];
    for (int i = 0; i < numQueries; i++) {
        HttpPost httpPost = new HttpPost(resourceUrl);
        httpPost.addHeader("content-type", "application/json");
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(queryFiles[i]))) {
            int length;
            while ((length = bufferedReader.read(CHAR_BUFFER)) > 0) {
                stringBuilder.append(new String(CHAR_BUFFER, 0, length));
            }
        }
        String query = stringBuilder.toString();
        httpPost.setEntity(new StringEntity(query));
        httpPosts[i] = httpPost;
    }

    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicLong totalResponseTime = new AtomicLong(0L);
    final ExecutorService executorService = Executors.newFixedThreadPool(numClients);

    for (int i = 0; i < numClients; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                    while (System.currentTimeMillis() < endTime) {
                        long startTime = System.currentTimeMillis();
                        CloseableHttpResponse httpResponse = httpClient
                                .execute(httpPosts[RANDOM.nextInt(numQueries)]);
                        httpResponse.close();
                        long responseTime = System.currentTimeMillis() - startTime;
                        counter.getAndIncrement();
                        totalResponseTime.getAndAdd(responseTime);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executorService.shutdown();

    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis() < endTime) {
        Thread.sleep(REPORT_INTERVAL_MILLIS);
        double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
        int count = counter.get();
        double avgResponseTime = ((double) totalResponseTime.get()) / count;
        System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: "
                + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
    }
}

From source file:at.tfr.securefs.client.SecurefsFileServiceClient.java

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

    SecurefsFileServiceClient client = new SecurefsFileServiceClient();
    try {// w ww  .j a  v  a2 s . com
        client.parse(args);

        if (client.asyncTest) {
            ExecutorService executor = Executors.newFixedThreadPool(client.threads);
            for (int i = 0; i < client.threads; i++) {
                executor.submit(client);
            }
            executor.shutdown();
            executor.awaitTermination(10, TimeUnit.MINUTES);
        } else {
            client.run();
        }

    } catch (Throwable e) {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp(SecurefsFileServiceClient.class.getSimpleName(), client.options);
        e.printStackTrace();
    }
}

From source file:com.amazonaws.services.kinesis.multilang.MultiLangDaemon.java

/**
 * @param args Accepts a single argument, that argument is a properties file which provides KCL configuration as
 *        well as the name of an executable.
 */// w w  w .j  av a 2  s  . c  om
public static void main(String[] args) {

    if (args.length == 0) {
        printUsage(System.err, "You must provide a properties file");
        System.exit(1);
    }
    MultiLangDaemonConfig config = null;
    try {
        config = new MultiLangDaemonConfig(args[0]);
    } catch (IOException e) {
        printUsage(System.err, "You must provide a properties file");
        System.exit(1);
    } catch (IllegalArgumentException e) {
        printUsage(System.err, e.getMessage());
        System.exit(1);
    }

    ExecutorService executorService = config.getExecutorService();

    // Daemon
    MultiLangDaemon daemon = new MultiLangDaemon(config.getKinesisClientLibConfiguration(),
            config.getRecordProcessorFactory(), executorService);

    Future<Integer> future = executorService.submit(daemon);
    try {
        System.exit(future.get());
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Encountered an error while running daemon", e);
    }
    System.exit(1);
}

From source file:com.wouterbreukink.onedrive.Main.java

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

    // Parse command line args
    try {//from  www .  j  ava  2s . c  o  m
        CommandLineOpts.initialise(args);
    } catch (ParseException ex) {
        log.error("Unable to parse command line arguments - " + ex.getMessage());
        CommandLineOpts.printHelp();
        return;
    }

    if (getCommandLineOpts().help()) {
        CommandLineOpts.printHelp();
        return;
    }

    if (getCommandLineOpts().version()) {
        String version = getCommandLineOpts().getClass().getPackage().getImplementationVersion();
        log.info("onedrive-java-client version " + (version != null ? version : "DEVELOPMENT"));
        return;
    }

    // Initialise a log file (if set)
    if (getCommandLineOpts().getLogFile() != null) {
        String logFileName = LogUtils.addFileLogger(getCommandLineOpts().getLogFile());
        log.info(String.format("Writing log output to %s", logFileName));
    }

    if (getCommandLineOpts().isAuthorise()) {
        AuthorisationProvider.FACTORY.printAuthInstructions();
        return;
    }

    if (getCommandLineOpts().getLocalPath() == null || getCommandLineOpts().getRemotePath() == null
            || getCommandLineOpts().getDirection() == null) {
        log.error("Must specify --local, --remote and --direction");
        CommandLineOpts.printHelp();
        return;
    }

    // Initialise the OneDrive authorisation
    AuthorisationProvider authoriser;
    try {
        authoriser = AuthorisationProvider.FACTORY.create(getCommandLineOpts().getKeyFile());
        authoriser.getAccessToken();
    } catch (OneDriveAPIException ex) {
        log.error("Unable to authorise client: " + ex.getMessage());
        log.error("Re-run the application with --authorise");
        return;
    }

    // Initialise the providers
    OneDriveProvider api;
    FileSystemProvider fileSystem;
    if (getCommandLineOpts().isDryRun()) {
        log.warn("This is a dry run - no changes will be made");
        api = OneDriveProvider.FACTORY.readOnlyApi(authoriser);
        fileSystem = FileSystemProvider.FACTORY.readOnlyProvider();
    } else {
        api = OneDriveProvider.FACTORY.readWriteApi(authoriser);
        fileSystem = FileSystemProvider.FACTORY.readWriteProvider();
    }

    // Report on progress
    TaskReporter reporter = new TaskReporter();

    // Get the primary drive
    Drive primary = api.getDefaultDrive();

    // Report quotas
    log.info(String.format("Using drive with id '%s' (%s). Usage %s of %s (%.2f%%)", primary.getId(),
            primary.getDriveType(), readableFileSize(primary.getQuota().getUsed()),
            readableFileSize(primary.getQuota().getTotal()),
            ((double) primary.getQuota().getUsed() / primary.getQuota().getTotal()) * 100));

    // Check the given root folder
    OneDriveItem rootFolder = api.getPath(getCommandLineOpts().getRemotePath());

    if (!rootFolder.isDirectory()) {
        log.error(String.format("Specified root '%s' is not a folder", rootFolder.getFullName()));
        return;
    }

    File localRoot = new File(getCommandLineOpts().getLocalPath());

    log.info(String.format("Local folder '%s'", localRoot.getAbsolutePath()));
    log.info(String.format("Remote folder '<onedrive>%s'", rootFolder.getFullName()));

    // Start synchronisation operation at the root
    final TaskQueue queue = new TaskQueue();
    queue.add(new CheckTask(new Task.TaskOptions(queue, api, fileSystem, reporter), rootFolder, localRoot));

    // Get a bunch of threads going
    ExecutorService executorService = Executors.newFixedThreadPool(getCommandLineOpts().getThreads());

    for (int i = 0; i < getCommandLineOpts().getThreads(); i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    //noinspection InfiniteLoopStatement
                    while (true) {
                        Task taskToRun = null;
                        try {
                            taskToRun = queue.take();
                            taskToRun.run();
                        } finally {
                            if (taskToRun != null) {
                                queue.done(taskToRun);
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
    }

    queue.waitForCompletion();
    log.info("Synchronisation complete");
    reporter.report();
}

From source file:com.amazonaws.services.dynamodbv2.streamsadapter.StreamsMultiLangDaemon.java

/**
 * @param args Accepts a single argument, that argument is a properties file which provides KCL configuration as
 *             well as the name of an executable.
 *//*from   w w  w . j  av a 2  s . co  m*/
public static void main(String[] args) {

    if (args.length == 0) {
        MultiLangDaemon.printUsage(System.err, "You must provide a properties file");
        System.exit(1);
    }
    MultiLangDaemonConfig config = null;
    try {
        config = new MultiLangDaemonConfig(args[0]);
    } catch (IOException | IllegalArgumentException e) {
        MultiLangDaemon.printUsage(System.err, "You must provide a valid properties file");
        System.exit(1);
    }

    final ExecutorService executorService = config.getExecutorService();
    final Worker worker = StreamsWorkerFactory.createDynamoDbStreamsWorker(config.getRecordProcessorFactory(),
            config.getKinesisClientLibConfiguration(), executorService);

    // Daemon
    final MultiLangDaemon daemon = new MultiLangDaemon(worker);

    final Future<Integer> future = executorService.submit(daemon);
    try {
        System.exit(future.get());
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Encountered an error while running daemon", e);
    }
    System.exit(1);
}

From source file:com.taveloper.http.test.PwTest.java

/**
 * @param args the command line arguments
 *///from   w w w.j a  v a2 s.  c  o m
public static void main(String[] args) throws IOException, InterruptedException {
    ExecutorService es = Executors.newFixedThreadPool(1);
    // ?     
    try {
        for (int i = 0; i < a.length; i++) {
            String s1 = a[i];
            for (int j = 0; j < a.length; j++) {
                String s2 = a[j];
                Pw pw = new Pw(s1 + s2);
                es.submit(pw);
            }
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    if (es.isShutdown()) {
    } else {
        Thread.sleep(1000);
    }
}

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 www. ja  v a  2s  .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/*from w  w w . jav 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();

                    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();
}