You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/07/18 16:36:54 UTC
svn commit: r1362967 - in /sling/trunk/bundles/commons/threads: ./
src/main/java/org/apache/sling/commons/threads/
src/main/java/org/apache/sling/commons/threads/impl/
Author: cziegeler
Date: Wed Jul 18 14:36:54 2012
New Revision: 1362967
URL: http://svn.apache.org/viewvc?rev=1362967&view=rev
Log:
SLING-2540 : Allow the ThreadPool to process Callable/Runnable and return a Future. Apply patch from Timothee Maret
Modified:
sling/trunk/bundles/commons/threads/pom.xml
sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
Modified: sling/trunk/bundles/commons/threads/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/pom.xml?rev=1362967&r1=1362966&r2=1362967&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/pom.xml (original)
+++ sling/trunk/bundles/commons/threads/pom.xml Wed Jul 18 14:36:54 2012
@@ -58,7 +58,7 @@
org.apache.sling.commons.threads.impl.Activator
</Bundle-Activator>
<Export-Package>
- org.apache.sling.commons.threads;version=3.1.0
+ org.apache.sling.commons.threads;version=3.2.0
</Export-Package>
<Private-Package>
org.apache.sling.commons.threads.impl
Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java?rev=1362967&r1=1362966&r2=1362967&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java Wed Jul 18 14:36:54 2012
@@ -16,6 +16,9 @@
*/
package org.apache.sling.commons.threads;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
/**
* The thread pool interface allows to start runnables by
* getting threads from a managed pool.
@@ -29,6 +32,22 @@ public interface ThreadPool {
void execute(Runnable runnable);
/**
+ * Submits a callable for execution
+ * @param callable The {@link Callable} to submit
+ * @return A {@link Future} representing pending completion of the {@link Callable}
+ * @since 3.2
+ */
+ <T> Future<T> submit(Callable<T> callable);
+
+ /**
+ * Submits a runnable for execution
+ * @param runnable The {@link Runnable} to submit
+ * @return A {@link Future} representing pending completion of the {@link Runnable}
+ * @since 3.2
+ */
+ Future<?> submit(Runnable runnable);
+
+ /**
* The name of the thread pool.
*/
String getName();
Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java?rev=1362967&r1=1362966&r2=1362967&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java Wed Jul 18 14:36:54 2012
@@ -17,7 +17,9 @@
package org.apache.sling.commons.threads.impl;
import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
@@ -152,23 +154,36 @@ public class DefaultThreadPool
/**
* @see org.apache.sling.commons.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.");
- }
+ public void execute(final Runnable runnable) {
+ checkExecutor();
if ( runnable != null ) {
- if ( this.logger.isDebugEnabled() ) {
- this.logger.debug("Executing runnable: {}, pool={}, active={}, corePoolSize={}, maxPoolSize={}, queueSize={}",
- new Object[] {runnable,
- this.name,
- this.executor.getActiveCount(),
- this.executor.getCorePoolSize(),
- this.executor.getMaximumPoolSize(),
- this.executor.getQueue().size()});
+ if ( logger.isDebugEnabled() ) {
+ logOperation("Executing runnable: ", runnable);
}
+ executor.execute(runnable);
+ }
+ }
- this.executor.execute(runnable);
+ /**
+ * @see org.apache.sling.commons.threads.ThreadPool#submit(java.util.concurrent.Callable)
+ */
+ public <T> Future<T> submit(final Callable<T> callable) {
+ checkExecutor();
+ if ( logger.isDebugEnabled() ) {
+ logOperation("Submitting callable: ", callable);
}
+ return executor.submit(callable);
+ }
+
+ /**
+ * @see org.apache.sling.commons.threads.ThreadPool#submit(java.lang.Runnable)
+ */
+ public Future<?> submit(final Runnable runnable) {
+ checkExecutor();
+ if ( logger.isDebugEnabled() ) {
+ logOperation("Submitting runnable: ", runnable);
+ }
+ return executor.submit(runnable);
}
/**
@@ -203,4 +218,19 @@ public class DefaultThreadPool
public ThreadPoolExecutor getExecutor() {
return this.executor;
}
+
+ private void checkExecutor() {
+ if ( this.executor == null ) {
+ throw new IllegalStateException("Thread pool " + this.name + " is already shutdown.");
+ }
+ }
+
+ private void logOperation(final String msg, final Object obj) {
+ logger.debug("{} {}, pool={}, active={}, corePoolSize={}, maxPoolSize={}, queueSize={}",
+ new Object[] {msg, obj, name,
+ executor.getActiveCount(),
+ executor.getCorePoolSize(),
+ executor.getMaximumPoolSize(),
+ executor.getQueue().size()});
+ }
}
Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java?rev=1362967&r1=1362966&r2=1362967&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java Wed Jul 18 14:36:54 2012
@@ -16,6 +16,8 @@
*/
package org.apache.sling.commons.threads.impl;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.sling.commons.threads.ThreadPool;
@@ -43,11 +45,25 @@ public final class ThreadPoolFacade impl
/**
* @see org.apache.sling.commons.threads.ThreadPool#execute(java.lang.Runnable)
*/
- public void execute(Runnable runnable) {
+ public void execute(final Runnable runnable) {
this.delegatee.execute(runnable);
}
/**
+ * @see org.apache.sling.commons.threads.ThreadPool#submit(java.util.concurrent.Callable)
+ */
+ public <T> Future<T> submit(final Callable<T> callable) {
+ return this.delegatee.submit(callable);
+ }
+
+ /**
+ * @see org.apache.sling.commons.threads.ThreadPool#submit(java.lang.Runnable)
+ */
+ public Future<?> submit(final Runnable runnable) {
+ return this.delegatee.submit(runnable);
+ }
+
+ /**
* @see org.apache.sling.commons.threads.ThreadPool#getConfiguration()
*/
public ThreadPoolConfig getConfiguration() {