Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to set thread creation/release handler (http://jira.jbo…
- Loading branch information
Showing
6 changed files
with
185 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jgroups.util; | ||
|
||
/** | ||
* An object that can alter the state of a thread when it receives a callback from a {@link ThreadManager} notifying | ||
* it that the thread has been created or released from use. | ||
* | ||
* @author Brian Stansberry | ||
* @version $Id: ThreadDecorator.java,v 1.1.2.1 2008/05/26 09:14:40 belaban Exp $ | ||
*/ | ||
public interface ThreadDecorator { | ||
/** | ||
* Notification that <code>thread</code> has just been created. | ||
* @param thread the thread | ||
*/ | ||
void threadCreated(Thread thread); | ||
|
||
/** | ||
* Notification that <code>thread</code> has just been released from use | ||
* (e.g. returned to a thread pool after executing a task). | ||
* @param thread the thread | ||
*/ | ||
void threadReleased(Thread thread); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.jgroups.util; | ||
|
||
/** | ||
* An object that manages threads and provides callbacks to a | ||
* {@link ThreadDecorator} to allow it to alter their state. | ||
* | ||
* @author Brian Stansberry | ||
* @version $Id: ThreadManager.java,v 1.1.2.1 2008/05/26 09:14:40 belaban Exp $ | ||
*/ | ||
public interface ThreadManager { | ||
/** | ||
* Gets the ThreadDecorator associated with this manager. | ||
* @return the ThreadDecorator, or <code>null</code> if there is none. | ||
*/ | ||
ThreadDecorator getThreadDecorator(); | ||
|
||
/** | ||
* Sets the ThreadDecorator associated this manager should use. | ||
* @param decorator the ThreadDecorator, or <code>null</code>. | ||
*/ | ||
void setThreadDecorator(ThreadDecorator decorator); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jgroups.util; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* ThreadPoolExecutor subclass that implements @{link ThreadManager}. | ||
* @author Brian Stansberry | ||
* @version $Id: ThreadManagerThreadPoolExecutor.java,v 1.1.2.1 2008/05/26 09:14:39 belaban Exp $ | ||
*/ | ||
public class ThreadManagerThreadPoolExecutor extends ThreadPoolExecutor implements ThreadManager { | ||
private ThreadDecorator decorator; | ||
|
||
public ThreadManagerThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); | ||
} | ||
|
||
public ThreadManagerThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); | ||
} | ||
|
||
public ThreadManagerThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); | ||
} | ||
|
||
public ThreadManagerThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); | ||
} | ||
|
||
public ThreadDecorator getThreadDecorator() { | ||
return decorator; | ||
} | ||
|
||
public void setThreadDecorator(ThreadDecorator decorator) { | ||
this.decorator=decorator; | ||
} | ||
|
||
/** | ||
* Invokes {@link ThreadDecorator#threadReleased(Thread)} on the current thread. | ||
* <p/> | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected void afterExecute(Runnable r, Throwable t) { | ||
try { | ||
super.afterExecute(r, t); | ||
} | ||
finally { | ||
if(decorator != null) | ||
decorator.threadReleased(Thread.currentThread()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters