You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jg...@apache.org on 2011/03/16 21:31:56 UTC
svn commit: r1082291 - in
/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src:
main/csharp/ main/csharp/Threads/ test/csharp/Threads/
Author: jgomes
Date: Wed Mar 16 20:31:56 2011
New Revision: 1082291
URL: http://svn.apache.org/viewvc?rev=1082291&view=rev
Log:
Change the TaskRunnerFactory to create a DedicatedTaskRunner thread instead of a PooledTaskRunner. This allows the background threads to run simultaneously rather than being placed into a queue for synchronous execution.
Fixes [AMQNET-319]. (See https://issues.apache.org/activemq/browse/AMQNET-319)
Modified:
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/SessionExecutor.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DedicatedTaskRunner.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DefaultThreadPools.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/TaskRunnerFactory.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/test/csharp/Threads/DedicatedTaskRunnerTest.cs
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/SessionExecutor.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/SessionExecutor.cs?rev=1082291&r1=1082290&r2=1082291&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/SessionExecutor.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/SessionExecutor.cs Wed Mar 16 20:31:56 2011
@@ -81,7 +81,7 @@ namespace Apache.NMS.ActiveMQ
{
if(this.taskRunner == null)
{
- this.taskRunner = new DedicatedTaskRunner(this);
+ this.taskRunner = DefaultThreadPools.DefaultTaskRunnerFactory.CreateTaskRunner(this);
}
taskRunner = this.taskRunner;
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DedicatedTaskRunner.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DedicatedTaskRunner.cs?rev=1082291&r1=1082290&r2=1082291&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DedicatedTaskRunner.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DedicatedTaskRunner.cs Wed Mar 16 20:31:56 2011
@@ -32,17 +32,25 @@ namespace Apache.NMS.ActiveMQ.Threads
private bool terminated = false;
private bool pending = false;
private bool shutdown = false;
-
+
public DedicatedTaskRunner(Task task)
+ : this(task, "ActiveMQ Task", ThreadPriority.Normal)
+ {
+ }
+
+ public DedicatedTaskRunner(Task task, string taskName, ThreadPriority taskPriority)
{
if(task == null)
{
throw new NullReferenceException("Task was null");
}
-
+
this.task = task;
- this.theThread = new Thread(Run) {IsBackground = true};
+ this.theThread = new Thread(Run);
+ this.theThread.IsBackground = true;
+ this.theThread.Priority = taskPriority;
+ this.theThread.Name = taskName;
this.theThread.Start();
}
@@ -100,23 +108,22 @@ namespace Apache.NMS.ActiveMQ.Threads
{
return;
}
-
+
this.pending = true;
-
+
Monitor.PulseAll(this.mutex);
- }
+ }
}
internal void Run()
{
- try
+ try
{
- while(true)
+ while(true)
{
- lock(this.mutex)
+ lock(this.mutex)
{
pending = false;
-
if(this.shutdown)
{
return;
@@ -128,12 +135,12 @@ namespace Apache.NMS.ActiveMQ.Threads
// wait to be notified.
lock(this.mutex)
{
- if(this.shutdown)
+ if(this.shutdown)
{
return;
}
-
- while(!this.pending)
+
+ while(!this.pending)
{
Monitor.Wait(this.mutex);
}
@@ -141,7 +148,7 @@ namespace Apache.NMS.ActiveMQ.Threads
}
}
}
- catch( ThreadAbortException )
+ catch(ThreadAbortException)
{
// Prevent the ThreadAbortedException for propogating.
Thread.ResetAbort();
@@ -150,7 +157,7 @@ namespace Apache.NMS.ActiveMQ.Threads
{
}
finally
- {
+ {
// Make sure we notify any waiting threads that thread
// has terminated.
lock(this.mutex)
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DefaultThreadPools.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DefaultThreadPools.cs?rev=1082291&r1=1082290&r2=1082291&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DefaultThreadPools.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/DefaultThreadPools.cs Wed Mar 16 20:31:56 2011
@@ -19,7 +19,7 @@ namespace Apache.NMS.ActiveMQ.Threads
{
public class DefaultThreadPools
{
- private static readonly TaskRunnerFactory DEFAULT_TASK_RUNNER_FACTORY = new TaskRunnerFactory();
+ private static readonly TaskRunnerFactory taskRunnerFactory = new TaskRunnerFactory();
private DefaultThreadPools()
{
@@ -27,11 +27,7 @@ namespace Apache.NMS.ActiveMQ.Threads
public static TaskRunnerFactory DefaultTaskRunnerFactory
{
- get
- {
- return DEFAULT_TASK_RUNNER_FACTORY;
- }
+ get { return taskRunnerFactory; }
}
-
}
}
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/TaskRunnerFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/TaskRunnerFactory.cs?rev=1082291&r1=1082290&r2=1082291&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/TaskRunnerFactory.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/main/csharp/Threads/TaskRunnerFactory.cs Wed Mar 16 20:31:56 2011
@@ -26,47 +26,41 @@ namespace Apache.NMS.ActiveMQ.Threads
/// processing for them to become idle. The manager ensures that each task is
/// processes but that no one task overtakes the system. This is kina like
/// cooperative multitasking.
- /// </summary>
+ ///
+ /// If your OS/JVM combination has a good thread model, you may want to avoid
+ /// using a thread pool to run tasks and use a DedicatedTaskRunner instead.
+ /// </summary>
public class TaskRunnerFactory
{
- protected int maxIterationsPerRun;
- protected String name;
- protected ThreadPriority priority;
- protected bool daemon;
+ public string name = "ActiveMQ Task";
+ public ThreadPriority priority = ThreadPriority.Normal;
+ public int maxIterationsPerRun = 1000;
+ public bool dedicatedTaskRunner = true;
public TaskRunnerFactory()
{
- InitTaskRunnerFactory("ActiveMQ Task", ThreadPriority.Normal, true, 1000, false);
}
- public TaskRunnerFactory(String name, ThreadPriority priority, bool daemon, int maxIterationsPerRun)
- {
- InitTaskRunnerFactory(name, priority, daemon, maxIterationsPerRun, false);
- }
-
- public TaskRunnerFactory(String name, ThreadPriority priority, bool daemon, int maxIterationsPerRun, bool dedicatedTaskRunner)
- {
- InitTaskRunnerFactory(name, priority, daemon, maxIterationsPerRun, dedicatedTaskRunner);
- }
-
- public void InitTaskRunnerFactory(String name, ThreadPriority priority, bool daemon, int maxIterationsPerRun, bool dedicatedTaskRunner)
- {
- this.name = name;
- this.priority = priority;
- this.daemon = daemon;
- this.maxIterationsPerRun = maxIterationsPerRun;
-
- // If your OS/JVM combination has a good thread model, you may want to avoid
- // using a thread pool to run tasks and use a DedicatedTaskRunner instead.
- }
-
- public void Shutdown()
- {
- }
-
- public TaskRunner CreateTaskRunner(Task task, String name)
- {
- return new PooledTaskRunner(task, maxIterationsPerRun);
+ public TaskRunner CreateTaskRunner(Task task)
+ {
+ return CreateTaskRunner(task, this.name);
+ }
+
+ public TaskRunner CreateTaskRunner(Task task, string name)
+ {
+ return CreateTaskRunner(task, name, this.priority);
+ }
+
+ public TaskRunner CreateTaskRunner(Task task, string name, ThreadPriority taskPriority)
+ {
+ if(this.dedicatedTaskRunner)
+ {
+ return new DedicatedTaskRunner(task, name, taskPriority);
+ }
+ else
+ {
+ return new PooledTaskRunner(task, this.maxIterationsPerRun);
+ }
}
}
}
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/test/csharp/Threads/DedicatedTaskRunnerTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/test/csharp/Threads/DedicatedTaskRunnerTest.cs?rev=1082291&r1=1082290&r2=1082291&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/test/csharp/Threads/DedicatedTaskRunnerTest.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/branches/1.5.x/src/test/csharp/Threads/DedicatedTaskRunnerTest.cs Wed Mar 16 20:31:56 2011
@@ -18,6 +18,7 @@
using System.Threading;
using Apache.NMS.ActiveMQ.Threads;
using NUnit.Framework;
+using System;
namespace Apache.NMS.ActiveMQ.Test.Threads
{
@@ -25,28 +26,28 @@ namespace Apache.NMS.ActiveMQ.Test.Threa
public class DedicatedTaskRunnerTest
{
- class SimpleCountingTask : Task
- {
+ class SimpleCountingTask : Task
+ {
private uint count;
-
- public SimpleCountingTask()
+
+ public SimpleCountingTask()
{
this.count = 0;
}
-
- public bool Iterate()
- {
+
+ public bool Iterate()
+ {
count++;
return false;
}
-
+
public uint Count
- {
- get{ return count; }
+ {
+ get { return count; }
}
}
-
- class InfiniteCountingTask : Task
+
+ class InfiniteCountingTask : Task
{
private uint count;
@@ -55,52 +56,50 @@ namespace Apache.NMS.ActiveMQ.Test.Threa
this.count = 0;
}
- public bool Iterate()
- {
+ public bool Iterate()
+ {
count++;
return true;
}
public uint Count
- {
- get{ return count; }
+ {
+ get { return count; }
}
}
[Test]
- public void TestSimple()
+ public void TestSimple()
{
try
{
new DedicatedTaskRunner(null);
Assert.Fail("Should throw a NullReferenceException");
}
- catch
+ catch(NullReferenceException)
{
}
-
- SimpleCountingTask simpleTask = new SimpleCountingTask();
- Assert.IsTrue( simpleTask.Count == 0 );
+
+ SimpleCountingTask simpleTask = new SimpleCountingTask();
+ Assert.IsTrue(simpleTask.Count == 0);
DedicatedTaskRunner simpleTaskRunner = new DedicatedTaskRunner(simpleTask);
simpleTaskRunner.Wakeup();
- Thread.Sleep( 500 );
- Assert.IsTrue( simpleTask.Count >= 1 );
+ Thread.Sleep(500);
+ Assert.IsTrue(simpleTask.Count >= 1);
simpleTaskRunner.Wakeup();
- Thread.Sleep( 500 );
- Assert.IsTrue( simpleTask.Count >= 2 );
-
+ Thread.Sleep(500);
+ Assert.IsTrue(simpleTask.Count >= 2);
+
InfiniteCountingTask infiniteTask = new InfiniteCountingTask();
- Assert.IsTrue( infiniteTask.Count == 0 );
+ Assert.IsTrue(infiniteTask.Count == 0);
DedicatedTaskRunner infiniteTaskRunner = new DedicatedTaskRunner(infiniteTask);
- Thread.Sleep( 500 );
- Assert.IsTrue( infiniteTask.Count != 0 );
+ Thread.Sleep(500);
+ Assert.IsTrue(infiniteTask.Count != 0);
infiniteTaskRunner.Shutdown();
uint count = infiniteTask.Count;
- Thread.Sleep( 500 );
- Assert.IsTrue( infiniteTask.Count == count );
-
+ Thread.Sleep(500);
+ Assert.IsTrue(infiniteTask.Count == count);
}
-
}
}