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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
public class SyncEvents
{
public SyncEvents()
{
_newItemEvent = new AutoResetEvent(false);
_exitThreadEvent = new ManualResetEvent(false);
_eventArray = new WaitHandle[2];
_eventArray[0] = _newItemEvent;
_eventArray[1] = _exitThreadEvent;
}
public EventWaitHandle ExitThreadEvent
{
get { return _exitThreadEvent; }
}
public EventWaitHandle NewItemEvent
{
get { return _newItemEvent; }
}
public WaitHandle[] EventArray
{
get { return _eventArray; }
}
private EventWaitHandle _newItemEvent;
private EventWaitHandle _exitThreadEvent;
private WaitHandle[] _eventArray;
}
public class Producer
{
public Producer(Queue<char> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
// Producer.ThreadRun
public void ThreadRun()
{
int count = 0;
Random r = new Random();
while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))
{
lock (((ICollection)_queue).SyncRoot)
{
//int tp = r.Next(0, 100);
char tp;
while (_queue.Count < 1)
{
Console.WriteLine("Taper lettre :");
tp = Convert.ToChar( Console.ReadLine());
_queue.Enqueue(tp);
Console.WriteLine("je produit " + tp);
_syncEvents.NewItemEvent.Set();
count++;
}
}
}
Console.WriteLine("thread producteur: produced {0} items", count);
}
private Queue<char> _queue;
private SyncEvents _syncEvents;
}
public class Consumer
{
public Consumer(Queue<char> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
// Consumer.ThreadRun
public void ThreadRun()
{
int count = 0;
while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1)
{
lock (((ICollection)_queue).SyncRoot)
{
char item = _queue.Dequeue();
Console.WriteLine("Je consome " + item);
}
count++;
}
Console.WriteLine("Consumer Thread: consumed {0} items", count);
}
private Queue<char> _queue;
private SyncEvents _syncEvents;
}
public class ThreadSyncSample
{
private static void ShowQueueContents(Queue<char> q)
{
lock (((ICollection)q).SyncRoot)
{
foreach (char item in q)
{
Console.Write("{0} ", item);
}
}
Console.WriteLine();
}
public static void Moul()
{
Queue<char> queue = new Queue<char>();
SyncEvents syncEvents = new SyncEvents();
Console.WriteLine("Configuration des threads.");
Producer producteur = new Producer(queue, syncEvents);
Consumer consomateur = new Consumer(queue, syncEvents);
Thread Thread_producteur = new Thread(producteur.ThreadRun);
Thread Thread_consomateur= new Thread(consomateur.ThreadRun);
Console.WriteLine("Lancement des threads producer and consumer...");
Thread_producteur.Start();
Thread_consomateur.Start();
for (int i = 0; i < 4; i++)
{
Console.WriteLine("le main s'endort et laisse la place au producteur consomateur durant 5min ");
Thread.Sleep(500000);
ShowQueueContents(queue);
}
Console.WriteLine("Signaling threads to terminate...");
syncEvents.ExitThreadEvent.Set();
Thread_producteur.Join();
Thread_consomateur.Join();
Console.ReadLine();
}
}
namespace thread
{
class Program
{
static void Main(string[] args)
{
ThreadSyncSample.Moul();
}
}
} |
Partager