Skip to content

Commit

Permalink
작업중
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Oct 8, 2024
1 parent d3e6bb9 commit 7d2a5ca
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
64 changes: 64 additions & 0 deletions src/Job/JobHandler.cs
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

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'value' of 'ConcurrentDictionary<JobHandler<T>, Action<T>>.TryGetValue(JobHandler<T>, out Action<T>)'

Check failure on line 48 in src/Job/JobHandler.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'value' of 'ConcurrentDictionary<JobHandler<T>, Action<T>>.TryGetValue(JobHandler<T>, out Action<T>)'

Check failure on line 48 in src/Job/JobHandler.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'value' of 'ConcurrentDictionary<JobHandler<T>, Action<T>>.TryGetValue(JobHandler<T>, out Action<T>)'

Check failure on line 48 in src/Job/JobHandler.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'value' of 'ConcurrentDictionary<JobHandler<T>, Action<T>>.TryGetValue(JobHandler<T>, out Action<T>)'
{
var item = hander.Dequeue();
if (item.xAs<int>() > 0)
{
Console.WriteLine(item.xAs<int>());
}
}
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
break;
}
}
}
}
13 changes: 11 additions & 2 deletions src/XForEachExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,20 @@ await Parallel.ForEachAsync(items, options, async (item, token) =>
return isCancel;
}

public static void xForEach<T1, T2, T3>(this T1[] item1, T2[] item2, T3[] item3, Action<T1, T2, T3> action)
/// <summary>
/// same method at python product method.
/// </summary>
/// <param name="item1"></param>
/// <param name="item2"></param>
/// <param name="item3"></param>
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
/// <exception cref="Exception"></exception>
public static void xForEach<T>(this T[] item1, T[] item2, T[] item3, Action<T, T, T> action)
{
if(item1.Length < item2.Length ||
item1.Length < item3.Length)
throw new Exception("item are not same length");
throw new Exception("item array are not same length");

for (var i = 0; i < item1.Length; i++)
{
Expand Down
29 changes: 29 additions & 0 deletions test/xForEachTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using eXtensionSharp.Job;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using NUnit.Framework;

Expand Down Expand Up @@ -131,5 +132,33 @@ public void xbatch_test()

Assert.Pass();
}

[Test]
public void xforeach_product_test()
{
var items1 = Enumerable.Range(1, 3).ToArray();
var items2 = Enumerable.Range(4, 3).ToArray();
var items3 = Enumerable.Range(7, 3).ToArray();

items1.xForEach(items2, items3, (a, b, c) =>
{
Assert.That(a, Is.EqualTo(items1[a - 1]));
Assert.That(b, Is.EqualTo(items2[b - 4]));
Assert.That(c, Is.EqualTo(items3[c - 7]));
});
}

[Test]
public async Task processor_test()
{
JobProsessor<int>.Instance.SetProessor(JobHandler<int>.Instance);

JobHandler<int>.Instance.Enqueue(1);
JobHandler<int>.Instance.Enqueue(2);
JobHandler<int>.Instance.Enqueue(3);
JobHandler<int>.Instance.Enqueue(4);

await Task.Delay(5000 * 2);
}
}
}

0 comments on commit 7d2a5ca

Please sign in to comment.