Skip to content

Commit

Permalink
Introduced thread factory interface for customizability.
Browse files Browse the repository at this point in the history
  • Loading branch information
magwo committed Aug 22, 2011
1 parent a585125 commit 80915dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions ExecutorsDotNet.csproj
Expand Up @@ -46,6 +46,7 @@
<Compile Include="test\CommonTester.cs" />
<Compile Include="test\ImmediateTester.cs" />
<Compile Include="test\SingleThreadTester.cs" />
<Compile Include="src\IThreadFactory.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions README
Expand Up @@ -6,4 +6,6 @@ execution can be dynamic and flexible even in run-time.
I don't suppose this framework is immensely useful, I guess because of its small scope,
but I enjoy making and improving it.

Related: http://blogs.msdn.com/b/pfxteam/archive/2010/04/09/9990424.aspx

/Magnus
18 changes: 18 additions & 0 deletions src/IThreadFactory.cs
@@ -0,0 +1,18 @@
using System;
using System.Threading;
namespace Executors
{
public interface IThreadFactory
{
Thread CreateOrGetThread(ThreadStart threadStart);
}


public class SimpleThreadFactory : IThreadFactory {
public Thread CreateOrGetThread (ThreadStart threadStart)
{
return new Thread(threadStart);
}
}
}

13 changes: 9 additions & 4 deletions src/SingleThreadExecutor.cs
Expand Up @@ -22,15 +22,20 @@ class SingleThreadExecutor : IExecutor
volatile bool shutdownCompleted = false;


public SingleThreadExecutor() : this(ShutdownMode.FinishAll)
public SingleThreadExecutor() : this(ShutdownMode.FinishAll, new SimpleThreadFactory())
{
}

public SingleThreadExecutor(ShutdownMode shutdownMode)

public SingleThreadExecutor(ShutdownMode shutdownMode) : this(shutdownMode, new SimpleThreadFactory())
{
}


public SingleThreadExecutor(ShutdownMode shutdownMode, IThreadFactory threadFactory)
{
this.shutdownMode = shutdownMode;
ThreadStart start = new ThreadStart(RunWorker);
workerThread = new Thread(start);
workerThread = threadFactory.CreateOrGetThread(start);
workerThread.Start();
}

Expand Down

0 comments on commit 80915dd

Please sign in to comment.