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:edu.cornell.med.icb.R.TestRConnectionPool.java

/**
 * Checks that two threads actually get the same connection pool.
 * @throws InterruptedException if the threads are interrupted during the test
 *//* w  w  w . j  a v a2s  . c  o  m*/
@Test
public void validateSingleton() throws InterruptedException {
    final RConnectionPool[] pools = new RConnectionPool[2];
    final CountDownLatch latch = new CountDownLatch(2);
    final ExecutorService threadPool = Executors.newCachedThreadPool();
    try {
        threadPool.submit(new Callable<Boolean>() {
            public Boolean call() {
                pools[0] = RConnectionPool.getInstance();
                latch.countDown();
                return true;
            }
        });

        threadPool.submit(new Callable<Boolean>() {
            public Boolean call() {
                pools[1] = RConnectionPool.getInstance();
                latch.countDown();
                return true;
            }
        });

        latch.await();

        assertNotNull("Connection pool should never be null", pools[0]);
        assertNotNull("Connection pool should never be null", pools[1]);
        assertEquals("Pools should be the same", pools[0], pools[1]);
    } finally {
        threadPool.shutdown();

        if (pools[0] != null) {
            pools[0].close();
        }

        if (pools[1] != null) {
            pools[1].close();
        }
    }
}

From source file:hivemall.mix.server.MixServerTest.java

@Test
public void testSimpleScenario() throws InterruptedException {
    int port = NetUtils.getAvailablePort();
    CommandLine cl = CommandLineUtils.parseOptions(
            new String[] { "-port", Integer.toString(port), "-sync_threshold", "3" }, MixServer.getOptions());
    MixServer server = new MixServer(cl);
    ExecutorService serverExec = Executors.newSingleThreadExecutor();
    serverExec.submit(server);/*ww w . j a v a 2s  . co m*/

    waitForState(server, ServerState.RUNNING);

    PredictionModel model = new DenseModel(16777216, false);
    model.configureClock();
    MixClient client = null;
    try {
        client = new MixClient(MixEventName.average, "testSimpleScenario", "localhost:" + port, false, 2,
                model);
        model.configureMix(client, false);

        final Random rand = new Random(43);
        for (int i = 0; i < 100000; i++) {
            Integer feature = Integer.valueOf(rand.nextInt(100));
            float weight = (float) rand.nextGaussian();
            model.set(feature, new WeightValue(weight));
        }

        Thread.sleep(5000L);

        long numMixed = model.getNumMixed();
        Assert.assertEquals("number of mix events: " + numMixed, numMixed, 0L);

        serverExec.shutdown();
    } finally {
        IOUtils.closeQuietly(client);
    }
}

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

private List<String> createTestClips(FPPool pool, int maxBlobSize, int thisMany, Writer summaryWriter)
        throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS);

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

    List<String> clipIds = Collections.synchronizedList(new ArrayList<String>());
    List<String> summaries = Collections.synchronizedList(new ArrayList<String>());
    for (int clipIdx = 0; clipIdx < thisMany; clipIdx++) {
        service.submit(new ClipWriter(pool, clipIds, maxBlobSize, summaries));
    }//from   w  w w  .java  2 s.  c o m

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

    Collections.sort(summaries);
    for (String summary : summaries) {
        summaryWriter.append(summary);
    }

    System.out.println();

    return clipIds;
}

From source file:de.rwth.dbis.acis.activitytracker.service.ActivityTrackerService.java

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)//from w  ww . j a va 2 s  .c o m
@ApiOperation(value = "This method returns a list of activities", notes = "Default the latest ten activities will be returned")
@ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Returns a list of activities"),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found"),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal server problems") })
//TODO add filter
public HttpResponse getActivities(
        @ApiParam(value = "Before cursor pagination", required = false) @DefaultValue("-1") @QueryParam("before") int before,
        @ApiParam(value = "After cursor pagination", required = false) @DefaultValue("-1") @QueryParam("after") int after,
        @ApiParam(value = "Limit of elements of components", required = false) @DefaultValue("10") @QueryParam("limit") int limit,
        @ApiParam(value = "User authorization token", required = false) @DefaultValue("") @HeaderParam("authorization") String authorizationToken) {

    DALFacade dalFacade = null;
    try {
        if (before != -1 && after != -1) {
            ExceptionHandler.getInstance().throwException(ExceptionLocation.ACTIVITIESERVICE,
                    ErrorCode.WRONG_PARAMETER, "both: before and after parameter not possible");
        }
        int cursor = before != -1 ? before : after;
        Pageable.SortDirection sortDirection = after != -1 ? Pageable.SortDirection.ASC
                : Pageable.SortDirection.DESC;

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(20);
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();

        dalFacade = getDBConnection();
        Gson gson = new Gson();
        ExecutorService executor = Executors.newCachedThreadPool();

        int getObjectCount = 0;
        PaginationResult<Activity> activities;
        List<ActivityEx> activitiesEx = new ArrayList<>();
        Pageable pageInfo = new PageInfo(cursor, limit, "", sortDirection);
        while (activitiesEx.size() < limit && getObjectCount < 5) {
            pageInfo = new PageInfo(cursor, limit, "", sortDirection);
            activities = dalFacade.findActivities(pageInfo);
            getObjectCount++;
            cursor = sortDirection == Pageable.SortDirection.ASC ? cursor + limit : cursor - limit;
            if (cursor < 0) {
                cursor = 0;
            }
            activitiesEx.addAll(
                    getObjectBodies(httpclient, executor, authorizationToken, activities.getElements()));
        }

        executor.shutdown();
        if (activitiesEx.size() > limit) {
            activitiesEx = activitiesEx.subList(0, limit);
        }
        PaginationResult<ActivityEx> activitiesExResult = new PaginationResult<>(pageInfo, activitiesEx);

        HttpResponse response = new HttpResponse(gson.toJson(activitiesExResult.getElements()),
                HttpURLConnection.HTTP_OK);
        Map<String, String> parameter = new HashMap<>();
        parameter.put("limit", String.valueOf(limit));
        response = this.addPaginationToHtppResponse(activitiesExResult, "", parameter, response);

        return response;

    } catch (ActivityTrackerException atException) {
        return new HttpResponse(ExceptionHandler.getInstance().toJSON(atException),
                HttpURLConnection.HTTP_INTERNAL_ERROR);
    } catch (Exception ex) {
        ActivityTrackerException atException = ExceptionHandler.getInstance().convert(ex,
                ExceptionLocation.ACTIVITIESERVICE, ErrorCode.UNKNOWN, ex.getMessage());
        return new HttpResponse(ExceptionHandler.getInstance().toJSON(atException),
                HttpURLConnection.HTTP_INTERNAL_ERROR);
    } finally {
        closeDBConnection(dalFacade);
    }
}

From source file:com.pari.nm.modules.jobs.CompareDeviceConfigJob.java

public void processGearmanJob(Set<Integer> deviceIds, Map<Integer, Map<String, Object>> perDeviceInputMap,
        JSONObject configObj) throws Exception {

    Gearman gearman = Gearman.createGearman();
    GearmanServer server = gearman.createGearmanServer("localhost", 4730);
    GearmanClient client = gearman.createGearmanClient();
    client.addServer(server);/*from  www .ja  v  a2s .c  o  m*/
    ExecutorService executor = Executors.newFixedThreadPool(100);

    List<Future<JSONObject>> list = new ArrayList<Future<JSONObject>>();

    List<String> definedOSTypes = null;

    boolean isApplicableToAllDevices = true;
    try {
        definedOSTypes = GoldenConfigDBHelper.getDefinedOSTypes();
        // if (definedOSTypes == null || definedOSTypes.size() == 0)
        // {
        // logMsg("Global definations are not defined for any OS types, hence exiting.");
        // throw new Exception("Global definations are not defined for any OS types.");
        // }
    } catch (Exception e1) {
        logger.error("Unable to load defined OS Types" + e1);
        gearman.shutdown();
        executor.shutdown();

        throw new Exception("Unable to load global OS definitions or NO OS definitions are added");
    }
    try {

        for (Integer deviceId : deviceIds) {
            NetworkNode node = NetworkNodeCache.getInstance().getNodeByID(deviceId);
            if (node != null) {
                String runConfig = node.getRunningConfig();
                String osName = node.getVersion().getOsTypeDetails().getOsName();
                if (runConfig == null || runConfig.trim().isEmpty()) {
                    logMsg(CspcXMLUtils.logMsgInStdFormat("", node.getNodeId(), node.getIpAddr().toString(),
                            System.currentTimeMillis(),
                            " Running Config has no Data . Skipping for this Device ."
                                    + node.getIpAddr().toString()));
                    continue;
                }
                configObj.put("config", runConfig);
                String ipAddress = NetworkNodeCache.getInstance().getNodeByID(deviceId).getIpAddr().toString();
                String hostName = NetworkNodeCache.getInstance().getNodeByID(deviceId).getNodeName();
                configObj.put("DeviceIp", ipAddress);
                configObj.put("DeviceId", deviceId);
                configObj.put("HostName", hostName);

                List<String> reportType = (List<String>) configObj.get("type");
                if (golbalDefinitionPerOS.get(osName) != null && !golbalDefinitionPerOS.get(osName).isEmpty()) {
                    configObj.putAll(golbalDefinitionPerOS.get(osName));
                }

                String template = configObj.get("template").toString();
                if (perDeviceInputMap.get(deviceId) != null) {
                    template = replacePerDeviceInput(template, perDeviceInputMap.get(deviceId));
                }

                configObj.put("template", template);

                boolean isAdded = GoldenConfigManagerImpl.getInstance().addDeviceConfigResult(jobName, jobId,
                        jobRunId, deviceId, reportType, null, template, runConfig);
                if (isAdded) {
                    logger.info("Added template and running config for this device " + deviceId);
                }
                logMsg(CspcXMLUtils.logMsgInStdFormat("", (int) deviceId, ipAddress, System.currentTimeMillis(),
                        "Started - Compared Device Configuration for device- " + ipAddress));

                JSONObject newConfObject = new JSONObject(configObj);
                Future<JSONObject> future = executor.submit(new CompareConfigJobSubmit(client, newConfObject));
                newConfObject = null;
                list.add(future);
            }
        }

        jobState = isApplicableToAllDevices ? JobStatus.SUCCESS : JobStatus.FAILED;
        for (Future<JSONObject> fut : list) {
            try {

                JSONObject obj = fut.get();

                int code = Integer.parseInt(obj.get("code").toString());
                // int deviceId = Integer.parseInt((String)
                // obj.get("DeviceId"));
                long deviceId = (long) obj.get("DeviceId");
                String ipAddress = obj.get("DeviceIp").toString();
                String reportName = obj.get("reportName").toString();
                if (code == 10) {

                    Map<String, String> configResult = (Map<String, String>) obj.get("output");

                    GoldenConfigManagerImpl.getInstance().updateDeviceConfigResult(jobName, jobId, jobRunId,
                            (int) deviceId, configResult, reportName);

                    logMsg(CspcXMLUtils.logMsgInStdFormat("", (int) deviceId, ipAddress,
                            System.currentTimeMillis(),
                            "Completed - Compared Device Configuration for device- " + ipAddress));
                } else if (code == -10) {
                    String msg = obj.get("msg").toString();
                    logMsg(CspcXMLUtils.logMsgInStdFormat("", (int) deviceId, ipAddress,
                            System.currentTimeMillis(), msg + ipAddress));
                } else if (code == -20) {
                    String msg = obj.get("msg").toString();
                    logMsg(CspcXMLUtils.logMsgInStdFormat("", (int) deviceId, ipAddress,
                            System.currentTimeMillis(), msg + ipAddress));

                    jobState = JobStatus.FAILED;
                }
            } catch (Exception e) {
                logger.error("Exception while getting Object from Worker.", e);
            }
        }
    } catch (Exception ex) {
        logger.error("Unable to load Process Compare Job" + ex);

    } finally {
        executor.shutdown();
        gearman.shutdown();

        if (templateBundleExtract != null && templateBundleExtract.exists()) {
            templateBundleExtract.delete();
        }
    }
    jobStatus.setState(jobId, JobStageConstants.DefaultJobStages.COMPLETED, 0, "Execution Completed.");
    context.setResult("Completed");
    JobRun.logJobCompletionStatus(jobId, jobRunId, ((jobState == JobStatus.SUCCESS) ? true : false));
}

From source file:com.graphhopper.jsprit.analysis.toolbox.ConcurrentBenchmarker.java

public void run() {
    System.out.println("start benchmarking [nuOfInstances=" + benchmarkInstances.size() + "][runsPerInstance="
            + runs + "]");
    double startTime = System.currentTimeMillis();
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    List<Future<BenchmarkResult>> futures = new ArrayList<Future<BenchmarkResult>>();
    for (final BenchmarkInstance p : benchmarkInstances) {

        Future<BenchmarkResult> futureResult = executor.submit(new Callable<BenchmarkResult>() {

            @Override/*  w  w  w .jav  a  2  s .c om*/
            public BenchmarkResult call() throws Exception {
                return runAlgoAndGetResult(p);
            }

        });
        futures.add(futureResult);

    }
    try {
        int count = 1;
        for (Future<BenchmarkResult> f : futures) {
            BenchmarkResult r = f.get();
            print(r, count);
            results.add(f.get());
            count++;
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    executor.shutdown();
    print(results);
    System.out.println("done [time=" + (System.currentTimeMillis() - startTime) / 1000 + "sec]");
}

From source file:com.jivesoftware.os.amza.service.AmzaService.java

public void compactAllTombstones() throws Exception {
    LOG.info("Manual compact all tombstones requests.");

    ExecutorService compactorPool = BoundedExecutor.newBoundedExecutor(numberOfStripes, "compactor");

    try {/*from   ww w  . j ava  2  s.com*/
        List<Future<?>> runnables = Lists.newArrayList();
        for (int i = 0; i < numberOfStripes; i++) {
            int stripe = i;
            runnables.add(compactorPool.submit(() -> {
                partitionTombstoneCompactor.compactTombstone(true, stripe);
                return null;
            }));
        }
        for (Future<?> runnable : runnables) {
            runnable.get();
        }
    } finally {
        compactorPool.shutdown();
    }
}

From source file:com.saucelabs.sauce_ondemand.driver.SauceOnDemandSPIImpl.java

/**
 * Creates a list of WebDriver instances based on the contents of a SAUCE_ONDEMAND_BROWSERS environment variable (typically set
 * by the Sauce Jenkins plugin).//from   w  ww  .j a  va  2s .  c  o m
 *
 * @param seleniumFactory
 * @param browserURL
 * @return
 */
@Override
public List<WebDriver> createWebDrivers(SeleniumFactory seleniumFactory, final String browserURL) {

    List<WebDriver> webDrivers = new ArrayList<WebDriver>();
    String browserJson = readPropertyOrEnv("SAUCE_ONDEMAND_BROWSERS", null);
    if (browserJson == null) {
        throw new IllegalArgumentException("Unable to find SAUCE_ONDEMAND_BROWSERS environment variable");
    }
    //parse JSON and extract the browser urls, so that we know how many threads to schedule
    List<String> browsers = new ArrayList<String>();
    try {
        JSONArray array = new JSONArray(new JSONTokener(browserJson));
        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            String uri = object.getString("url");
            if (!uri.startsWith(SCHEME))
                return null; // not ours

            uri = uri.substring(SCHEME.length());
            if (!uri.startsWith("?"))
                throw new IllegalArgumentException("Missing '?':" + uri);
            browsers.add(uri);
        }
    } catch (JSONException e) {
        throw new IllegalArgumentException("Error parsing JSON", e);
    }

    //create a fixed thread pool for the number of browser
    ExecutorService service = Executors.newFixedThreadPool(browsers.size());
    List<Callable<WebDriver>> callables = new ArrayList<Callable<WebDriver>>();
    for (final String browser : browsers) {
        callables.add(new Callable<WebDriver>() {
            public WebDriver call() throws Exception {
                return createWebDriver(browserURL, null, browser);
            }
        });
    }
    //invoke all the callables, and wait for each thread to return
    try {
        List<Future<WebDriver>> futures = service.invokeAll(callables);
        for (Future<WebDriver> future : futures) {
            webDrivers.add(future.get());
        }
    } catch (InterruptedException e) {
        throw new IllegalArgumentException("Error retrieving webdriver", e);
    } catch (ExecutionException e) {
        throw new IllegalArgumentException("Error retrieving webdriver", e);
    }
    service.shutdown();

    return Collections.unmodifiableList(webDrivers);
}

From source file:eu.edisonproject.training.wsd.Wikidata.java

private Map<CharSequence, List<CharSequence>> getbroaderIDS(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    Map<CharSequence, List<CharSequence>> map = new HashMap<>();
    if (terms.size() > 0) {
        int maxT = 2;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT);
        ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue);
        //            ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 5000L, TimeUnit.MILLISECONDS,  new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy());

        Set<Future<Map<CharSequence, List<CharSequence>>>> set1 = new HashSet<>();
        String prop = "P31";
        for (Term t : terms) {
            URL url = new URL(
                    PAGE + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUid());
            Logger.getLogger(Wikidata.class.getName()).log(Level.FINE, url.toString());
            WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 1);
            Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req);
            set1.add(future);/*from w  w  w  . j ava  2 s .  c  o  m*/
        }
        pool.shutdown();

        for (Future<Map<CharSequence, List<CharSequence>>> future : set1) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<CharSequence, List<CharSequence>> c = future.get();
            if (c != null) {
                map.putAll(c);
            }
        }
    }

    return map;
}