mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-28 13:01:05 +03:00
v2.0
SmartThreadPool v2.0
This commit is contained in:
+43
-134
@@ -42,144 +42,52 @@ namespace SmartThreadPoolTests
|
||||
/// Example of how to queue a work item and then wait on a timeout for the result.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Timeout()
|
||||
[ExpectedException(typeof(WorkItemTimeoutException))]
|
||||
public void Timeout()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
bool success = false;
|
||||
|
||||
IWorkItemResult wir =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
|
||||
try
|
||||
{
|
||||
wir.GetResult(500, true);
|
||||
}
|
||||
catch (WorkItemTimeoutException)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
try
|
||||
{
|
||||
wir.GetResult(500, true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
/// <summary>
|
||||
/// Example of how to interrupt the waiting for a work item to complete.
|
||||
/// </summary>
|
||||
[Test]
|
||||
[ExpectedException(typeof(WorkItemTimeoutException))]
|
||||
public void WorkItemWaitCanceling()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);
|
||||
|
||||
/// <summary>
|
||||
/// Example of how to queue a work item and then cancel it while it is in the queue.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void WorkItemCanceling()
|
||||
{
|
||||
// Create a SmartThreadPool with only one thread.
|
||||
// It just to show how to use the work item canceling feature
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 1);
|
||||
|
||||
bool success = false;
|
||||
|
||||
// Queue a work item that will occupy the thread in the pool
|
||||
IWorkItemResult wir1 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
|
||||
// Queue another work item that will wait for the first to complete
|
||||
IWorkItemResult wir2 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
|
||||
// Wait a while for the thread pool to start executing the first work item
|
||||
Thread.Sleep(100);
|
||||
|
||||
// The first work item cannot be canceled since it is currently executing
|
||||
if (!wir1.Cancel())
|
||||
{
|
||||
// Cancel the second work item while it still in the queue
|
||||
if (wir2.Cancel())
|
||||
{
|
||||
try
|
||||
{
|
||||
// Retreiving result of a canceled work item throws an exception
|
||||
wir2.GetResult();
|
||||
}
|
||||
catch (WorkItemCancelException)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example of how to interrupt the waiting for a work item to complete.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void WorkItemWaitCanceling()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);
|
||||
|
||||
bool success = false;
|
||||
|
||||
// Queue a work item that will occupy the thread in the pool
|
||||
IWorkItemResult wir1 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
|
||||
// Queue another work item that will wait for the first to complete
|
||||
IWorkItemResult wir2 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);
|
||||
|
||||
try
|
||||
{
|
||||
wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
|
||||
}
|
||||
catch (WorkItemTimeoutException)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example of how to queue a work item and then cancel it while it is in the queue.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void WorkItemCancelingAndInUseWorkerThreads()
|
||||
{
|
||||
// Create a SmartThreadPool with only one thread.
|
||||
// It just to show how to use the work item canceling feature
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 10);
|
||||
|
||||
IWorkItemResult [] wirs = new IWorkItemResult[100];
|
||||
for(int i = 0; i < 100; ++i)
|
||||
{
|
||||
wirs[i] = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
}
|
||||
|
||||
|
||||
// Wait a while for the thread pool to start executing the first work item
|
||||
Thread.Sleep(100);
|
||||
|
||||
for(int i = 0; i < 100; ++i)
|
||||
{
|
||||
wirs[i].Cancel();
|
||||
}
|
||||
|
||||
smartThreadPool.WaitForIdle(2000);
|
||||
|
||||
int inUseThreads = smartThreadPool.InUseThreads;
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
|
||||
Assert.AreEqual(0, inUseThreads);
|
||||
}
|
||||
// Queue a work item that will occupy the thread in the pool
|
||||
IWorkItemResult wir1 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
|
||||
|
||||
// Queue another work item that will wait for the first to complete
|
||||
IWorkItemResult wir2 =
|
||||
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);
|
||||
|
||||
try
|
||||
{
|
||||
wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
|
||||
}
|
||||
finally
|
||||
{
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private object DoSomeWork(object state)
|
||||
{
|
||||
@@ -187,12 +95,13 @@ namespace SmartThreadPoolTests
|
||||
return 1;
|
||||
}
|
||||
|
||||
private object SignalCancel(object state)
|
||||
{
|
||||
ManualResetEvent cancelWaitHandle = state as ManualResetEvent;
|
||||
Thread.Sleep(250);
|
||||
cancelWaitHandle.Set();
|
||||
return null;
|
||||
}
|
||||
private object SignalCancel(object state)
|
||||
{
|
||||
ManualResetEvent cancelWaitHandle = state as ManualResetEvent;
|
||||
Thread.Sleep(250);
|
||||
cancelWaitHandle.Set();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user