mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 09:01:19 +03:00
Added InUseThreads to IWorkItemsGroup
Also added a UnitTest for it. Thanks to TheCutter.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
@@ -13,9 +14,6 @@ namespace WorkItemsGroupTests
|
|||||||
[Category("TestWIGConcurrencyChanges")]
|
[Category("TestWIGConcurrencyChanges")]
|
||||||
public class TestWIGConcurrencyChanges
|
public class TestWIGConcurrencyChanges
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Example of waiting for idle
|
|
||||||
/// </summary>
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestWIGConcurrencyChange1WIG()
|
public void TestWIGConcurrencyChange1WIG()
|
||||||
{
|
{
|
||||||
@@ -57,9 +55,6 @@ namespace WorkItemsGroupTests
|
|||||||
smartThreadPool.Shutdown();
|
smartThreadPool.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Example of waiting for idle
|
|
||||||
/// </summary>
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestWIGConcurrencyChange2WIGs()
|
public void TestWIGConcurrencyChange2WIGs()
|
||||||
{
|
{
|
||||||
@@ -141,67 +136,57 @@ namespace WorkItemsGroupTests
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
/// <summary>
|
|
||||||
/// Example of waiting for idle
|
|
||||||
/// </summary>
|
|
||||||
[Test]
|
[Test]
|
||||||
public void WaitForIdle()
|
public void TestWIGConcurrencyChange()
|
||||||
{
|
{
|
||||||
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
|
SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0);
|
||||||
|
|
||||||
|
IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads);
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
for(int i = 0; i < 100; ++i)
|
for (int i = 0; i < 100; ++i)
|
||||||
{
|
{
|
||||||
smartThreadPool.QueueWorkItem(
|
wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null);
|
||||||
new WorkItemCallback(this.DoSomeWork),
|
|
||||||
null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
success = !smartThreadPool.WaitForIdle(3500);
|
wig.Concurrency = 1;
|
||||||
success = success && smartThreadPool.WaitForIdle(1000);
|
success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000);
|
||||||
|
Assert.IsTrue(success);
|
||||||
|
|
||||||
|
wig.Concurrency = 5;
|
||||||
|
success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000);
|
||||||
|
Assert.IsTrue(success);
|
||||||
|
|
||||||
|
wig.Concurrency = 25;
|
||||||
|
success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000);
|
||||||
|
Assert.IsTrue(success);
|
||||||
|
|
||||||
|
wig.Concurrency = 10;
|
||||||
|
success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000);
|
||||||
|
Assert.IsTrue(success);
|
||||||
|
|
||||||
smartThreadPool.Shutdown();
|
smartThreadPool.Shutdown();
|
||||||
|
|
||||||
Assert.IsTrue(success);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void WaitForIdleOnWrongThread()
|
|
||||||
{
|
|
||||||
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
|
|
||||||
|
|
||||||
IWorkItemResult wir = smartThreadPool.QueueWorkItem(
|
|
||||||
new WorkItemCallback(this.DoWaitForIdle),
|
|
||||||
smartThreadPool);
|
|
||||||
|
|
||||||
Exception e;
|
|
||||||
wir.GetResult(out e);
|
|
||||||
|
|
||||||
smartThreadPool.Shutdown();
|
|
||||||
|
|
||||||
Assert.IsTrue(e is NotSupportedException);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private int x = 0;
|
|
||||||
private object DoSomeWork(object state)
|
|
||||||
{
|
|
||||||
Debug.WriteLine(Interlocked.Increment(ref x));
|
|
||||||
Thread.Sleep(1000);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private object DoWaitForIdle(object state)
|
|
||||||
{
|
private bool WaitForWIGThreadsInUse(IWorkItemsGroup wig, int maxThreadsCount, int timeout)
|
||||||
SmartThreadPool smartThreadPool = state as SmartThreadPool;
|
{
|
||||||
smartThreadPool.WaitForIdle();
|
DateTime end = DateTime.Now + new TimeSpan(0, 0, 0, 0, timeout);
|
||||||
return null;
|
|
||||||
}
|
bool success = false;
|
||||||
*/
|
while (DateTime.Now <= end && !success)
|
||||||
}
|
{
|
||||||
|
success = (wig.InUseThreads == maxThreadsCount);
|
||||||
|
Thread.Sleep(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object DoSomeLongWork(object state)
|
||||||
|
{
|
||||||
|
Thread.Sleep(250);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,6 +84,11 @@ namespace Amib.Threading
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
int WaitingCallbacks { get; }
|
int WaitingCallbacks { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the number of currently executing work items
|
||||||
|
/// </summary>
|
||||||
|
int InUseThreads { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get an array with all the state objects of the currently running items.
|
/// Get an array with all the state objects of the currently running items.
|
||||||
/// The array represents a snap shot and impact performance.
|
/// The array represents a snap shot and impact performance.
|
||||||
|
|||||||
@@ -1395,18 +1395,6 @@ namespace Amib.Threading
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the number of busy (not idle) threads in the thread pool.
|
|
||||||
/// </summary>
|
|
||||||
public int InUseThreads
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
ValidateNotDisposed();
|
|
||||||
return _inUseWorkerThreads;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if the current running work item has been cancelled.
|
/// Returns true if the current running work item has been cancelled.
|
||||||
/// Must be used within the work item's callback method.
|
/// Must be used within the work item's callback method.
|
||||||
@@ -1508,6 +1496,18 @@ namespace Amib.Threading
|
|||||||
set { MaxThreads = value; }
|
set { MaxThreads = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the number of busy (not idle) threads in the thread pool.
|
||||||
|
/// </summary>
|
||||||
|
public override int InUseThreads
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
ValidateNotDisposed();
|
||||||
|
return _inUseWorkerThreads;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the number of work items in the queue.
|
/// Get the number of work items in the queue.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -126,6 +126,14 @@ namespace Amib.Threading.Internal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int InUseThreads
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _workItemsExecutingInStp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override int WaitingCallbacks
|
public override int WaitingCallbacks
|
||||||
{
|
{
|
||||||
get { return _workItemsQueue.Count; }
|
get { return _workItemsQueue.Count; }
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ namespace Amib.Threading.Internal
|
|||||||
|
|
||||||
public abstract int Concurrency { get; set; }
|
public abstract int Concurrency { get; set; }
|
||||||
public abstract int WaitingCallbacks { get; }
|
public abstract int WaitingCallbacks { get; }
|
||||||
|
public abstract int InUseThreads { get; }
|
||||||
|
|
||||||
public abstract object[] GetStates();
|
public abstract object[] GetStates();
|
||||||
public abstract WIGStartInfo WIGStartInfo { get; }
|
public abstract WIGStartInfo WIGStartInfo { get; }
|
||||||
public abstract void Start();
|
public abstract void Start();
|
||||||
|
|||||||
Reference in New Issue
Block a user