-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3e6bb9
commit 7d2a5ca
Showing
3 changed files
with
104 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace eXtensionSharp.Job; | ||
|
||
public class JobHandler<T> | ||
{ | ||
private static Lazy<JobHandler<T>> _instance = new Lazy<JobHandler<T>>(() => new JobHandler<T>()); | ||
public static JobHandler<T> Instance => _instance.Value; | ||
|
||
private readonly ConcurrentQueue<T> _queue; | ||
private JobHandler() | ||
{ | ||
_queue = new ConcurrentQueue<T>(); | ||
} | ||
|
||
public void Enqueue(T item) => _queue.Enqueue(item); | ||
public T Dequeue() => _queue.TryDequeue(out var result) ? result : default; | ||
} | ||
|
||
public class JobProsessor<T> | ||
{ | ||
private static Lazy<JobProsessor<T>> _instance = new Lazy<JobProsessor<T>>(() => new JobProsessor<T>()); | ||
public static JobProsessor<T> Instance => _instance.Value; | ||
|
||
|
||
private ConcurrentDictionary<JobHandler<T>, Action<T>> _concurrentDictionary; | ||
|
||
private Thread _thread; | ||
private JobProsessor() | ||
{ | ||
_concurrentDictionary = new(); | ||
_thread = new Thread(StartProess); | ||
_thread.IsBackground = true; | ||
_thread.Start(); | ||
} | ||
|
||
public void SetProessor(JobHandler<T> jobHandler, Action<T> callback) | ||
{ | ||
_concurrentDictionary.TryAdd(jobHandler, callback); | ||
} | ||
|
||
public void StartProess() | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
if (_concurrentDictionary.TryGetValue(out var hander)) | ||
Check failure on line 48 in src/Job/JobHandler.cs GitHub Actions / build
Check failure on line 48 in src/Job/JobHandler.cs GitHub Actions / build
Check failure on line 48 in src/Job/JobHandler.cs GitHub Actions / build
|
||
{ | ||
var item = hander.Dequeue(); | ||
if (item.xAs<int>() > 0) | ||
{ | ||
Console.WriteLine(item.xAs<int>()); | ||
} | ||
} | ||
} | ||
catch (InvalidOperationException e) | ||
{ | ||
Console.WriteLine(e); | ||
break; | ||
} | ||
} | ||
} | ||
} |
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