-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
37 lines (30 loc) · 1.02 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleObjectPool {
public class Program
{
public static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
Task.Run(() =>
{
if (Console.ReadKey().KeyChar == 'c' || Console.ReadKey().KeyChar == 'C')
cts.Cancel();
}, cts.Token);
ObjectPool<TestClass> pool = new TestPool();
Parallel.For(0, 1000000, (i, loopState) =>
{
TestClass mc = pool.CheckOut();
Console.CursorLeft = 0;
Console.WriteLine("{0:####.####}", mc.GetValue(i));
pool.CheckIn(mc);
if (cts.Token.IsCancellationRequested)
loopState.Stop();
});
Console.WriteLine("Press the Enter key to exit.");
Console.ReadLine();
cts.Dispose();
}
}
}