Example usage for java.util.concurrent ExecutorService execute

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

Introduction

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

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:fr.efl.chaine.xslt.GauloisPipe.java

/**
 * Execute the specified templates on the specified files to the specified
 * output directory on the specified number of threads.
 *
 * @param templates the specified templates
 * @param inputs the specified input files
 * @param outputDirectory the specified output directory
 * @param nbThreads the specified number of thread
 * @param processor the processor/*ww w  .  ja v  a 2s. c o m*/
 * @param listener The listener to start, if not null
 * @return <tt>false</tt> if an error occurs while processing.
 */
private boolean executesPipeOnMultiThread(final Pipe pipe, List<ParametrableFile> inputs, int nbThreads,
        Listener listener) {
    ExecutorService service = (nbThreads == 1) ? Executors.newSingleThreadExecutor(getThreadFactory())
            : Executors.newFixedThreadPool(nbThreads, getThreadFactory());
    // a try to solve multi-thread compiling problem...
    // that's a pretty dirty hack, but just a try, to test...
    if (xslCache.isEmpty() && !inputs.isEmpty()) {
        // in the opposite case, there is only a listener, and probably the first
        // file will be proccess alone...
        try {
            XsltTransformer transformer = buildTransformer(pipe, inputs.get(0).getFile(),
                    inputs.get(0).getFile().toURI().toURL().toExternalForm(),
                    ParametersMerger.merge(inputs.get(0).getParameters(), config.getParams()), messageListener,
                    null);
        } catch (IOException | InvalidSyntaxException | URISyntaxException | SaxonApiException ex) {
            String msg = "while pre-compiling for a multi-thread use...";
            LOGGER.error(msg);
            errors.add(new GauloisRunException(msg, ex));
        }
    }
    for (ParametrableFile pf : inputs) {
        final ParametrableFile fpf = pf;
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    execute(pipe, fpf, messageListener);
                } catch (SaxonApiException | IOException | InvalidSyntaxException | URISyntaxException ex) {
                    String msg = "[" + instanceName + "] while processing " + fpf.getFile().getName();
                    LOGGER.error(msg, ex);
                    errors.add(new GauloisRunException(msg, fpf.getFile()));
                }
            }
        };
        service.execute(r);
    }
    if (listener == null) {
        // on ajoute plus rien
        service.shutdown();
        try {
            service.awaitTermination(5, TimeUnit.HOURS);
            return true;
        } catch (InterruptedException ex) {
            LOGGER.error("[" + instanceName + "] multi-thread processing interrupted, 5 hour limit exceed.");
            return false;
        }
    } else {
        ExecutionContext context = new ExecutionContext(this, pipe, messageListener, service);
        final HttpListener httpListener = new HttpListener(listener.getPort(), listener.getStopKeyword(),
                context);
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                httpListener.run();
            }
        };
        new Thread(runner).start();
        return true;
    }
}

From source file:org.zaproxy.zap.extension.websocket.WebSocketProxy.java

/**
 * Start listening to the WebSocket-connection using threads from the given pool. Read also
 * buffered bytes from incoming connection (Server -> ZAP).
 *
 * @param listenerThreadPool Thread pool is provided by {@link ExtensionWebSocket}.
 * @param remoteReader This {@link InputStream} that contained the handshake response.
 * @throws WebSocketException/*from   ww w  . ja  v a2  s .  c  o m*/
 */
public void startListeners(ExecutorService listenerThreadPool, InputStream remoteReader)
        throws WebSocketException {
    setState(State.CONNECTING);

    // check if both sockets are open, otherwise no need for listening
    if (localSocket != null && (localSocket.isClosed() || !localSocket.isConnected())) {
        throw new WebSocketException("local socket is closed or not connected");
    }

    if (remoteSocket != null && (remoteSocket.isClosed() || !remoteSocket.isConnected())) {
        throw new WebSocketException("remote socket is closed or not connected");
    }

    // ensure right settings are used for our sockets
    try {
        if (localSocket != null) {
            localSocket.setSoTimeout(0); // infinite timeout
            localSocket.setTcpNoDelay(true);
            localSocket.setKeepAlive(true);
        }

        if (remoteSocket != null) {
            remoteSocket.setSoTimeout(0);
            remoteSocket.setTcpNoDelay(true);
            remoteSocket.setKeepAlive(true);
        }
    } catch (SocketException e) {
        throw new WebSocketException(e);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Start listeners for channel '" + toString() + "'.");
    }

    try {
        if (!isServerMode()) {
            // use existing InputStream for remote socket,
            // as it may already contain first WebSocket-frames
            remoteListener = createListener(remoteSocket, remoteReader, "remote");
        }
        localListener = createListener(localSocket, "local");
    } catch (WebSocketException e) {
        shutdown();
        throw e;
    }

    // need to set State.OPEN before listening to sockets, otherwise
    // it might happen, that observers are notified about a new message
    // before they are informed about a new channel state.
    setState(State.OPEN);

    if (!isServerMode()) {
        listenerThreadPool.execute(remoteListener);
    }
    listenerThreadPool.execute(localListener);
}

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

/**
 * we reorder the collection of entities in concurrency and commit them in
 * sequence, because we have to conform to the infrastructure of spring's
 * transaction management layer./*ww  w .  ja  v  a2s . co  m*/
 * 
 * @param statementName
 * @param parameterObject
 * @return
 */
private Object batchInsertAfterReordering(final String statementName, final Object parameterObject) {
    Set<String> keys = new HashSet<String>();
    keys.add(getDefaultDataSourceName());
    keys.addAll(getCobarDataSourceService().getDataSources().keySet());

    final CobarMRBase mrbase = new CobarMRBase(keys);

    ExecutorService executor = createCustomExecutorService(Runtime.getRuntime().availableProcessors(),
            "batchInsertAfterReordering");
    try {
        final StringBuffer exceptionStaktrace = new StringBuffer();

        Collection<?> paramCollection = ((BatchInsertTask) parameterObject).getEntities();

        final CountDownLatch latch = new CountDownLatch(paramCollection.size());

        Iterator<?> iter = paramCollection.iterator();
        while (iter.hasNext()) {
            final Object entity = iter.next();
            Runnable task = new Runnable() {
                public void run() {
                    try {
                        SortedMap<String, DataSource> dsMap = lookupDataSourcesByRouter(statementName, entity);
                        if (MapUtils.isEmpty(dsMap)) {
                            logger.info(
                                    "can't find routing rule for {} with parameter {}, so use default data source for it.",
                                    statementName, entity);
                            mrbase.emit(getDefaultDataSourceName(), entity);
                        } else {
                            if (dsMap.size() > 1) {
                                throw new IllegalArgumentException(
                                        "unexpected routing result, found more than 1 target data source for current entity:"
                                                + entity);
                            }
                            mrbase.emit(dsMap.firstKey(), entity);
                        }
                    } catch (Throwable t) {
                        exceptionStaktrace.append(ExceptionUtils.getFullStackTrace(t));
                    } finally {
                        latch.countDown();
                    }
                }
            };
            executor.execute(task);
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new ConcurrencyFailureException(
                    "unexpected interruption when re-arranging parameter collection into sub-collections ", e);
        }

        if (exceptionStaktrace.length() > 0) {
            throw new ConcurrencyFailureException(
                    "unpected exception when re-arranging parameter collection, check previous log for details.\n"
                            + exceptionStaktrace);
        }
    } finally {
        executor.shutdown();
    }

    List<ConcurrentRequest> requests = new ArrayList<ConcurrentRequest>();
    for (Map.Entry<String, List<Object>> entity : mrbase.getResources().entrySet()) {
        final List<Object> paramList = entity.getValue();
        if (CollectionUtils.isEmpty(paramList)) {
            continue;
        }

        String identity = entity.getKey();

        final DataSource dataSourceToUse = findDataSourceToUse(entity.getKey());

        final SqlMapClientCallback callback = new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                return executor.insert(statementName, paramList);
            }
        };

        ConcurrentRequest request = new ConcurrentRequest();
        request.setDataSource(dataSourceToUse);
        request.setAction(callback);
        request.setExecutor(getDataSourceSpecificExecutors().get(identity));
        requests.add(request);
    }
    return getConcurrentRequestProcessor().process(requests);
}

From source file:com.globalsight.connector.blaise.BlaiseCreateJobHandler.java

@ActionHandler(action = "createBlaiseJob", formClass = "com.globalsight.connector.blaise.form.CreateBlaiseJobForm")
public void createBlaiseJob(HttpServletRequest request, HttpServletResponse response, Object form)
        throws Exception {
    CreateBlaiseJobForm blaiseForm = (CreateBlaiseJobForm) form;
    SessionManager sessionMgr = this.getSessionManager(request);

    String currentCompanyId = CompanyThreadLocal.getInstance().getValue();
    User user = (User) sessionMgr.getAttribute(WebAppConstants.USER);
    if (user == null) {
        String userName = request.getParameter("userName");
        if (userName != null && !"".equals(userName)) {
            user = ServerProxy.getUserManager().getUserByName(userName);
            sessionMgr.setAttribute(WebAppConstants.USER, user);
        }//from w w w. j  a  va2  s .  co  m
    }

    long blcId = Long.parseLong(blaiseForm.getBlaiseConnectorId());
    BlaiseConnector blc = BlaiseManager.getBlaiseConnectorById(blcId);

    File attachFile = null;
    String jobCommentFilePathName = (String) sessionMgr.getAttribute("uploadAttachment");
    if (jobCommentFilePathName != null) {
        attachFile = new File(jobCommentFilePathName);
    }
    sessionMgr.removeElement("uploadAttachment");
    String attachFileName = request.getParameter("attachment");

    ExecutorService pool = Executors.newFixedThreadPool(MAX_THREAD);
    List<Long> entryIds = new ArrayList<Long>();
    List<FileProfile> fileProfileList = new ArrayList<FileProfile>();
    String fileMapFileProfile = blaiseForm.getFileMapFileProfile();
    String[] ffs = fileMapFileProfile.split(",");
    for (String ff : ffs) {
        String[] f = ff.split("-");
        entryIds.add(Long.parseLong(f[0]));
        fileProfileList.add(HibernateUtil.get(FileProfileImpl.class, Long.parseLong(f[1])));
    }

    // Claim all entries one by one.
    BlaiseHelper helper = getBlaiseHelper(blc);
    for (int i = 0; i < entryIds.size(); i++) {
        TranslationInboxEntryVo curEntry = currPageEntryMap.get(entryIds.get(i));
        if (curEntry.getEntry().isGroup()) {
            helper.claim(entryIds.get(i));
        }
    }

    long l10Id = fileProfileList.get(0).getL10nProfileId();
    BasicL10nProfile l10Profile = HibernateUtil.get(BasicL10nProfile.class, l10Id);

    String publicUuid = (String) sessionMgr.getAttribute("uuid");
    sessionMgr.removeElement("uuid");
    File srcFolder = null;
    if (publicUuid != null) {
        srcFolder = new File(AmbFileStoragePathUtils.getJobAttributeDir(), publicUuid);
    }
    // Every entry creates one job with one workflow
    for (int i = 0; i < entryIds.size(); i++) {
        TranslationInboxEntryVo curEntry = currPageEntryMap.get(entryIds.get(i));
        FileProfile curFileProfile = fileProfileList.get(i);
        String uuid = JobImpl.createUuid();
        if (srcFolder != null && srcFolder.exists()) {
            // Locate file attribute by uuid
            File trgFolder = new File(AmbFileStoragePathUtils.getJobAttributeDir(), uuid);
            FileUtil.copyFolder(srcFolder, trgFolder);
        }

        List<JobAttribute> jobAttribtues = getJobAttributes(blaiseForm.getAttributeString(), l10Profile);
        CreateBlaiseJobThread runnable = new CreateBlaiseJobThread(user, currentCompanyId, blc, blaiseForm,
                curEntry, curFileProfile, attachFile, attachFileName, uuid, jobAttribtues);
        Thread t = new MultiCompanySupportedThread(runnable);
        pool.execute(t);
    }
    pool.shutdown();
    if (srcFolder != null && srcFolder.exists()) {
        FileUtil.deleteFile(srcFolder);
    }

    pageReturn();
}

From source file:com.testmax.framework.BasePage.java

private ExecutorService setSuitePerformanceExecutor(BasePage page) {
    String url = "";
    Set testset = this.getPageURL().getUrlKeySet();
    this.newThreadCount = this.threadCount * testset.size();
    ExecutorService exec = Executors.newFixedThreadPool(this.newThreadCount);
    for (int count = 0; count < this.getThreadCount(); count++) {
        if (count == 0) {
            executeGraph.createChart(this.timeout);
            responseGraph.createChart(this.timeout);
            elaspedTimeGraph.createChart(this.timeout);
            activeThreadGraph.createChart(this.timeout);
        }/*w w  w. j a  v a 2s.  c  om*/
        for (Object actionName : testset) {
            this.action = actionName.toString();
            //System.out.println("action="+this.action);
            this.urlConfig = this.pageUrl.getUrlConfig(actionName.toString());
            url = this.getURLConfig().getUrl();
            if (url != null && url.indexOf("http") < 0) {
                url = (ConfigLoader.getConfig("WEB_SERVICE_URL").replace("[env]",
                        ConfigLoader.getConfig("QA_TEST_ENV"))) + this.getURLConfig().getUrl();

                this.getURLConfig().setUrl(url);
                //System.out.println(url);
            }

            exec.execute(new HttpThread(this, actionName.toString()));
        }
    }
    exec.shutdown();

    return (exec);

}

From source file:com.atlauncher.data.Settings.java

public void downloadUpdatedFiles() {
    ArrayList<Downloadable> downloads = getLauncherFiles();
    if (downloads != null) {
        ExecutorService executor = Executors.newFixedThreadPool(this.concurrentConnections);
        for (final Downloadable download : downloads) {
            executor.execute(new Runnable() {
                @Override//from  w w w .  j  av  a2  s .co m
                public void run() {
                    if (download.needToDownload()) {
                        LogManager.info("Downloading Launcher File " + download.getFile().getName());
                        download.download(false);
                    }
                }
            });
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
    }

    LogManager.info("Finished downloading updated files!");

    if (Language.INSTANCE.getCurrent() != null) {
        try {
            Language.INSTANCE.reload(Language.INSTANCE.getCurrent());
        } catch (IOException e) {
            logStackTrace("Couldn't reload langauge " + Language.INSTANCE.getCurrent(), e);
        }
    }
}

From source file:com.atlauncher.data.Settings.java

/**
 * Downloads and loads all external libraries used by the launcher as specified in the Configs/JSON/libraries.json
 * file.// w  ww  .  j av a 2s . c o  m
 */
private void downloadExternalLibraries() {
    LogManager.debug("Downloading external libraries");

    FileReader fr = null;

    try {
        fr = new FileReader(new File(this.jsonDir, "libraries.json"));

        java.lang.reflect.Type type = new TypeToken<List<LauncherLibrary>>() {
        }.getType();

        this.launcherLibraries = Gsons.DEFAULT.fromJson(fr, type);
    } catch (Exception e) {
        logStackTrace(e);
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    ExecutorService executor = Executors.newFixedThreadPool(getConcurrentConnections());

    for (final LauncherLibrary library : this.launcherLibraries) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                Downloadable download = library.getDownloadable();

                if (download.needToDownload()) {
                    LogManager.info("Downloading library " + library.getFilename() + "!");
                    download.download(false);
                }
            }
        });
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }

    for (LauncherLibrary library : this.launcherLibraries) {
        File file = library.getFile();

        if (library.shouldAutoLoad() && !Utils.addToClasspath(file)) {
            LogManager.error("Couldn't add " + file + " to the classpath!");
            if (library.shouldExitOnFail()) {
                LogManager.error("Library is necessary so launcher will exit!");
                System.exit(1);
            }
        }
    }

    LogManager.debug("Finished downloading external libraries");
}

From source file:com.atlauncher.data.Settings.java

/**
 * Loads info about the different Minecraft versions
 *///w  w w . j av a 2 s. c o m
private void loadMinecraftVersions() {
    LogManager.debug("Loading Minecraft versions");
    this.minecraftVersions = new HashMap<String, MinecraftVersion>();
    List<MinecraftVersion> list = new ArrayList<MinecraftVersion>();
    try {
        java.lang.reflect.Type type = new TypeToken<List<MinecraftVersion>>() {
        }.getType();
        list = Gsons.DEFAULT.fromJson(new FileReader(new File(getJSONDir(), "minecraftversions.json")), type);
    } catch (JsonSyntaxException e) {
        logStackTrace(e);
    } catch (JsonIOException e) {
        logStackTrace(e);
    } catch (FileNotFoundException e) {
        logStackTrace(e);
    }
    if (list == null) {
        LogManager.error("Error loading Minecraft Versions. List was null. Exiting!");
        System.exit(1); // Cannot recover from this so exit
    }
    for (MinecraftVersion mv : list) {
        this.minecraftVersions.put(mv.getVersion(), mv);
    }
    LogManager.info("[Background] Checking Minecraft Versions Started");
    ExecutorService executor = Executors.newFixedThreadPool(this.concurrentConnections);
    for (final Entry<String, MinecraftVersion> entry : this.minecraftVersions.entrySet()) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                entry.getValue().loadVersion();
            }
        });
    }
    executor.execute(new Runnable() {
        @Override
        public void run() {
            LogManager.info("[Background] Checking Minecraft Versions Complete");
        }
    });
    executor.shutdown();
    while (!executor.isShutdown()) {
    }
    LogManager.debug("Finished loading Minecraft versions");
}

From source file:de.uni_rostock.goodod.evaluator.OntologyTest.java

public void executeTest() throws Throwable {

    ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    Set<URI> fromOntologies = new HashSet<URI>(25);
    Set<URI> toOntologies = new HashSet<URI>(25);
    Set<? extends OWLOntologyIRIMapper> bioTopLiteMapper = null;
    if (null != bioTopLiteURI) {
        bioTopLiteMapper = Collections.singleton(new SimpleIRIMapper(
                IRI.create("http://purl.org/biotop/biotoplite.owl"), IRI.create(bioTopLiteURI)));
    }/*from w  w w .  ja  va 2  s . c o m*/
    OntologyCache cache = OntologyCache.setupSharedCache(bioTopLiteMapper, getIgnoredImports(), threadCount);
    NormalizerChainFactory chain = new NormalizerChainFactory();/* new NormalizerChainFactory(importer, intersector, namer, decomposer, subsumer);*/
    cache.setNormalizerFactory(chain);

    fromOntologies.addAll(groupAOntologies);

    if (globalConfig.getBoolean("one-way", false)) {
        /*
         * If one way comparisons are requested, we only compare group A to
         * group B (and model).
         */
        toOntologies.addAll(groupBOntologies);
    } else {

        /*
         * By default, we do cross-comparisons between the groups, so we
         * create a global set for both. For simplicity, this just 
         * means adding the second set to fromOntologies and aliasing it
         * as toOntologies.
         */

        fromOntologies.addAll(groupBOntologies);
        toOntologies = fromOntologies;
    }

    if (null != modelOntology) {
        toOntologies.add(modelOntology);
    }
    logger.info("Running comparisons for test '" + getTestName() + "'.");

    for (URI u1 : fromOntologies) {
        for (URI u2 : toOntologies) {
            if (u1.equals(u2)) {
                continue;
            }
            /*
             *  Working with the ontologies is resource intensive. We want
             *  to handle more than one at a time, especially on multicore
             *  machines, but neigher starving ourselves from I/O nor
             *  generating massive cache or memory churn is very smart.
             */
            int waitCount = 0;
            while (inProgressCount.get() > threadCount) {
                if (0 == ++waitCount % 8) {

                    /* 
                     * Thight loop a few times, then yield in order to let
                     * the other threads finish.
                     */
                    Thread.yield();
                }
            }
            comparisonStarted();
            try {
                OntologyPair p = new OntologyPair(cache, u1, u2);
                executor.execute(new ComparisonRunner(u1, u2, p));
            } catch (Throwable e) {
                logger.warn("Could not compare " + u1.toString() + " and " + u2.toString() + ".", e);
                Set<URI> values = failedComparisons.get(u1);
                if (null != values) {
                    values.add(u2);
                } else {
                    values = new HashSet<URI>();
                    values.add(u2);
                    failedComparisons.put(u2, values);
                }
            }
        }

    }
    executor.shutdown();
    while (false == executor.isTerminated()) {
        // wait until we're done.
    }
    logger.info("Comparisons on '" + getTestName() + "' completed.");
    if (logger.isDebugEnabled()) {
        writeNormalizedOntologiesTo(fromOntologies, cache, new File(System.getProperty("java.io.tmpdir")));
    }
    cache.teardown();
    cache = null;
}