Example usage for java.util.concurrent ExecutorService shutdown

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

Introduction

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

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

From source file:com.clonephpscrapper.crawler.ClonePhpScrapper.java

public void crawledCategories() throws URISyntaxException, IOException, InterruptedException, Exception {

    String url = "http://clonephp.com/";

    //       Document doc = Jsoup.parse(fetchPage(new URI(url)));
    String response = "";
    response = new GetRequestHandler().doGetRequest(new URL(url));

    Document doc = Jsoup.parse(response);

    Elements ele = doc.select("table[class=dir] tbody tr td table[class=dir_cat] tbody tr th a");//.first();

    for (Element ele1 : ele) {
        objCategories = new Categories();

        String categoryName = ele1.text();
        String categoryUrl = "http://clonephp.com/" + ele1.attr("href");

        System.out.println("CATEGORY_NAME : " + categoryName);
        System.out.println("CATEGORY_URL  : " + categoryUrl);

        objCategories.setCategoryName(categoryName);
        objCategories.setCategoryUrl(categoryUrl);

        objClonePhpDaoImpl.insertCategoriesData(objCategories);

        //            objCrawlingEachUrlData.crawlingUrlData(categoryUrl);
    }/*from ww w .  ja  va2 s .  com*/

    List<Future<String>> list = new ArrayList<Future<String>>();
    ExecutorService executor = Executors.newFixedThreadPool(5);

    List<Categories> listCatogories = objClonePhpDaoImpl.getCategoriesDataList();

    for (Categories listCatogory : listCatogories) {

        try {
            Callable worker = new CrawlingEachUrlData(listCatogory, objClonePhpDaoImpl);
            Future<String> future = executor.submit(worker);
            list.add(future);
        } catch (Exception exx) {
            System.out.println(exx);
        }

    }

    for (Future<String> fut : list) {
        try {
            //print the return value of Future, notice the output delay in console
            // because Future.get() waits for task to get completed
            System.out.println(new Date() + "::" + fut.get());
        } catch (InterruptedException | ExecutionException ep) {
            ep.printStackTrace();
        }
    }
    //shut down the executor service now
    executor.shutdown();

}

From source file:byps.http.HWireClient.java

@Override
public void done() {

    internalCancelAllRequests(MESSAGEID_DISCONNECT);

    isDone = true;//ww  w.  jav  a  2  s  .  c o  m

    if (isMyThreadPool) {
        if (threadPool instanceof ExecutorService) {
            ExecutorService tp = (ExecutorService) threadPool;
            tp.shutdown();
        }
    }
}

From source file:com.emc.ecs.sync.storage.CasStorageTest.java

private void delete(FPPool pool, List<String> clipIds) throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS);

    System.out.print("Deleting clips");

    for (String clipId : clipIds) {
        service.submit(new ClipDeleter(pool, clipId));
    }/*  w ww  .j a  v a 2  s  . c  o m*/

    service.shutdown();
    service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES);
    service.shutdownNow();

    System.out.println();
}

From source file:be.panako.cli.Store.java

@Override
public void run(final String... args) {
    int processors = availableProcessors();
    int counter = 0;
    final ExecutorService executor = Executors.newFixedThreadPool(processors);
    final List<File> files = this.getFilesFromArguments(args);
    boolean extractMetaData = hasArgument("-m", args) || hasArgument("--meta-data", args);
    if (files.size() > 1) {
        String msg = "Processing " + files.size() + " files on " + processors + " seperate threads.";
        System.out.println(msg);//from  w w w  . java  2  s. c o  m
        LOG.info("Store task started. " + msg);
    }
    System.out.println("Audiofile;Audio duration;Fingerprinting duration;ratio");
    for (File file : files) {
        counter++;
        //executor.submit(new StoreTask(file, counter, files.size()));
        StoreTask task = new StoreTask(file, counter, files.size(), extractMetaData);
        task.run();
    }

    try {
        //do not accept more tasks.
        executor.shutdown();
        //wait for tasks to finish
        executor.awaitTermination(300, java.util.concurrent.TimeUnit.DAYS);
        //System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*catch (InterruptedException e1) {
       //Thread was interrupted
       e1.printStackTrace();
       LOG.severe("Did not finish all tasks, thread was interrupted!");
    }*/
}

From source file:com.palantir.paxos.PaxosConsensusFastTest.java

@Test
public void loseQuorumDiffToken() throws InterruptedException {
    for (int i = QUORUM_SIZE; i < NUM_POTENTIAL_LEADERS; i++) {
        state.goDown(i);// w ww  . j  a  va 2 s  .  co  m
    }
    LeadershipToken t = state.gainLeadership(0);
    state.goDown(QUORUM_SIZE - 1);
    ExecutorService exec = PTExecutors.newSingleThreadExecutor();
    Future<Void> f = exec.submit(new Callable<Void>() {
        @Override
        public Void call() {
            int i = QUORUM_SIZE - 1;
            while (!Thread.currentThread().isInterrupted()) {
                int next = i + 1;
                if (next == NUM_POTENTIAL_LEADERS) {
                    next = QUORUM_SIZE - 1;
                }
                state.goDown(next);
                state.comeUp(i);
                i = next;
            }
            return null;
        }
    });
    // Don't check leadership immediately after gaining it, since quorum might get lost.
    LeadershipToken token2 = state.gainLeadershipWithoutCheckingAfter(0);
    assertTrue("leader can confirm leadership with quorum", t.sameAs(token2));
    f.cancel(true);
    exec.shutdown();
    exec.awaitTermination(10, TimeUnit.SECONDS);
    for (int i = 0; i < NUM_POTENTIAL_LEADERS; i++) {
        state.comeUp(i);
    }
}

From source file:com.emc.ecs.sync.storage.CasStorageTest.java

private String summarize(FPPool pool, List<String> clipIds) throws Exception {
    List<String> summaries = Collections.synchronizedList(new ArrayList<String>());

    ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS);

    System.out.print("Summarizing clips");

    for (String clipId : clipIds) {
        service.submit(new ClipReader(pool, clipId, summaries));
    }/* www.  j ava2s. c o m*/

    service.shutdown();
    service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES);
    service.shutdownNow();

    System.out.println();

    Collections.sort(summaries);
    StringBuilder out = new StringBuilder();
    for (String summary : summaries) {
        out.append(summary);
    }
    return out.toString();
}

From source file:com.relativitas.maven.plugins.formatter.FormatterMojo.java

private void formatAll(final ResultCollector rc, final Properties hashCache) throws MojoExecutionException {
    final ExecutorService service;
    if (isCleanBuild) {
        //This seems to be the best ratio time gained/threads
        //More threads only reduce a little more.
        service = Executors.newFixedThreadPool(2);
    } else {/* w w  w .j a v  a  2  s.c  o m*/
        service = Executors.newFixedThreadPool(1);
    }

    formatSourceDirectory(service, rc, hashCache, sourceDirectory);
    formatSourceDirectory(service, rc, hashCache, testSourceDirectory);

    service.shutdown();
    try {
        //If we wait 10 minutes,something is really wrong or you have Hundreds of thousands of files.This is bad!
        service.awaitTermination(10, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
    }
}

From source file:com.vmware.bdd.manager.SoftwareManagerCollector.java

private void updateVersion(AppManagerRead appManagerRead) {
    String softMgrVersion = "UNKNOWN";

    final SoftwareManager softwareManager = this.getSoftwareManager(appManagerRead.getName());
    // fork a child thread to do the actual connecting action
    // this is to avoid the time out issue for the socket connection when the target host is shutdown
    ExecutorService exec = Executors.newFixedThreadPool(1);
    Future<String> futureResult = exec.submit(new Callable<String>() {
        @Override//  w  ww.j a va  2  s.  com
        public String call() throws Exception {
            // TODO Auto-generated method stub
            return softwareManager.getVersion();
        }
    });

    String result = (String) CommonUtil.waitForThreadResult(futureResult, waitTimeForAppMgrConn);
    if (null != result) {
        softMgrVersion = result;
    }
    exec.shutdown();

    appManagerRead.setVersion(softMgrVersion);
}

From source file:com.hygenics.parser.Upload.java

private void doScp() {
    ArrayList<String> files = new ArrayList<String>();
    File fp = new File(localPath);

    if (fp.isDirectory()) {
        for (String f : fp.list()) {
            files.add(localPath + f);/*from  www.j a v a 2s.c  o m*/
        }
    } else {
        files.add(localPath);
    }

    int p = 0;
    int partsize = files.size() / MAXFILESPERTHREAD;
    int offset = 0;

    if (partsize < 100) {
        partsize = files.size();
    }

    ExecutorService exec = Executors.newFixedThreadPool(this.numthreads);

    do {
        List<String> subset = files.subList(offset, offset + partsize);
        exec.execute(new SCP(subset, this.remotePath));

        p++;
        if (p == numthreads) {
            try {
                exec.awaitTermination(timeout, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            p = 0;
        }
        offset += partsize;
    } while (offset < files.size());

    if (p > 0) {
        try {
            exec.awaitTermination(timeout, TimeUnit.MILLISECONDS);
            exec.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.amazonaws.services.kinesis.aggregators.datastore.DynamoQueryEngine.java

public List<TableKeyStructure> parallelQueryKeys(QueryKeyScope scope, int threads) throws Exception {
    List<ParallelKeyScanWorker> workers = new ArrayList<>();
    Collection<Future<?>> workerStatus = new ArrayList<>();
    List<TableKeyStructure> output = new ArrayList<>();
    int totalResultsProcessed = 0;

    // set up the executor thread pool
    ExecutorService executor = Executors.newFixedThreadPool(threads);

    // create workers for each segment that we need to do queries against
    for (int i = 0; i < threads; i++) {
        ParallelKeyScanWorker worker = new ParallelKeyScanWorker(this.tableName, i, threads, scope,
                this.labelAttribute, this.dateAttribute);
        workers.add(worker);//www  .j a  va2 s.c  om
        workerStatus.add(executor.submit(worker));
    }

    for (Future<?> f : workerStatus) {
        f.get();
    }
    executor.shutdown();

    for (ParallelKeyScanWorker w : workers) {
        // throw any exceptions the worker incurred
        w.throwExceptions();

        if (w.getResultCount() > 0) {
            output.addAll(w.getOutput());
        }

        totalResultsProcessed += w.getResultsProcessed();
    }

    LOG.info(String.format("Key Extraction Complete - Processed %s Key Items", totalResultsProcessed));

    return output;
}