Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 314C4200D41 for ; Tue, 7 Nov 2017 10:24:27 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 2FD61160C02; Tue, 7 Nov 2017 09:24:27 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CD324160C01 for ; Tue, 7 Nov 2017 10:24:25 +0100 (CET) Received: (qmail 98344 invoked by uid 500); 7 Nov 2017 09:24:24 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 98093 invoked by uid 99); 7 Nov 2017 09:24:24 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Nov 2017 09:24:24 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 0D6B087594; Tue, 7 Nov 2017 09:24:24 +0000 (UTC) Date: Tue, 07 Nov 2017 09:24:24 +0000 To: "commits@sling.apache.org" Subject: [sling-org-apache-sling-commons-threads] 01/33: Add new threads module for providing thread pools. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: rombert@apache.org In-Reply-To: <151004666355.20119.1780283780382689543@gitbox.apache.org> References: <151004666355.20119.1780283780382689543@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: sling-org-apache-sling-commons-threads X-Git-Refname: refs/tags/org.apache.sling.commons.threads-2.0.2-incubator X-Git-Reftype: annotated tag X-Git-Rev: cdf45818eca719b0bbc76145a4d5909aa0cda42a X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20171107092424.0D6B087594@gitbox.apache.org> archived-at: Tue, 07 Nov 2017 09:24:27 -0000 This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.threads-2.0.2-incubator in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-threads.git commit cdf45818eca719b0bbc76145a4d5909aa0cda42a Author: Carsten Ziegeler AuthorDate: Mon Feb 11 11:03:12 2008 +0000 Add new threads module for providing thread pools. git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads@620457 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 85 ++++++++ .../java/org/apache/sling/threads/ThreadPool.java | 42 ++++ .../apache/sling/threads/ThreadPoolManager.java | 40 ++++ .../sling/threads/impl/DefaultThreadFactory.java | 79 +++++++ .../sling/threads/impl/DefaultThreadPool.java | 239 +++++++++++++++++++++ .../threads/impl/DefaultThreadPoolManager.java | 150 +++++++++++++ .../sling/threads/impl/ExtendedThreadFactory.java | 67 ++++++ 7 files changed, 702 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5027142 --- /dev/null +++ b/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + org.apache.sling + sling + 1-incubator-SNAPSHOT + ../../parent/pom.xml + + + org.apache.sling.threads + bundle + 2.0.0-incubator-SNAPSHOT + + Sling - Thread Support + + Support for thread handling like pooling. + + + + + scm:svn:http://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads + + + scm:svn:https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads + + + http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads + + + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + + org.apache.sling.threads + + + org.apache.sling.threads.impl + + + + + + + + + org.apache.felix + org.osgi.compendium + + + org.slf4j + slf4j-api + + + diff --git a/src/main/java/org/apache/sling/threads/ThreadPool.java b/src/main/java/org/apache/sling/threads/ThreadPool.java new file mode 100644 index 0000000..f10527e --- /dev/null +++ b/src/main/java/org/apache/sling/threads/ThreadPool.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads; + +/** + * The ThreadPool interface allows to start runnables by + * getting threads from a managed pool. + * + * @version $Id$ + */ +public interface ThreadPool { + + /** + * Execute a runnable + * @param runnable The {@link Runnable} to execute + */ + void execute(Runnable runnable); + + /** + * The name of the thread pool. + */ + String getName(); + + /** + * Shut down the thread pool. + */ + void shutdown(); +} diff --git a/src/main/java/org/apache/sling/threads/ThreadPoolManager.java b/src/main/java/org/apache/sling/threads/ThreadPoolManager.java new file mode 100644 index 0000000..603aa46 --- /dev/null +++ b/src/main/java/org/apache/sling/threads/ThreadPoolManager.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads; + +/** + * The ThreadPoolManager manages thread pools. + * + * @version $Id$ + */ +public interface ThreadPoolManager { + + /** + * Add a new pool. + * If a pool with the same name already exists, the new pool is not added + * and false is returned. + * @param pool The pool + * @return True if the pool could be added, false otherwise. + */ + boolean add(ThreadPool pool); + + /** + * Get a thread pool + * @param name The name of the thread pool or null for the default pool. + */ + ThreadPool get(String name); +} diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java new file mode 100644 index 0000000..78a514e --- /dev/null +++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads.impl; + + +/** + * This class is responsible to create new Thread instances. + * It's a very basic implementation. + * + * @version $Id$ + */ +public class DefaultThreadFactory + implements ExtendedThreadFactory { + + /** The daemon mode */ + private boolean isDaemon = DefaultThreadPoolManager.DEFAULT_DAEMON_MODE; + + /** The priority of newly created Threads */ + private int priority = DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY; + + /** + * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setDaemon(boolean) + */ + public void setDaemon( boolean isDaemon ) { + this.isDaemon = isDaemon; + } + + /** + * @see org.apache.sling.threads.impl.ExtendedThreadFactory#isDaemon() + */ + public boolean isDaemon() { + return this.isDaemon; + } + + /** + * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setPriority(int) + */ + public void setPriority( final int priority ) { + if( ( Thread.MAX_PRIORITY == priority ) || + ( Thread.MIN_PRIORITY == priority ) || + ( Thread.NORM_PRIORITY == priority ) ) { + this.priority = priority; + } else { + throw new IllegalStateException("Unknown priority " + this.priority); + } + } + + /** + * @see org.apache.sling.threads.impl.ExtendedThreadFactory#getPriority() + */ + public int getPriority() { + return this.priority; + } + + /** + * @see org.apache.sling.threads.impl.ExtendedThreadFactory#newThread(java.lang.Runnable) + */ + public Thread newThread( final Runnable command ) { + final Thread thread = new Thread( command ); + thread.setPriority( this.priority ); + thread.setDaemon( this.isDaemon ); + + return thread; + } +} diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java new file mode 100644 index 0000000..c3fb849 --- /dev/null +++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads.impl; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.apache.sling.threads.ThreadPool; +import org.apache.sling.threads.ThreadPoolManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The DefaultThreadPool class implements the {@link ThreadPool} interface. + * Instances of this class are managed by the {@link ThreadPoolManager}. + * + * @version $Id$ + */ +public class DefaultThreadPool + implements ThreadPool { + + /** By default we use the logger for this class. */ + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** The name of this thread pool */ + protected final String name; + + /** The executor. */ + protected ThreadPoolExecutor executor; + + /** Should we wait for running jobs to terminate on shutdown ? */ + protected final boolean shutdownGraceful; + + /** How long to wait for running jobs to terminate on disposition */ + protected final int shutdownWaitTimeMs; + + /** + * Create a new thread pool. + * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME} + * is used + */ + public DefaultThreadPool(final String name) { + this(DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME, + DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE, + DefaultThreadPoolManager.DEFAULT_MAX_POOL_SIZE, + DefaultThreadPoolManager.DEFAULT_QUEUE_SIZE, + DefaultThreadPoolManager.DEFAULT_KEEP_ALIVE_TIME, + DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY, + DefaultThreadPoolManager.DEFAULT_SHUTDOWN_GRACEFUL, + DefaultThreadPoolManager.DEFAULT_SHUTDOWN_WAIT_TIME, + null, + DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY, + DefaultThreadPoolManager.DEFAULT_DAEMON_MODE); + } + + /** + * Create a new thread pool. + * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME} + * is used + */ + public DefaultThreadPool(final String name, + int minPoolSize, + int maxPoolSize, + final int queueSize, + long keepAliveTime, + String blockPolicy, + final boolean shutdownGraceful, + final int shutdownWaitTimeMs, + final ThreadFactory factory, + final int priority, + final boolean isDaemon) { + this.logger.info("ThreadPool [{}] initializing ...", name); + + // name + if ( name != null ) { + this.name = name; + } else { + this.name = DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME; + } + + // factory + final ThreadFactory threadFactory; + if (factory == null) { + logger.warn("No ThreadFactory is configured. Will use a " + + DefaultThreadFactory.class.getName()); + threadFactory = new DefaultThreadFactory(); + } else { + threadFactory = factory; + } + + // Min pool size + // make sure we have enough threads for the default thread pool as we + // need one for ourself + if (DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME.equals(name) + && ((minPoolSize > 0) && (minPoolSize < DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE))) { + minPoolSize = DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE; + } else if (minPoolSize < 1) { + minPoolSize = 1; + this.logger.warn("min-pool-size < 1 for pool \"" + name + "\". Set to 1"); + } + // Max pool size + maxPoolSize = (maxPoolSize < 0) ? Integer.MAX_VALUE : maxPoolSize; + + // Set priority and daemon if the factory is an extended factory + if ( threadFactory instanceof ExtendedThreadFactory ) { + final ExtendedThreadFactory extTF = (ExtendedThreadFactory)threadFactory; + extTF.setPriority(priority); + extTF.setDaemon(isDaemon); + } else { + if ( priority != Thread.NORM_PRIORITY ) { + this.logger.warn("ThreadFactory " + threadFactory + " does not support setting the priority or daemon setting."); + } + if ( isDaemon != DefaultThreadPoolManager.DEFAULT_DAEMON_MODE ) { + this.logger.warn("ThreadFactory " + threadFactory + " does not support setting the daemon mode."); + } + } + + // Keep alive time + if (keepAliveTime < 0) { + keepAliveTime = 1000; + this.logger.warn("keep-alive-time-ms < 0 for pool \"" + name + "\". Set to 1000"); + } + + // Queue + final BlockingQueue queue; + if (queueSize != 0) { + if (queueSize > 0) { + queue = new java.util.concurrent.ArrayBlockingQueue(queueSize); + } else { + queue = new LinkedBlockingQueue(); + } + } else { + queue = new SynchronousQueue(); + } + + if ( blockPolicy == null ) { + blockPolicy = DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY; + } + final RejectedExecutionHandler handler; + if (DefaultThreadPoolManager.POLICY_ABORT.equalsIgnoreCase(blockPolicy)) { + handler = new ThreadPoolExecutor.AbortPolicy(); + } else if (DefaultThreadPoolManager.POLICY_DISCARD.equalsIgnoreCase(blockPolicy)) { + handler = new ThreadPoolExecutor.AbortPolicy(); + } else if (DefaultThreadPoolManager.POLICY_DISCARD_OLDEST.equalsIgnoreCase(blockPolicy)) { + handler = new ThreadPoolExecutor.AbortPolicy(); + } else if (DefaultThreadPoolManager.POLICY_RUN.equalsIgnoreCase(blockPolicy)) { + handler = new ThreadPoolExecutor.AbortPolicy(); + } else { + final StringBuffer msg = new StringBuffer(); + msg.append("WARNING: Unknown block-policy configuration \"") + .append(blockPolicy); + msg.append("\". Should be one of \"").append(DefaultThreadPoolManager.POLICY_ABORT); + msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD); + msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD_OLDEST); + msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_RUN); + msg.append("\". Will use \"").append(DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY).append("\""); + logger.warn(msg.toString()); + handler = new ThreadPoolExecutor.CallerRunsPolicy(); + } + this.shutdownGraceful = shutdownGraceful; + this.shutdownWaitTimeMs = shutdownWaitTimeMs; + this.executor = new ThreadPoolExecutor(minPoolSize, + maxPoolSize, + keepAliveTime, + TimeUnit.MILLISECONDS, + queue, + threadFactory, + handler); + this.logger.info("ThreadPool [{}] initialized.", name); + } + + /** + * @see org.apache.sling.threads.ThreadPool#getName() + */ + public String getName() { + return name; + } + + /** + * @see org.apache.sling.threads.ThreadPool#execute(java.lang.Runnable) + */ + public void execute(Runnable runnable) { + if ( this.executor == null ) { + throw new IllegalStateException("Thread pool " + this.name + " is already shutdown."); + } + if ( runnable != null ) { + this.logger.debug("Executing runnable: {},pool={}", runnable, this.name); + + this.executor.execute(runnable); + } + } + + /** + * @see org.apache.sling.threads.ThreadPool#shutdown() + */ + public void shutdown() { + if ( this.executor != null ) { + if (shutdownGraceful) { + this.executor.shutdown(); + } else { + this.executor.shutdownNow(); + } + + try { + if (this.shutdownWaitTimeMs > 0) { + if (!this.executor.awaitTermination(this.shutdownWaitTimeMs, TimeUnit.MILLISECONDS)) { + logger.warn("running commands have not terminated within " + + this.shutdownWaitTimeMs + + "ms. Will shut them down by interruption"); + this.executor.shutdownNow(); + } + } + } catch (final InterruptedException ie) { + this.logger.error("Cannot shutdown ThreadPool", ie); + } + this.executor = null; + } + } +} diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java new file mode 100644 index 0000000..d122cd3 --- /dev/null +++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.threads.ThreadPool; +import org.apache.sling.threads.ThreadPoolManager; +import org.osgi.service.component.ComponentContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The DefaultThreadPoolManager implements the {@link ThreadPoolManager} interface + * and is responsible to manage {@link ThreadPool}s. + * + * @scr.component metatype="false" + * @scr.service interface="org.apache.sling.threads.ThreadPoolManager" + * + * @version $Id$ + */ +public class DefaultThreadPoolManager implements ThreadPoolManager { + + /** The default queue size */ + protected final static int DEFAULT_QUEUE_SIZE = -1; + + /** The default maximum pool size */ + protected final static int DEFAULT_MAX_POOL_SIZE = 5; + + /** The default minimum pool size */ + protected final static int DEFAULT_MIN_POOL_SIZE = 5; + + /** The default thread priority */ + protected final static int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY; + + /** The default daemon mode */ + protected final static boolean DEFAULT_DAEMON_MODE = false; + + /** The default keep alive time */ + protected final static long DEFAULT_KEEP_ALIVE_TIME = 60000L; + + /** The default way to shutdown gracefully */ + protected final static boolean DEFAULT_SHUTDOWN_GRACEFUL = false; + + /** The default shutdown waittime time */ + protected final static int DEFAULT_SHUTDOWN_WAIT_TIME = -1; + + /** The default shutdown waittime time */ + protected final static String DEFAULT_THREADPOOL_NAME = "default"; + + /** ThreadPool block policy ABORT */ + protected final static String POLICY_ABORT = "ABORT"; + + /** ThreadPool block policy DISCARD */ + protected final static String POLICY_DISCARD = "DISCARD"; + + /** ThreadPool block policy DISCARD-OLDEST */ + protected final static String POLICY_DISCARD_OLDEST = "DISCARDOLDEST"; + + /** ThreadPool block policy RUN */ + protected final static String POLICY_RUN = "RUN"; + + /** The default shutdown waittime time */ + protected final static String DEFAULT_BLOCK_POLICY = POLICY_RUN; + + /** By default we use the logger for this class. */ + protected Logger logger = LoggerFactory.getLogger(getClass()); + + /** The managed thread pools */ + protected final Map pools = new HashMap(); + + /** + * Activate this component. + */ + protected void activate(ComponentContext context) throws Exception { + this.logger.info("Starting thread pool manager."); + final ThreadPool defaultPool = new DefaultThreadPool( + DEFAULT_THREADPOOL_NAME, + DEFAULT_MIN_POOL_SIZE, + DEFAULT_MAX_POOL_SIZE, + DEFAULT_QUEUE_SIZE, + DEFAULT_KEEP_ALIVE_TIME, + DEFAULT_BLOCK_POLICY, + DEFAULT_SHUTDOWN_GRACEFUL, + DEFAULT_SHUTDOWN_WAIT_TIME, + null, + DEFAULT_THREAD_PRIORITY, + DEFAULT_DAEMON_MODE); + this.pools.put(defaultPool.getName(), defaultPool); + this.logger.info("Thread pool manager startet with default pool."); + } + + /** + * Deactivate this component. + */ + protected void deactivate(ComponentContext context) throws Exception { + this.logger.info("Stopping thread pool manager."); + this.logger.debug("Disposing all thread pools"); + + for (ThreadPool pool : this.pools.values()) { + this.logger.debug("Shutting down thread pool {}", pool.getName()); + + pool.shutdown(); + + this.logger.debug("Thread pool " + pool.getName() + " is shut down."); + } + this.pools.clear(); + this.logger.info("Thread pool manager stopped."); + } + + /** + * @see org.apache.sling.threads.ThreadPoolManager#add(org.apache.sling.threads.ThreadPool) + */ + public boolean add(ThreadPool pool) { + synchronized ( this.pools ) { + if (null != pools.get(pool.getName())) { + return false; + } + pools.put(pool.getName(), pool); + } + return true; + } + + /** + * @see org.apache.sling.threads.ThreadPoolManager#get(java.lang.String) + */ + public ThreadPool get(String name) { + if ( name == null ) { + name = DEFAULT_THREADPOOL_NAME; + } + synchronized (this.pools) { + return this.pools.get(name); + } + } +} diff --git a/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java b/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java new file mode 100644 index 0000000..5ad586f --- /dev/null +++ b/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.threads.impl; + +import java.util.concurrent.ThreadFactory; + +/** + * This is an extension to the {@link ThreadFactory}. + * + * @version $Id$ + */ +public interface ExtendedThreadFactory + extends ThreadFactory { + + /** + * Set the daemon mode of created Threads should have + * + * @param isDaemon Whether new {@link Thread}s should run as daemons. + */ + void setDaemon( boolean isDaemon ); + + /** + * Get the daemon mode created Threads will have + * + * @return Whether new {@link Thread}s should run as daemons. + */ + boolean isDaemon(); + + /** + * Set the priority newly created Threads should have + * + * @param priority One of {@link Thread#MIN_PRIORITY}, {@link + * Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY} + */ + void setPriority( int priority ); + + /** + * Get the priority newly created Threads will have + * + * @return One of {@link Thread#MIN_PRIORITY}, {@link + * Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY} + */ + int getPriority(); + + /** + * Create a new Thread for a {@link Runnable} command + * + * @param command The Runnable + * + * @return new Thread + */ + Thread newThread( Runnable command ); +} -- To stop receiving notification emails like this one, please contact "commits@sling.apache.org" .