Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:de.tu_dortmund.ub.data.dswarm.TaskProcessingUnit.java

private static void executeTransform(final String inputDataModelID, final String outputDataModelID,
        final Optional<Boolean> optionalDoIngestOnTheFly, final Optional<Boolean> optionalDoExportOnTheFly,
        final Optional<String> optionalExportMimeType, final Optional<String> optionalExportFileExtension,
        final Integer engineThreads, final String serviceName, final Properties config) throws Exception {

    // create job list
    final LinkedList<Callable<String>> transforms = new LinkedList<>();
    transforms.add(new Transform(config, inputDataModelID, outputDataModelID, optionalDoIngestOnTheFly,
            optionalDoExportOnTheFly, optionalExportMimeType, optionalExportFileExtension, 0));

    // work on jobs
    final ThreadPoolExecutor pool = new ThreadPoolExecutor(engineThreads, engineThreads, 0L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    execute(serviceName, transforms, pool);
}

From source file:de.tu_dortmund.ub.data.dswarm.TaskProcessingUnit.java

private static void executeExport(final String exportDataModelID, final Optional<String> optionalExportMimeType,
        final Optional<String> optionalExportFileExtension, final Integer engineThreads,
        final String serviceName, final Properties config) throws Exception {

    // create job list
    final LinkedList<Callable<String>> exports = new LinkedList<>();
    exports.add(new Export(exportDataModelID, optionalExportMimeType, optionalExportFileExtension, config));

    // work on jobs
    final ThreadPoolExecutor pool = new ThreadPoolExecutor(engineThreads, engineThreads, 0L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    execute(serviceName, exports, pool);
}

From source file:dk.dbc.opensearch.datadock.DatadockMain.java

private void initializeServices() throws ObjectRepositoryException, InstantiationException,
        IllegalAccessException, PluginException, HarvesterIOException, IllegalStateException,
        ParserConfigurationException, IOException, IllegalArgumentException, SQLException,
        InvocationTargetException, SAXException, ConfigurationException, ClassNotFoundException {
    log.trace("Initializing plugin resolver");
    //        String host = FedoraConfig.getHost();
    //        String port = FedoraConfig.getPort();
    //        String user = FedoraConfig.getUser();
    //        String pass = FedoraConfig.getPassPhrase();
    FcrepoReader reader = new FcrepoReader(host, port);
    FcrepoModifier modifier = new FcrepoModifier(host, port, user, pass);
    PluginResolver pluginResolver = new PluginResolver();

    //String javascriptPath = FileSystemConfig.getScriptPath();

    flowMapCreator = new FlowMapCreator(this.pluginFlowXmlPath, this.pluginFlowXsdPath);
    Map<String, List<PluginTask>> flowMap = flowMapCreator.createMap(pluginResolver, reader, modifier,
            javascriptPath);//from  ww  w .  j  av  a  2  s .co m

    log.trace("Initializing harvester");
    IHarvest harvester = this.initializeHarvester();

    log.trace("Initializing the DatadockPool");
    LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(this.queueSize);
    ThreadPoolExecutor threadpool = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize,
            this.keepAliveTime, TimeUnit.SECONDS, queue);
    DatadockPool datadockPool = new DatadockPool(threadpool, harvester, flowMap);

    log.trace("Initializing the DatadockManager");
    datadockManager = new DatadockManager(datadockPool, harvester, flowMap);
}

From source file:com.revetkn.ios.analyzer.ArtworkAnalyzer.java

/**
 * @return Creates a backing thread pool used for concurrent execution of image processing tasks.
 *//*  w w  w.  ja  v  a 2  s.c om*/
protected ExecutorService createExecutorService() {
    int coreThreadCount = getRuntime().availableProcessors();
    int maximumThreadCount = coreThreadCount;
    int unusedThreadTerminationTimeoutInSeconds = 5;

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(coreThreadCount, maximumThreadCount,
            unusedThreadTerminationTimeoutInSeconds, SECONDS, new LinkedBlockingQueue<Runnable>());

    threadPoolExecutor.allowCoreThreadTimeOut(true);

    return threadPoolExecutor;
}

From source file:org.bimserver.GeometryGenerator.java

@SuppressWarnings("unchecked")
public GenerateGeometryResult generateGeometry(long uoid, final PluginManager pluginManager,
        final DatabaseSession databaseSession, final IfcModelInterface model, final int pid, final int rid,
        final boolean store, GeometryCache geometryCache)
        throws BimserverDatabaseException, GeometryGeneratingException {
    GenerateGeometryResult generateGeometryResult = new GenerateGeometryResult();
    packageMetaData = model.getPackageMetaData();
    productClass = packageMetaData.getEClass("IfcProduct");
    productRepresentationClass = packageMetaData.getEClass("IfcProductRepresentation");
    geometryFeature = productClass.getEStructuralFeature("geometry");
    representationFeature = productClass.getEStructuralFeature("Representation");
    representationsFeature = productRepresentationClass.getEStructuralFeature("Representations");

    if (geometryCache != null && !geometryCache.isEmpty()) {
        returnCachedData(model, geometryCache, databaseSession, pid, rid);
        return null;
    }//from  ww w. ja  va2s . com
    long start = System.nanoTime();
    String pluginName = "";
    if (model.getPackageMetaData().getSchema() == Schema.IFC4) {
        pluginName = "org.bimserver.ifc.step.serializer.Ifc4StepSerializerPlugin";
    } else if (model.getPackageMetaData().getSchema() == Schema.IFC2X3TC1) {
        pluginName = "org.bimserver.ifc.step.serializer.Ifc2x3tc1StepSerializerPlugin";
    }

    try {
        final SerializerPlugin ifcSerializerPlugin = (SerializerPlugin) pluginManager.getPlugin(pluginName,
                true);
        if (ifcSerializerPlugin == null) {
            throw new UserException("No IFC serializer found");
        }

        User user = (User) databaseSession.get(uoid, OldQuery.getDefault());
        UserSettings userSettings = user.getUserSettings();
        RenderEnginePluginConfiguration defaultRenderEngine = userSettings.getDefaultRenderEngine();
        if (defaultRenderEngine == null) {
            throw new UserException("No default render engine has been selected for this user");
        }
        final RenderEnginePlugin renderEnginePlugin = pluginManager
                .getRenderEngine(defaultRenderEngine.getPluginDescriptor().getPluginClassName(), true);
        if (renderEnginePlugin == null) {
            throw new UserException("No (enabled) render engine found of type "
                    + defaultRenderEngine.getPluginDescriptor().getPluginClassName());
        }

        int maxSimultanousThreads = Math.min(
                bimServer.getServerSettingsCache().getServerSettings().getRenderEngineProcesses(),
                Runtime.getRuntime().availableProcessors());
        if (maxSimultanousThreads < 1) {
            maxSimultanousThreads = 1;
        }

        final RenderEngineSettings settings = new RenderEngineSettings();
        settings.setPrecision(Precision.SINGLE);
        settings.setIndexFormat(IndexFormat.AUTO_DETECT);
        settings.setGenerateNormals(true);
        settings.setGenerateTriangles(true);
        settings.setGenerateWireFrame(false);

        final RenderEngineFilter renderEngineFilter = new RenderEngineFilter();

        if (maxSimultanousThreads == 1) {
            Runner runner = new Runner(null, renderEnginePlugin, databaseSession, settings, store, model,
                    ifcSerializerPlugin, model, pid, rid, null, renderEngineFilter, generateGeometryResult);
            runner.run();
        } else {
            Set<EClass> classes = new HashSet<>();
            for (IdEObject object : model.getAllWithSubTypes(packageMetaData.getEClass("IfcProduct"))) {
                IdEObject representation = (IdEObject) object.eGet(representationFeature);
                if (representation != null
                        && ((List<?>) representation.eGet(representationsFeature)).size() > 0) {
                    classes.add(object.eClass());
                }
            }

            if (classes.size() == 0) {
                return null;
            }

            classes.remove(packageMetaData.getEClass("IfcAnnotation"));
            classes.remove(packageMetaData.getEClass("IfcOpeningElement"));

            LOGGER.debug("Using " + maxSimultanousThreads + " processes for geometry generation");
            ThreadPoolExecutor executor = new ThreadPoolExecutor(maxSimultanousThreads, maxSimultanousThreads,
                    24, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(classes.size()));

            final Map<IdEObject, IdEObject> bigMap = new HashMap<IdEObject, IdEObject>();

            HideAllInversesObjectIDM idm = new HideAllInversesObjectIDM(
                    CollectionUtils.singleSet(packageMetaData.getEPackage()),
                    pluginManager.getMetaDataManager().getPackageMetaData("ifc2x3tc1").getSchemaDefinition());
            OidProvider oidProvider = new OidProvider() {
                @Override
                public long newOid(EClass eClass) {
                    return databaseSession.newOid(eClass);
                }
            };
            for (final EClass eClass : classes) {
                final BasicIfcModel targetModel = new BasicIfcModel(
                        pluginManager.getMetaDataManager().getPackageMetaData("ifc2x3tc1"), null);
                ModelHelper modelHelper = new ModelHelper(bimServer.getMetaDataManager(), targetModel);
                modelHelper.setOidProvider(oidProvider);
                modelHelper.setObjectIDM(idm);

                IdEObject newOwnerHistory = modelHelper.copyBasicObjects(model, bigMap);

                for (IdEObject idEObject : model.getAll(eClass)) {
                    IdEObject newObject = modelHelper.copy(idEObject, false,
                            ModelHelper.createObjectIdm(idEObject.eClass()));
                    modelHelper.copyDecomposes(idEObject, newOwnerHistory);
                    bigMap.put(newObject, idEObject);
                    if (eClass.getName().equals("IfcWallStandardCase")) {
                        EStructuralFeature hasOpeningsFeature = idEObject.eClass()
                                .getEStructuralFeature("HasOpenings");
                        for (IdEObject ifcRelVoidsElement : ((List<IdEObject>) idEObject
                                .eGet(hasOpeningsFeature))) {
                            bigMap.put(modelHelper.copy(ifcRelVoidsElement, false), ifcRelVoidsElement);
                            EStructuralFeature relatedOpeningElementFeature = ifcRelVoidsElement.eClass()
                                    .getEStructuralFeature("RelatedOpeningElement");
                            IdEObject relatedOpeningElement = (IdEObject) ifcRelVoidsElement
                                    .eGet(relatedOpeningElementFeature);
                            if (relatedOpeningElement != null) {
                                bigMap.put(modelHelper.copy(relatedOpeningElement, false),
                                        relatedOpeningElement);
                            }
                        }
                    }
                }

                executor.submit(new Runner(eClass, renderEnginePlugin, databaseSession, settings, store,
                        targetModel, ifcSerializerPlugin, model, pid, rid, bigMap, renderEngineFilter,
                        generateGeometryResult));
            }
            executor.shutdown();
            executor.awaitTermination(24, TimeUnit.HOURS);
        }

        long end = System.nanoTime();
        LOGGER.info("Rendertime: " + ((end - start) / 1000000) + "ms, " + "Reused: "
                + Formatters.bytesToString(bytesSaved.get()) + ", Total: "
                + Formatters.bytesToString(totalBytes.get()) + ", Final: "
                + Formatters.bytesToString(totalBytes.get() - bytesSaved.get()));
    } catch (Exception e) {
        LOGGER.error("", e);
        throw new GeometryGeneratingException(e);
    }
    return generateGeometryResult;
}

From source file:org.alfresco.repo.transaction.RetryingTransactionHelperTest.java

@SuppressWarnings("unchecked")
private void runThreads(final RetryingTransactionHelper txnHelper, final List<Throwable> caughtExceptions,
        final Pair<Integer, Integer>... startDurationPairs) {
    ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(10));

    class Work implements Runnable {
        private final CountDownLatch startLatch;
        private final long endTime;

        public Work(CountDownLatch startLatch, long endTime) {
            this.startLatch = startLatch;
            this.endTime = endTime;
        }//w w w.  ja  v  a 2 s . c o m

        public void run() {
            try {
                txnHelper.doInTransaction(new RetryingTransactionCallback<Void>() {

                    public Void execute() throws Throwable {
                        // Signal that we've started
                        startLatch.countDown();

                        long duration = endTime - System.currentTimeMillis();
                        if (duration > 0) {
                            Thread.sleep(duration);
                        }
                        return null;
                    }
                });
            } catch (Throwable e) {
                caughtExceptions.add(e);
                // We never got a chance to signal we had started so do it now
                if (startLatch.getCount() > 0) {
                    startLatch.countDown();
                }
            }
        }
    }
    ;

    // Schedule the transactions at their required start times
    long startTime = System.currentTimeMillis();
    long currentStart = 0;
    for (Pair<Integer, Integer> pair : startDurationPairs) {
        int start = pair.getFirst();
        long now = System.currentTimeMillis();
        long then = startTime + start;
        if (then > now) {
            try {
                Thread.sleep(then - now);
            } catch (InterruptedException e) {
            }
            currentStart = start;
        }
        CountDownLatch startLatch = new CountDownLatch(1);
        Runnable work = new Work(startLatch, startTime + currentStart + pair.getSecond());
        executorService.execute(work);
        try {
            // Wait for the thread to get up and running. We need them starting in sequence
            startLatch.await(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
        }
    }
    // Wait for the threads to have finished
    executorService.shutdown();
    try {
        executorService.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    }

}

From source file:org.lilyproject.tools.import_.cli.JsonImport.java

private void startExecutor() {
    executor = new ThreadPoolExecutor(threadCount, threadCount, 10, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(250));
    executor.setRejectedExecutionHandler(new WaitPolicy());
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient.java

/**
 * <p>/*from  ww  w.  ja v  a2  s.  com*/
 * Creates a {@link VersionControlClient} that will use the given web
 * service proxy class and the given connection. The size of the download
 * worker thread pool and the thread idle timeouts can also be specified.
 * </p>
 * <p>
 * Generally you do not instantiate this class yourself. See
 * {@link TFSTeamProjectCollection#getClient(Class)}.
 * </p>
 *
 * @param connection
 *        the connection to use (must not be <code>null</code>)
 * @param webService
 *        the {@link _RepositorySoap} web service proxy to use (must not be
 *        <code>null</code>)
 * @param webServiceExtensions
 *        the {@link _RepositoryExtensionsSoap} proxy to use for TFS 2010
 *        features (may be null)
 * @param repository4
 *        the {@link _Repository4Soap} proxy to use for TFS 2012 features
 *        (may be null)
 * @param repository5
 *        the {@link _Repository5Soap} proxy to use for TFS 2012 QU1
 *        features (may be null)
 * @param maximumGetEngineWorkerThreads
 *        the maximum number of simultaneous worker threads started to
 *        process get operations. This controls the maximum number of
 *        threads that could be performing a file download in parallel,
 *        though these worker threads perform non-network work that may
 *        prevent the workers reaching maximum theoretical network
 *        parallelism. Must be > 0.
 * @param getEngineWorkerThreadIdleTimeoutSeconds
 *        the number of seconds of consecutive idle time after which a get
 *        engine worker thread stops running. Choose a number that provides
 *        for some thread re-use between calls that process get operations
 *        (so threads can be reused from previous operations). Choosing a
 *        very large number will result in idle threads hanging around long
 *        after their last get operation, possibly consuming resources when
 *        it is unlikely there will be any get-related work for them to
 *        perform in the near future. Must be >= 0.
 */
protected VersionControlClient(final TFSTeamProjectCollection connection, final _RepositorySoap webService,
        final _RepositoryExtensionsSoap webServiceExtensions, final _Repository4Soap repository4,
        final _Repository5Soap repository5, final int maximumGetEngineWorkerThreads,
        final int getEngineWorkerThreadIdleTimeoutSeconds) {
    /*
     * TODO Make these parameters available via some {@link
     * TFSTeamProjectCollection} configuration mechanism.
     */

    Check.notNull(connection, "connection"); //$NON-NLS-1$
    Check.isTrue(maximumGetEngineWorkerThreads > 0, "GetEngine worker threads must be > 0"); //$NON-NLS-1$
    Check.isTrue(getEngineWorkerThreadIdleTimeoutSeconds >= 0, "GetEngine worker timeout must be >= 0"); //$NON-NLS-1$
    Check.notNull(webService, "webService"); //$NON-NLS-1$

    this.connection = connection;
    this._webService = webService;
    this._webServiceExtensions = webServiceExtensions;
    this._repository4 = repository4;
    this._repository5 = repository5;

    /*
     * Look up the GUID of this VersionControlClient in the local workspace
     * cache. If it's not there, fetch it from the server.
     */
    final InternalServerInfo serverInfo = Workstation.getCurrent(getConnection().getPersistenceStoreProvider())
            .getCache().getServerInfoByURI(connection.getBaseURI());
    if (null == serverInfo) {
        serverGUID = connection.getInstanceID();
    } else {
        serverGUID = serverInfo.getServerGUID();
    }

    /**
     * The default get engine worker pool. The actual Executor we use should
     * be unbounded becausethe BoundedExecutor handles the submission
     * limiting.
     */
    threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, getEngineWorkerThreadIdleTimeoutSeconds,
            TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

    uploadDownloadWorkerExecutor = new BoundedExecutor(threadPoolExecutor, maximumGetEngineWorkerThreads);

    /*
     * Configure default property filters for the Unix execute bit.
     */
    if (Platform.isCurrentPlatform(Platform.GENERIC_UNIX)
            && getWebServiceLayer().getServiceLevel().getValue() >= WebServiceLevel.TFS_2012.getValue()) {
        setDefaultItemPropertyFilters(
                new String[] { PropertyConstants.EXECUTABLE_KEY, PropertyConstants.SYMBOLIC_KEY });
    }

    // Start listening for Workstation events
    Workstation.getCurrent(getConnection().getPersistenceStoreProvider()).addWorkstationEventListener(this);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestDelegationTokenRenewer.java

private DelegationTokenRenewer createNewDelegationTokenRenewer(Configuration conf,
        final AtomicInteger counter) {
    DelegationTokenRenewer renew = new DelegationTokenRenewer() {

        @Override//w  w  w .ja  v  a2s .  c o m
        protected ThreadPoolExecutor createNewThreadPoolService(Configuration conf) {
            ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 5, 3L, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>()) {

                @Override
                protected void afterExecute(Runnable r, Throwable t) {
                    counter.decrementAndGet();
                    super.afterExecute(r, t);
                }

                @Override
                public void execute(Runnable command) {
                    counter.incrementAndGet();
                    super.execute(command);
                }
            };
            return pool;
        }
    };
    renew.setRMContext(TestUtils.getMockRMContext());
    return renew;
}

From source file:net.pms.dlna.DLNAResource.java

/**
 * First thing it does it searches for an item matching the given objectID.
 * If children is false, then it returns the found object as the only object in the list.
 * TODO: (botijo) This function does a lot more than this!
 * @param objectId ID to search for.//w  ww . j  a v  a  2 s  .c o  m
 * @param returnChildren State if you want all the children in the returned list.
 * @param start
 * @param count
 * @param renderer Renderer for which to do the actions.
 * @return List of DLNAResource items.
 * @throws IOException
 */
public synchronized List<DLNAResource> getDLNAResources(String objectId, boolean returnChildren, int start,
        int count, RendererConfiguration renderer) throws IOException {
    ArrayList<DLNAResource> resources = new ArrayList<DLNAResource>();
    DLNAResource dlna = search(objectId, count, renderer);

    if (dlna != null) {
        String systemName = dlna.getSystemName();
        dlna.setDefaultRenderer(renderer);

        if (!returnChildren) {
            resources.add(dlna);
            dlna.refreshChildrenIfNeeded();
        } else {
            dlna.discoverWithRenderer(renderer, count, true);

            if (count == 0) {
                count = dlna.getChildren().size();
            }

            if (count > 0) {
                ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(count);

                int nParallelThreads = 3;
                if (dlna instanceof DVDISOFile) {
                    nParallelThreads = 1; // Some DVD drives die wih 3 parallel threads
                }

                ThreadPoolExecutor tpe = new ThreadPoolExecutor(Math.min(count, nParallelThreads), count, 20,
                        TimeUnit.SECONDS, queue);

                for (int i = start; i < start + count; i++) {
                    if (i < dlna.getChildren().size()) {
                        final DLNAResource child = dlna.getChildren().get(i);

                        if (child != null) {
                            tpe.execute(child);
                            resources.add(child);
                        } else {
                            logger.warn("null child at index {} in {}", i, systemName);
                        }
                    }
                }

                try {
                    tpe.shutdown();
                    tpe.awaitTermination(20, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    logger.error("error while shutting down thread pool executor for " + systemName, e);
                }

                logger.trace("End of analysis for {}", systemName);
            }
        }
    }

    return resources;
}