Example usage for java.util.concurrent ExecutorService isTerminated

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

Introduction

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

Prototype

boolean isTerminated();

Source Link

Document

Returns true if all tasks have completed following shut down.

Usage

From source file:cloudworker.RemoteWorker.java

public static void main(String[] args) throws Exception {
    //Command interpreter
    CommandLineInterface cmd = new CommandLineInterface(args);
    final int poolSize = Integer.parseInt(cmd.getOptionValue("s"));
    long idle_time = Long.parseLong(cmd.getOptionValue("i")); //idle time = 60 sec

    init();//from w ww  .j a  va2s .c  o m
    System.out.println("Initialized one remote worker.\n");

    //Create thread pool
    ExecutorService threadPool = Executors.newFixedThreadPool(poolSize);
    BlockingExecutor blockingPool = new BlockingExecutor(threadPool, poolSize);

    //Get queue url
    GetQueueUrlResult urlResult = sqs.getQueueUrl("JobQueue");
    String jobQueueUrl = urlResult.getQueueUrl();

    // Receive messages
    //System.out.println("Receiving messages from JobQueue.\n");

    //...Check idle state
    boolean terminate = false;
    boolean startClock = true;
    long start_time = 0, end_time;

    JSONParser parser = new JSONParser();
    Runtime runtime = Runtime.getRuntime();
    String task_id = null;
    boolean runAnimoto = false;

    while (!terminate || idle_time == 0) {
        while (getQueueSize(sqs, jobQueueUrl) > 0) {

            //Batch retrieving messages
            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest().withQueueUrl(jobQueueUrl)
                    .withMaxNumberOfMessages(10);

            List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

            for (Message message : messages) {
                //System.out.println("  Message");
                //                  System.out.println("    MessageId:     " + message.getMessageId());
                //                  System.out.println("    ReceiptHandle: " + message.getReceiptHandle());
                //                  System.out.println("    MD5OfBody:     " + message.getMD5OfBody());
                //System.out.println("    Body:          " + message.getBody());

                //Get task
                String messageBody = message.getBody();
                JSONObject json = (JSONObject) parser.parse(messageBody);

                task_id = json.get("task_id").toString();
                String task = json.get("task").toString();

                try {
                    //Check duplicate task
                    dynamoDB.addTask(task_id, task);

                    //Execute task, will be blocked if no more thread is currently available 
                    blockingPool.submitTask(new Animoto(task_id, task, sqs));

                    // Delete the message
                    String messageRecieptHandle = message.getReceiptHandle();
                    sqs.deleteMessage(new DeleteMessageRequest(jobQueueUrl, messageRecieptHandle));
                } catch (ConditionalCheckFailedException ccf) {
                    //DO something...
                }

            }

            startClock = true;

        }

        //Start clock to measure idle time
        if (startClock) {
            startClock = false;
            start_time = System.currentTimeMillis();
        } else {
            end_time = System.currentTimeMillis();
            long elapsed_time = (end_time - start_time) / 1000;
            if (elapsed_time > idle_time) {
                terminate = true;
            }
        }
    }

    //System.out.println();

    threadPool.shutdown();
    // Wait until all threads are finished
    while (!threadPool.isTerminated()) {

    }

    //Terminate running instance
    cleanUpInstance();

}

From source file:com.ciphertool.zodiacengine.CipherSolutionExecutor.java

/**
 * @param args/*from ww  w  .j a  v a 2s  . co m*/
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException {
    // Spin up the Spring application context
    setUp();

    CipherDto cipherDto = null;

    Cipher cipher = cipherDao.findByCipherName(cipherName);

    long start = System.currentTimeMillis();

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);

    cipherDto = new CipherDto(String.valueOf(0), cipher);

    /*
     * We want to generate and validate a specific number of solutions, no
     * matter how long it takes.
     */
    if (applicationDurationType == ApplicationDurationType.ITERATION) {
        if (numIterations <= 0) {
            throw new IllegalArgumentException(
                    "ApplicationDurationType set to ITERATION, but numIterations was not set or was set incorrectly.");
        }

        log.info("Beginning solution generation.  Generating " + numIterations + " solutions using "
                + maxThreads + " threads.");

        for (long i = 1; i <= numIterations; i++) {
            Runnable cipherTask = new CipherSolutionSynchronizedRunnable(solutionGenerator, solutionEvaluator,
                    cipherDto);

            executor.execute(cipherTask);
        }

        // Make executor accept no new threads and finish all existing
        // threads in the queue
        executor.shutdown();
    }
    /*
     * We want to generate and validate solutions for a set amount of time,
     * no matter how many we can generate in that time period.
     */
    else if (applicationDurationType == ApplicationDurationType.TEMPORAL) {
        if (applicationRunMillis <= 0) {
            throw new IllegalArgumentException(
                    "ApplicationDurationType set to TEMPORAL, but applicationRunMillis was not set or was set incorrectly.");
        }

        log.info("Beginning solution generation.  Generating solutions for " + applicationRunMillis
                + "ms using " + maxThreads + " threads.");

        long count = 0;

        while (true) {
            Runnable cipherTask = new CipherSolutionSynchronizedRunnable(solutionGenerator, solutionEvaluator,
                    cipherDto);

            executor.execute(cipherTask);

            /*
             * This is a fairly rudimentary way of managing the number of
             * tasks sent to the executor.
             * 
             * If we don't manage it somehow, the app will get bogged down
             * by the continuous while loop and performance will degrade
             * significantly.
             */
            if (++count >= queueTaskLimit) {
                count = 0;

                executor.shutdown();

                /*
                 * We are mainly concerned about blocking until all tasks
                 * are finished, so the timeout is not a big concern.
                 */
                executor.awaitTermination(1, TimeUnit.MINUTES);

                executor = Executors.newFixedThreadPool(maxThreads);

                if ((System.currentTimeMillis() - start) > applicationRunMillis) {
                    break;
                }
            }
        }

        // Make executor stop immediately
        executor.shutdownNow();
    }

    // Wait until all threads are finished
    while (!executor.isTerminated()) {
    }

    SolutionChromosome solutionMostMatches = cipherDto.getSolutionMostMatches();
    SolutionChromosome solutionMostUnique = cipherDto.getSolutionMostUnique();
    SolutionChromosome solutionMostAdjacent = cipherDto.getSolutionMostAdjacent();

    /*
     * Print out summary information
     */
    log.info("Took " + (System.currentTimeMillis() - start) + "ms to generate and validate "
            + cipherDto.getNumSolutions() + " solutions.");
    log.info("Highest total matches achieved: " + solutionMostMatches.getTotalMatches());
    log.info("Average total matches: " + (cipherDto.getTotalMatchSum() / cipherDto.getNumSolutions()));
    log.info("Best solution found: " + solutionMostMatches);
    log.info("Most unique matches achieved: " + solutionMostUnique.getUniqueMatches());
    log.info("Average unique matches: " + (cipherDto.getUniqueMatchSum() / cipherDto.getNumSolutions()));
    log.info("Solution with most unique matches found: " + solutionMostUnique);
    log.info("Most adjacent matches achieved: " + solutionMostAdjacent.getAdjacentMatchCount());
    log.info("Average adjacent matches: " + (cipherDto.getAdjacentMatchSum() / cipherDto.getNumSolutions()));
    log.info("Solution with most adjacent matches found: " + solutionMostAdjacent);
}

From source file:Main.java

public static void printStatus(Object o) {
    if (o instanceof Thread) {
        Thread t = (Thread) o;
        System.out.println("Thread " + t.getName() + " " + t.getState());
    } else if (o instanceof ExecutorService) {
        ExecutorService e = (ExecutorService) o;
        System.out.println("Executor " + e.isShutdown() + " " + e.isTerminated());
    }//from www .  j a v a2s. c  o  m
}

From source file:Main.java

public static void stop(ExecutorService executor) {
    try {/*  w  w  w .  ja v a  2  s . c  o  m*/
        executor.shutdown();
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        System.err.println("termination interrupted");
    } finally {
        if (!executor.isTerminated()) {
            System.err.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}

From source file:Main.java

public static void stop(ExecutorService executor) {
    try {/* w w  w  .  j av  a  2s.  co m*/
        executor.shutdown();
        executor.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        System.err.println("termination interrupted");
    } finally {
        if (!executor.isTerminated()) {
            System.err.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}

From source file:Main.java

public static void stop(ExecutorService executor) {
    try {/*from  w  w w .j ava  2 s  .  c  o  m*/
        executor.shutdown();
        executor.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        System.err.println("termination interrupted");
    } finally {
        if (!executor.isTerminated()) {
            System.out.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}

From source file:gr.demokritos.iit.cru.creativity.reasoning.semantic.WebMiner.java

public static String WebMiner(String seed, int difficulty, String language, boolean compactForm)
        throws ClassNotFoundException, SQLException, IOException, InstantiationException,
        IllegalAccessException {//w  w w . ja  va2  s .c  o  m
    Gson gson = new Gson();
    Connect c = new Connect(language);
    RandomWordGenerator r = new RandomWordGenerator(c);
    String randomPhrase = r.selectRandomWord(seed, difficulty).replace(",", " ");
    InfoSummarization inf = new InfoSummarization(c);
    LinkedHashMap<String, Double> TagCloud = new LinkedHashMap<String, Double>();

    Set<String> pages = new HashSet<String>();
    ArrayList<String> urls = new ArrayList<String>();
    ArrayList<String> urls_temp = new ArrayList<String>();
    if (language.equalsIgnoreCase("en")) {
        if (randomPhrase.length() == 0) {
            randomPhrase = seed;
        }
        String bingAppId = c.getBingAppId();
        BingCrawler bc = new BingCrawler(bingAppId, language);
        urls_temp = bc.crawl(randomPhrase);
        int url_loop = 0;
        while ((url_loop < 5) && (url_loop < urls_temp.size())) {
            urls.add(urls_temp.get(url_loop));
            url_loop++;
        }
    } else if (language.equalsIgnoreCase("el")) {
        String bingAppId = c.getBingAppId();
        BingCrawler bc = new BingCrawler(bingAppId, language);
        urls_temp = bc.crawl(randomPhrase);
        int url_loop = 0;
        while ((url_loop < 5) && (url_loop < urls_temp.size())) {
            urls.add(urls_temp.get(url_loop));
            url_loop++;
        }
    } else if (language.equalsIgnoreCase("de")) {//keep only the first word of the random phrase for search
        if (randomPhrase.length() == 0) {
            randomPhrase = seed;
        }
        urls_temp = HTMLUtilities.linkExtractor(
                "http://www.fragfinn.de/kinderliste/suche?start=0&query=" + randomPhrase.split(" ")[0], "UTF-8",
                0);

        for (String url : urls_temp) {
            urls.add(StringEscapeUtils.unescapeHtml4(url));
            if (urls.size() == 5) {
                break;
            }
        }
    }
    String delims = "[{} .,;?!():\"]+";

    String[] words = randomPhrase.split(",");
    String[] user_keywords = seed.split(delims);
    if (urls.size() > 0) {
        ExecutorService threadPool = Executors.newFixedThreadPool(urls.size());
        for (String url : urls) {
            threadPool.submit(new HTMLPages(url, pages, language)); //stopWordSet, tokensHashMap,language));
            // threadPool.submit(HTMLTokenizer());
        }
        threadPool.shutdown();
        while (!threadPool.isTerminated()) {

        }

        LinkedHashMap<ArrayList<String>, Double> temp = inf.TopTermsBing(pages, compactForm);
        HashMap<String, Double> temp2 = new HashMap<String, Double>();
        for (ArrayList<String> stems : temp.keySet()) {
            for (int j = 0; j < stems.size(); j++) {
                String s = stems.get(j).split("\\{")[0];
                s = s.replace(",", " ");
                s = s.trim();

                boolean wordnet = true;
                //if term is not one of the initial random phrase
                for (int i = 0; i < words.length; i++) {
                    if (s.equalsIgnoreCase(words[i])) {
                        wordnet = false;
                    }
                }
                //and if it 's not in the initial words of user
                for (int i = 0; i < user_keywords.length; i++) {
                    if (s.equalsIgnoreCase(user_keywords[i])) {
                        wordnet = false;
                    }
                }
                //in german or greek, ignore english words from search english words
                if (language.equalsIgnoreCase("de") || language.equalsIgnoreCase("el")) {
                    if (c.getWn().getCommonPos(s) != null) {
                        continue;
                    }
                }
                //return it with its stem's weight
                if (wordnet) {
                    //for every stem, put each of its corresponding terms to tagCloud with the stem's tf
                    temp2.put(stems.get(j), temp.get(stems));
                }
            }
        }
        TagCloud = inf.sortHashMapByValues(temp2);
        threadPool.shutdownNow();
    }
    String json = gson.toJson(TagCloud);
    c.CloseConnection();
    return json;
}

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

/**
 * /*w w  w. j  a  va2  s .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:agileinterop.AgileInterop.java

private static JSONObject getPSRData(String body) throws ParseException, InterruptedException, APIException {
    // Parse body as JSON
    JSONParser parser = new JSONParser();
    JSONArray jsonBody = (JSONArray) parser.parse(body);

    Map<String, Object> data = new HashMap<>();

    class GetObjectData implements Runnable {
        private String psrNumber;
        private Map<String, Object> data;
        private IServiceRequest psr;

        public GetObjectData(String psrNumber, Map<String, Object> data)
                throws APIException, InterruptedException {
            this.psrNumber = psrNumber;
            this.data = data;

            psr = (IServiceRequest) Agile.session.getObject(IServiceRequest.OBJECT_TYPE, psrNumber);
        }/* w ww .j  a v  a 2 s . co m*/

        @Override
        public void run() {
            this.data.put(psrNumber, new HashMap<String, Object>());

            try {
                if (psr != null) {
                    getCellValues();
                    getAttachments();
                    getHistory();
                }
            } catch (APIException ex) {
                Logger.getLogger(AgileInterop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void getCellValues() throws APIException {
            Map<String, Object> cellValues = new HashMap<>();

            long startTime = System.currentTimeMillis();

            // Get cell values
            ICell[] cells = psr.getCells();
            for (ICell cell : cells) {
                if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                    if (cell.getValue() != null) {
                        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                        sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                        cellValues.put(cell.getName(), sdf.format((Date) cell.getValue()));
                    } else {
                        cellValues.put(cell.getName(), cell.toString());
                    }
                } else {
                    cellValues.put(cell.getName(), cell.toString());
                }
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getCellValues executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("cellValues", cellValues);
        }

        private void getAttachments() throws APIException {
            List<Map<String, String>> attachments = new ArrayList<>();

            long startTime = System.currentTimeMillis();

            // Get attachments information
            ITable table = psr.getTable("Attachments");
            ITwoWayIterator tableIterator = table.getTableIterator();
            while (tableIterator.hasNext()) {
                IRow row = (IRow) tableIterator.next();
                Map<String, String> attachment = new HashMap<>();

                ICell[] cells = row.getCells();
                for (ICell cell : cells) {
                    if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                        if (cell.getValue() != null) {
                            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                            sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                            attachment.put(cell.getName(), sdf.format((Date) cell.getValue()));
                        } else {
                            attachment.put(cell.getName(), cell.toString());
                        }
                    } else {
                        attachment.put(cell.getName(), cell.toString());
                    }
                }

                attachments.add(attachment);
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getAttachments executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("attachments", attachments);
        }

        private void getHistory() throws APIException {
            List<Map<String, String>> histories = new ArrayList<>();

            long startTime = System.currentTimeMillis();

            // Get history information
            ITable table = psr.getTable("History");
            ITwoWayIterator tableIterator = table.getTableIterator();
            while (tableIterator.hasNext()) {
                IRow row = (IRow) tableIterator.next();
                Map<String, String> history = new HashMap<>();

                ICell[] cells = row.getCells();
                for (ICell cell : cells) {
                    if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                        if (cell.getValue() != null) {
                            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                            sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                            history.put(cell.getName(), sdf.format((Date) cell.getValue()));
                        } else {
                            history.put(cell.getName(), cell.toString());
                        }
                    } else {
                        history.put(cell.getName(), cell.toString());
                    }
                }

                histories.add(history);
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getHistory executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("history", histories);
        }
    }

    synchronized (data) {
        // Do something funky with the first one
        Thread t = new Thread(new GetObjectData(jsonBody.get(0).toString(), data));
        t.start();
        t.join();

        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (Object object : jsonBody.subList(1, jsonBody.size() - 1)) {
            executor.execute(new Thread(new GetObjectData(object.toString(), data)));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
        }
    }

    JSONObject obj = new JSONObject();
    obj.put("data", data);
    return obj;
}

From source file:agileinterop.AgileInterop.java

private static JSONObject getPSRList(String body)
        throws ParseException, InterruptedException, java.text.ParseException, Exception {
    // Parse body as JSON
    JSONParser parser = new JSONParser();
    JSONObject jsonBody = (JSONObject) parser.parse(body);

    Date startDateOriginated = new SimpleDateFormat("MM/dd/yyyy")
            .parse(jsonBody.get("startDateOriginated").toString());
    Date endDateOriginated = new SimpleDateFormat("MM/dd/yyyy")
            .parse(jsonBody.get("endDateOriginated").toString());

    List<String> data = new ArrayList<>();

    class getPSRListForDate implements Runnable {
        private Date dateOriginated;
        private List<String> data;

        public getPSRListForDate(Date dateOriginated, List<String> data) {
            this.dateOriginated = dateOriginated;
            this.data = data;
        }//from   ww w.j  ava 2 s  .  c om

        public getPSRListForDate(List<String> data) {
            this.data = data;
        }

        @Override
        public void run() {
            try {
                String dateOriginatedString = new SimpleDateFormat("MM/dd/yyyy").format(dateOriginated);

                Long startTime = System.currentTimeMillis();

                IQuery query = (IQuery) Agile.session.createObject(IQuery.OBJECT_TYPE,
                        "Product Service Requests");

                String criteria = "[Cover Page.Date Originated] == '" + dateOriginatedString
                        + " 12:00:00 AM GMT' AND "
                        + "[Cover Page.Type] IN ('Customer Complaint', 'Customer Inquiry', 'Partner Complaint', "
                        + "'Reportable Malfunction / Adverse Event', 'Ancillary Devices & Applications', 'Distributors / Partners', "
                        + "'MDR Decision Tree', 'Investigation Report - No RGA', 'Investigation Report - RGA')";
                query.setCriteria(criteria);

                query.setResultAttributes(new Integer[] { 4856 }); // 4856 = Object ID of [Cover Page.PSR Number]

                ITable queryResults = query.execute();
                queryResults.setPageSize(5000);

                ITwoWayIterator tableIterator = queryResults.getTableIterator();
                while (tableIterator.hasNext()) {
                    IRow row = (IRow) tableIterator.next();
                    data.add(row.getCell(4856).toString()); // 4856 = Object ID of [Cover Page.PSR Number]
                }

                Long endTime = System.currentTimeMillis();

                String logMessage = String.format("getPSRList: Query for %s executed in %d milliseconds",
                        new SimpleDateFormat("yyyy-MM-dd").format(dateOriginated), endTime - startTime);
                System.out.println(logMessage);
            } catch (APIException ex) {
                Logger.getLogger(AgileInterop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    if (startDateOriginated.after(endDateOriginated)) {
        throw new Exception("startDateOriginated is after endDateOriginated. This makes no sense, silly.");
    }

    synchronized (data) {
        Date currentDateOriginated = startDateOriginated;

        ExecutorService executor = Executors.newFixedThreadPool(6);
        do {
            executor.execute(new Thread(new getPSRListForDate(currentDateOriginated, data)));

            Calendar c = Calendar.getInstance();
            c.setTime(currentDateOriginated);
            c.add(Calendar.DATE, 1);

            currentDateOriginated = c.getTime();
        } while (currentDateOriginated.before(endDateOriginated)
                | currentDateOriginated.equals(endDateOriginated));

        executor.shutdown();
        while (!executor.isTerminated()) {
        }
    }

    JSONObject obj = new JSONObject();
    obj.put("data", data);
    return obj;
}