parent
8e441919a9
commit
a59e624c1c
@ -0,0 +1,9 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net5.0</TargetFramework> |
||||
<RootNamespace>Sync_Ueb01_Counter</RootNamespace> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
||||
@ -0,0 +1,9 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net5.0</TargetFramework> |
||||
<RootNamespace>Sync_Ueb02_Latch</RootNamespace> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
||||
@ -0,0 +1,9 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net5.0</TargetFramework> |
||||
<RootNamespace>Sync_Ueb03_WaitPool</RootNamespace> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
||||
@ -0,0 +1,20 @@ |
||||
using System; |
||||
|
||||
namespace Sync_Ueb04_NestedMonitor |
||||
{ |
||||
class BlackHole { |
||||
|
||||
private NotifyingQueue<String> queue = new NotifyingQueue<String>(); |
||||
|
||||
public void Put(String thing) { |
||||
lock (this) { |
||||
queue.Enqueue(thing); |
||||
} |
||||
} |
||||
public String Get() { |
||||
lock (this) { |
||||
return queue.Dequeue(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,32 @@ |
||||
using System.Threading; |
||||
|
||||
namespace Sync_Ueb04_NestedMonitor |
||||
{ |
||||
class NotifyingQueue<T> { |
||||
|
||||
private const int SIZE = 10; |
||||
private T[] queue = new T[SIZE]; |
||||
private int head = 0; |
||||
private int tail = 0; |
||||
|
||||
public void Enqueue(T item) { |
||||
lock (this) { |
||||
head++; |
||||
head %= SIZE; |
||||
queue[head] = item; |
||||
Monitor.Pulse(this); |
||||
} |
||||
} |
||||
|
||||
public T Dequeue() { |
||||
lock (this) { |
||||
if (head == tail) { |
||||
Monitor.Wait(this); |
||||
} |
||||
tail++; |
||||
tail %= SIZE; |
||||
return queue[tail]; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,9 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net5.0</TargetFramework> |
||||
<RootNamespace>Sync_Ueb04_NestedMonitor</RootNamespace> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
||||
@ -0,0 +1,28 @@ |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
namespace Sync_Ueb04_NestedMonitor |
||||
{ |
||||
class TestNestedMonitor { |
||||
|
||||
public static BlackHole blackhole = new BlackHole(); |
||||
|
||||
public static void Main() |
||||
{ |
||||
Thread t1 = new Thread(delegate() { |
||||
Console.WriteLine(blackhole.Get().ToString()); |
||||
}); |
||||
|
||||
Thread t2 = new Thread(delegate() { |
||||
blackhole.Put("Sonne, Licht, irgendetwas..."); |
||||
}); |
||||
|
||||
Console.WriteLine("Wir starten die Untersuchung..."); |
||||
t1.Start(); |
||||
t2.Start(); |
||||
t1.Join(); |
||||
t2.Join(); |
||||
Console.WriteLine("Was ist passiert..."); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,16 @@ |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
namespace LostSignals { |
||||
|
||||
class AWait : ISynch { |
||||
|
||||
public void Acquire() |
||||
{ |
||||
} |
||||
|
||||
public void Release() |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,19 @@ |
||||
using System.Threading; |
||||
|
||||
namespace LostSignals |
||||
{ |
||||
class AWaitTest |
||||
{ |
||||
static void Main() |
||||
{ |
||||
ISynch signal = new AWait(); |
||||
Operator op = new Operator(signal); |
||||
new Thread(op.Do).Start(); |
||||
for (int i = 0; i < 10; i++) |
||||
{ |
||||
Worker worker = new Worker(signal, op, "Worker" + i); |
||||
new Thread(worker.Do).Start(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,8 @@ |
||||
namespace LostSignals |
||||
{ |
||||
interface ISynch |
||||
{ |
||||
void Acquire(); |
||||
void Release(); |
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
namespace LostSignals |
||||
{ |
||||
class Operator |
||||
{ |
||||
public const int MAXCOUNT = 4; |
||||
private int counter = 0; |
||||
private ISynch signal; |
||||
private Random rnd; |
||||
private EventWaitHandle next = new AutoResetEvent(false); |
||||
|
||||
public Operator(ISynch signal) { |
||||
this.signal = signal; |
||||
rnd = new Random(); |
||||
} |
||||
|
||||
private void Init() { |
||||
lock (this) { |
||||
counter = MAXCOUNT; |
||||
Console.WriteLine("Es wurden " + counter + " Operationen vorbereitet..."); |
||||
} |
||||
} |
||||
|
||||
public int Operation() { |
||||
lock (this) { |
||||
return rnd.Next(1000); |
||||
} |
||||
} |
||||
|
||||
public void Done(String id) { |
||||
lock (this) { |
||||
counter++; |
||||
Console.WriteLine(counter + ". Operation wurde durch " + id + " beendet."); |
||||
if (counter == MAXCOUNT) { |
||||
next.Set(); |
||||
} |
||||
} |
||||
} |
||||
public void Do() { |
||||
while (true) { |
||||
Init(); |
||||
lock (this) { |
||||
for (; counter > 0; counter--) { |
||||
signal.Release(); |
||||
} |
||||
} |
||||
next.WaitOne(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,9 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net5.0</TargetFramework> |
||||
<RootNamespace>Sync_Ueb05_LostSignals</RootNamespace> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
||||
@ -0,0 +1,31 @@ |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
namespace LostSignals |
||||
{ |
||||
class Worker |
||||
{ |
||||
private ISynch signal; |
||||
private Operator op; |
||||
private String id; |
||||
|
||||
public Worker(ISynch signal, Operator op, String id) |
||||
{ |
||||
this.signal = signal; |
||||
this.op = op; |
||||
this.id = id; |
||||
} |
||||
|
||||
public void Do() |
||||
{ |
||||
while (true) |
||||
{ |
||||
signal.Acquire(); |
||||
Console.WriteLine(id + " released..."); |
||||
Thread.Sleep(op.Operation()); |
||||
op.Done(id); |
||||
Thread.Sleep(1); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue