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:configurator.Configurator.java

/**
 * The main method for telnet configuring the network devices. Prepare data
 * and create thread for each device/*from   w w w  .  ja va 2  s.  c o  m*/
 *
 * @param ipText - List of ip ranges, subnets and single ip
 * @param authData - authorization data for work on devices
 * @param models list of models for telnet configure
 */
void telnetWork(String ipText, AuthenticationData authData, List<String> models) {
    try {
        List<String> ipList = parseIp(ipText);
        ExecutorService exec = Executors.newCachedThreadPool();
        ipList.forEach((ip) -> {
            Snmp snmp = new Snmp(authData.getCommunity());
            String modelOid = mainProperites.getProperty("Identifier_Oid");
            String model = snmp.get(ip, modelOid).trim();
            if (models.contains(model)) {
                try {
                    String modelAddress = getAddressByModelName(model);
                    if (modelAddress != null) {
                        exec.execute(new TelnetConfigureThread(ip, authData, modelAddress));
                    } else {
                        throw new FileNotFoundException();
                    }
                } catch (FileNotFoundException ex) {
                    System.exit(0);
                }
            } else {
            }
        });
        exec.shutdown();
    } catch (Exception e) {
        System.out.println(e);
        //ip address is incorrect
    }
}

From source file:com.paniclauncher.workers.InstanceInstaller.java

private void downloadMods(ArrayList<Mod> mods) {
    fireSubProgressUnknown();/*from   w  ww.  j av  a  2s. c om*/
    ExecutorService executor = Executors.newFixedThreadPool(8);
    ArrayList<PanicLauncherDownloadable> downloads = getDownloadableMods();
    totalDownloads = downloads.size();

    for (PanicLauncherDownloadable download : downloads) {
        executor.execute(download);
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }

    for (Mod mod : mods) {
        if (!downloads.contains(mod) && !isCancelled()) {
            fireTask(App.settings.getLocalizedString("common.downloading") + " " + mod.getFile());
            mod.download(this);
        }
    }
}

From source file:com.paniclauncher.workers.InstanceInstaller.java

private void downloadMojangStuffNew() {
    firePropertyChange("doing", null, App.settings.getLocalizedString("instance.downloadingresources"));
    firePropertyChange("subprogressint", null, null);
    ExecutorService executor = Executors.newFixedThreadPool(8);
    ArrayList<MojangDownloadable> downloads = getNeededResources();
    totalResources = downloads.size();//from   ww w  .  j  av a 2 s .c  o m

    for (MojangDownloadable download : downloads) {
        executor.execute(download);
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }
    if (!isCancelled()) {
        fireTask(App.settings.getLocalizedString("instance.organisinglibraries"));
        fireSubProgress(0);
        if (!isServer) {
            String[] libraries = librariesNeeded.split(",");
            for (String libraryFile : libraries) {
                Utils.copyFile(new File(App.settings.getLibrariesDir(), libraryFile), getBinDirectory());
            }
            String[] natives = nativesNeeded.split(",");
            for (String nativeFile : natives) {
                Utils.unzip(new File(App.settings.getLibrariesDir(), nativeFile), getNativesDirectory());
            }
            Utils.delete(new File(getNativesDirectory(), "META-INF"));
        }
        if (isServer) {
            Utils.copyFile(
                    new File(App.settings.getJarsDir(), "minecraft_server." + this.minecraftVersion + ".jar"),
                    getRootDirectory());
        } else {
            Utils.copyFile(new File(App.settings.getJarsDir(), this.minecraftVersion + ".jar"),
                    new File(getBinDirectory(), "minecraft.jar"), true);
        }
    }
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

static <T> void executeAction(final LinkedBlockingQueue<T> queue, final int numThreads, final Action<T> action)
        throws IOException {
    final CountDownLatch failureLatch = new CountDownLatch(1);
    final CountDownLatch doneLatch = new CountDownLatch(queue.size());
    final AtomicInteger numFailures = new AtomicInteger(0);
    final AtomicInteger completedThreads = new AtomicInteger(0);

    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    try {//from w w w  . j a v a 2 s  . c  om
        for (int i = 0; i < numThreads; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        T item = queue.poll();
                        if (null == item) {
                            break;
                        }
                        try {
                            action.execute(item);
                        } catch (IOException ioe) {
                            logger.error("Failed to execute action on item '{}'", item, ioe);
                            numFailures.incrementAndGet();
                            failureLatch.countDown();
                            break;
                        }
                        doneLatch.countDown();
                    }
                    if (numFailures.get() == 0 && completedThreads.incrementAndGet() == numThreads) {
                        failureLatch.countDown();
                    }
                }
            });
        }
        try {
            failureLatch.await();
            if (numFailures.get() > 0) {
                throw new IOException("Encountered " + numFailures.get() + " failures on executing action.");
            }
            doneLatch.await();
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
            logger.warn("Interrupted on executing action", ie);
            throw new DLInterruptedException("Interrupted on executing action", ie);
        }
    } finally {
        executorService.shutdown();
    }
}

From source file:com.trellmor.berrymotes.sync.EmoteDownloader.java

public void start(SyncResult syncResult) {
    Log.info("EmoteDownload started");

    this.updateNetworkInfo();

    mSyncResult = syncResult;/*from w w w  . j  a va  2  s . c  om*/

    if (!mIsConnected) {
        Log.error("Network not available");
        syncResult.stats.numIoExceptions++;
        return;
    }

    // Registers BroadcastReceiver to track network connection changes.
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    NetworkReceiver receiver = new NetworkReceiver();
    mContext.registerReceiver(receiver, filter);

    ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);

    mHttpClient = AndroidHttpClient.newInstance(USER_AGENT);
    try {
        String[] subreddits = getSubreddits();

        for (String subreddit : subreddits) {
            if (mSubreddits.isChecked(subreddit)) {
                Runnable subredditEmoteDownloader = new SubredditEmoteDownloader(mContext, this, subreddit);
                executor.execute(subredditEmoteDownloader);
            } else {
                // Delete this subreddit
                deleteSubreddit(subreddit, mContentResolver);
                // Reset last download date
                SharedPreferences.Editor settings = PreferenceManager.getDefaultSharedPreferences(mContext)
                        .edit();
                settings.remove(SettingsActivity.KEY_SYNC_LAST_MODIFIED + subreddit);
                settings.commit();
            }
        }
        executor.shutdown();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
    } catch (URISyntaxException e) {
        Log.error("Emotes URL is malformed", e);
        synchronized (mSyncResult) {
            mSyncResult.stats.numParseExceptions++;
            if (mSyncResult.delayUntil < 60 * 60)
                mSyncResult.delayUntil = 60 * 60;
        }
        return;
    } catch (IOException e) {
        Log.error("Error reading from network: " + e.getMessage(), e);
        synchronized (mSyncResult) {
            mSyncResult.stats.numIoExceptions++;
            if (mSyncResult.delayUntil < 30 * 60)
                mSyncResult.delayUntil = 30 * 60;
        }
        return;
    } catch (InterruptedException e) {
        synchronized (mSyncResult) {
            syncResult.moreRecordsToGet = true;
        }

        Log.info("Sync interrupted");

        executor.shutdownNow();
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
        } catch (InterruptedException e2) {
        }

        Thread.currentThread().interrupt();
    } finally {
        Log.info("Deleted emotes: " + Long.toString(mSyncResult.stats.numDeletes));
        Log.info("Added emotes: " + Long.toString(mSyncResult.stats.numInserts));

        // Unregisters BroadcastReceiver at the end
        mContext.unregisterReceiver(receiver);

        mHttpClient.close();
    }

    Log.info("EmoteDownload finished");
}

From source file:com.fluidops.iwb.provider.CkanProvider.java

@Override
public void gather(List<Statement> res) throws Exception {
    // Read CKAN location and establish connection
    URL registryUrl = new URL(config.location);
    HttpURLConnection registryConnection = (HttpURLConnection) registryUrl.openConnection();
    registryConnection.setRequestMethod("GET");

    // Check if connection to CKAN could be established
    if (registryConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        String msg = String.format("Connection to the CKAN registry could not be established. (%s, %s)",
                registryConnection.getResponseCode(), registryConnection.getResponseMessage());
        logger.info(msg);//from  w w w .  j a v  a  2s. com
        throw new IllegalStateException(msg);
    }
    logger.trace("Connection to CKAN established successfully.");

    String siteContent = GenUtil.readUrl(registryConnection.getInputStream());

    JSONObject groupAsJson = null;
    JSONArray packageListJsonArray = null;
    try {
        groupAsJson = new JSONObject(new JSONTokener(siteContent));
        packageListJsonArray = groupAsJson.getJSONArray("packages");
    } catch (JSONException e) {
        String msg = String.format("Returned content %s is not valid JSON. Check if the registry URL is valid.",
                siteContent);
        logger.debug(msg);
        throw new IllegalStateException(msg);
    }
    logger.trace("Extracted JSON from CKAN successfully");

    // Create metadata about LOD catalog
    res.add(ProviderUtils.createStatement(CKAN.CKAN_CATALOG, RDF.TYPE, Vocabulary.DCAT.CATALOG));
    res.add(ProviderUtils.createStatement(CKAN.CKAN_CATALOG, RDFS.LABEL, CKAN.CKAN_CATALOG_LABEL));

    // Extract metadata for individual data sets listed in CKAN
    MultiThreadedHttpConnectionManager connectionManager = null;
    ExecutorService pool = null;
    try {
        pool = Executors.newFixedThreadPool(10);
        connectionManager = new MultiThreadedHttpConnectionManager();
        HttpClient client = new HttpClient(connectionManager);

        List<Statement> synchedList = Collections.synchronizedList(res);
        for (int i = 0; i < packageListJsonArray.length(); i++) {
            String host = "http://www.ckan.net/package/" + packageListJsonArray.get(i).toString();
            String baseUri = findBaseUri(
                    "http://www.ckan.net/api/rest/package/" + packageListJsonArray.get(i).toString());
            baseUri = (baseUri == null) ? host : baseUri;
            pool.execute(new MetadataReader(client, host, baseUri, CKAN.CKAN_CATALOG, synchedList));
        }
    } finally {
        if (pool != null) {
            pool.shutdown();
            pool.awaitTermination(4, TimeUnit.HOURS);
        }
        if (connectionManager != null)
            connectionManager.shutdown();
    }
}

From source file:com.datastax.loader.CqlDelimLoad.java

public boolean run(String[] args)
        throws IOException, ParseException, InterruptedException, ExecutionException, KeyStoreException,
        NoSuchAlgorithmException, KeyManagementException, CertificateException, UnrecoverableKeyException {
    if (false == parseArgs(args)) {
        System.err.println("Bad arguments");
        System.err.println(usage());
        return false;
    }/*  www  .ja va  2s.  c  o m*/

    // Setup
    if (false == setup())
        return false;

    // open file
    Deque<File> fileList = new ArrayDeque<File>();
    File infile = null;
    File[] inFileList = null;
    boolean onefile = true;
    if (STDIN.equalsIgnoreCase(filename)) {
        infile = null;
    } else {
        infile = new File(filename);
        if (infile.isFile()) {
        } else {
            inFileList = infile.listFiles();
            if (inFileList.length < 1)
                throw new IOException("directory is empty");
            onefile = false;
            Arrays.sort(inFileList, new Comparator<File>() {
                public int compare(File f1, File f2) {
                    return f1.getName().compareTo(f2.getName());
                }
            });
            for (int i = 0; i < inFileList.length; i++)
                fileList.push(inFileList[i]);
        }
    }

    // Launch Threads
    ExecutorService executor;
    long total = 0;
    if (onefile) {
        // One file/stdin to process
        executor = Executors.newSingleThreadExecutor();
        Callable<Long> worker = new CqlDelimLoadTask(cqlSchema, delimiter, charsPerColumn, nullString,
                commentString, dateFormatString, localDateFormatString, boolStyle, locale, maxErrors, skipRows,
                skipCols, maxRows, badDir, infile, session, consistencyLevel, numFutures, batchSize, numRetries,
                queryTimeout, maxInsertErrors, successDir, failureDir, nullsUnset, format, keyspace, table);
        Future<Long> res = executor.submit(worker);
        total = res.get();
        executor.shutdown();
    } else {
        executor = Executors.newFixedThreadPool(numThreads);
        Set<Future<Long>> results = new HashSet<Future<Long>>();
        while (!fileList.isEmpty()) {
            File tFile = fileList.pop();
            Callable<Long> worker = new CqlDelimLoadTask(cqlSchema, delimiter, charsPerColumn, nullString,
                    commentString, dateFormatString, localDateFormatString, boolStyle, locale, maxErrors,
                    skipRows, skipCols, maxRows, badDir, tFile, session, consistencyLevel, numFutures,
                    batchSize, numRetries, queryTimeout, maxInsertErrors, successDir, failureDir, nullsUnset,
                    format, keyspace, table);
            results.add(executor.submit(worker));
        }
        executor.shutdown();
        for (Future<Long> res : results)
            total += res.get();
    }

    // Cleanup
    cleanup();
    //System.err.println("Total rows inserted: " + total);

    return true;
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

public Pair<Set<Long>, Set<Long>> collectLedgers(List<URI> uris, List<List<String>> allocationPaths)
        throws IOException {
    Preconditions.checkArgument(uris.size() > 0, "No uri provided to audit");

    String zkServers = validateAndGetZKServers(uris);
    RetryPolicy retryPolicy = new BoundExponentialBackoffRetryPolicy(conf.getZKRetryBackoffStartMillis(),
            conf.getZKRetryBackoffMaxMillis(), Integer.MAX_VALUE);
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().name("DLAuditor-ZK").zkServers(zkServers)
            .sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryPolicy(retryPolicy)
            .zkAclId(conf.getZkAclId()).build();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {/*from   w w  w.  j a  v a 2  s  . c  o  m*/
        BKDLConfig bkdlConfig = resolveBKDLConfig(zkc, uris);
        logger.info("Resolved bookkeeper config : {}", bkdlConfig);

        BookKeeperClient bkc = BookKeeperClientBuilder.newBuilder().name("DLAuditor-BK").dlConfig(conf)
                .zkServers(bkdlConfig.getBkZkServersForWriter()).ledgersPath(bkdlConfig.getBkLedgersPath())
                .build();
        try {
            Set<Long> bkLedgers = collectLedgersFromBK(bkc, executorService);
            Set<Long> dlLedgers = collectLedgersFromDL(uris, allocationPaths);
            return Pair.of(bkLedgers, dlLedgers);
        } finally {
            bkc.close();
        }
    } finally {
        zkc.close();
        executorService.shutdown();
    }
}

From source file:com.alibaba.otter.shared.arbitrate.zookeeper.DistributedReentrantLockTest.java

@Test
protected void test_lock() {
    ExecutorService exeucotr = Executors.newCachedThreadPool();
    final int count = 50;
    final CountDownLatch latch = new CountDownLatch(count);

    final DistributedReentrantLock lock = new DistributedReentrantLock(dir);
    for (int i = 0; i < count; i++) {
        exeucotr.submit(new Runnable() {

            public void run() {
                try {
                    Thread.sleep(1000);
                    lock.lock();//from   w w w. ja v  a2 s.  c o m
                    Thread.sleep(100 + RandomUtils.nextInt(100));

                    System.out.println("id: " + lock.getId() + " is leader: " + lock.isOwner());
                } catch (InterruptedException e) {
                    want.fail();
                } catch (KeeperException e) {
                    want.fail();
                } finally {
                    latch.countDown();
                    try {
                        lock.unlock();
                    } catch (KeeperException e) {
                        want.fail();
                    }
                }

            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }

    exeucotr.shutdown();
}

From source file:info.magnolia.imaging.caching.CachingImageStreamerRepositoryTest.java

/**
 * This test is not executed by default - too long !
 * Used to reproduce the "session already closed issue", see MGNLIMG-59.
 * Set the "expiration" property of the jobs map in CachingImageStreamer to a longer value
 * to have more chances of reproducing the problem.
 *///w  w w.  j a  v  a  2s.  c  o m
@Ignore
@Test
public void testConcurrencyAndJCRSessions() throws Exception {
    final HierarchyManager srcHM = MgnlContext.getHierarchyManager("website");
    final String srcPath = "/foo/bar";
    ContentUtil.createPath(srcHM, srcPath);

    // ParameterProvider for tests - return a new instance of the same node everytime
    // if we'd return the same src instance everytime, the purpose of this test would be null
    final ParameterProviderFactory<Object, Content> ppf = new TestParameterProviderFactory(srcHM, srcPath);

    final OutputFormat png = new OutputFormat();
    png.setFormatName("png");

    final ImageOperationChain<ParameterProvider<Content>> generator = new ImageOperationChain<ParameterProvider<Content>>();
    final URLImageLoader<ParameterProvider<Content>> load = new URLImageLoader<ParameterProvider<Content>>();
    load.setUrl(getClass().getResource("/funnel.gif").toExternalForm());
    generator.addOperation(load);
    generator.setOutputFormat(png);
    generator.setName("foo blob bar");
    generator.setParameterProviderFactory(ppf);

    // yeah, we're using a "wrong" workspace for the image cache, to avoid having to setup a custom one in this test
    final HierarchyManager hm = MgnlContext.getHierarchyManager("config");

    final ImageStreamer streamer = new CachingImageStreamer(hm, ppf.getCachingStrategy(),
            new DefaultImageStreamer());

    // thread pool of 10, launching 8 requests, can we hit some concurrency please ?
    final ExecutorService executor = Executors.newFixedThreadPool(10);
    final ByteArrayOutputStream[] outs = new ByteArrayOutputStream[8];
    final Future[] futures = new Future[8];
    for (int i = 0; i < outs.length; i++) {
        final int ii = i;
        outs[i] = new ByteArrayOutputStream();
        futures[i] = executor.submit(new Runnable() {
            @Override
            public void run() {
                final ParameterProvider p = generator.getParameterProviderFactory()
                        .newParameterProviderFor(null);
                try {
                    streamer.serveImage(generator, p, outs[ii]);
                } catch (Exception e) {
                    throw new RuntimeException(e); // TODO
                }
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);

    for (Future<?> future : futures) {
        assertTrue(future.isDone());
        assertFalse(future.isCancelled());
        // ignore the results of TestJob - but if there was an exception thrown by TestJob.call(),
        // it is only thrown back at us when we call get() below. (so the test will fail badly if the job threw an exception)
        Object ignored = future.get();
    }

    shutdownRepository(true);

    // sleep for a while so that the jobs map's expiration thread can kick in !
    Thread.sleep(10000);
}