added Csharp Uebung projects

main
Jonas Arnold 4 years ago
parent 8e441919a9
commit a59e624c1c
  1. 14
      ADIS_Csharp/ADIS_Csharp.sln
  2. 22
      ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs
  3. 36
      ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs
  4. 9
      ADIS_Csharp/Sync-Ueb01-Counter/Sync-Ueb01-Counter.csproj
  5. 13
      ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs
  6. 21
      ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs
  7. 28
      ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs
  8. 9
      ADIS_Csharp/Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj
  9. 22
      ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs
  10. 28
      ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs
  11. 9
      ADIS_Csharp/Sync-Ueb03-WaitPool/Sync-Ueb03-WaitPool.csproj
  12. 19
      ADIS_Csharp/Sync-Ueb03-WaitPool/TestWaitPool.cs
  13. 20
      ADIS_Csharp/Sync-Ueb04-NestedMonitor/BlackHole.cs
  14. 32
      ADIS_Csharp/Sync-Ueb04-NestedMonitor/NotifyingQueue.cs
  15. 9
      ADIS_Csharp/Sync-Ueb04-NestedMonitor/Sync-Ueb04-NestedMonitor.csproj
  16. 28
      ADIS_Csharp/Sync-Ueb04-NestedMonitor/TestNestedMonitor.cs
  17. 16
      ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs
  18. 19
      ADIS_Csharp/Sync-Ueb05-LostSignals/AWaitTest.cs
  19. 8
      ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs
  20. 53
      ADIS_Csharp/Sync-Ueb05-LostSignals/Operator.cs
  21. 9
      ADIS_Csharp/Sync-Ueb05-LostSignals/Sync-Ueb05-LostSignals.csproj
  22. 31
      ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs

@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.1.32421.90 VisualStudioVersion = 17.1.32421.90
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb01-Counter", "Sync-Ueb01-Counter\Sync-Ueb01-Counter.csproj", "{AB57199F-6373-4730-AB5B-76803A589AAF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb02-Latch", "Sync-Ueb02-Latch\Sync-Ueb02-Latch.csproj", "{BCCFD952-F042-483E-8660-EEAD346E37E5}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +19,14 @@ Global
{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Debug|Any CPU.Build.0 = Debug|Any CPU {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.ActiveCfg = Release|Any CPU {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.Build.0 = Release|Any CPU {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.Build.0 = Release|Any CPU
{AB57199F-6373-4730-AB5B-76803A589AAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB57199F-6373-4730-AB5B-76803A589AAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB57199F-6373-4730-AB5B-76803A589AAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB57199F-6373-4730-AB5B-76803A589AAF}.Release|Any CPU.Build.0 = Release|Any CPU
{BCCFD952-F042-483E-8660-EEAD346E37E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCCFD952-F042-483E-8660-EEAD346E37E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCCFD952-F042-483E-8660-EEAD346E37E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCCFD952-F042-483E-8660-EEAD346E37E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb01_Counter
{
class Counter
{
private int count = 0;
private static object syncRoot = new();
public int NextNumber()
{
lock (syncRoot)
{
count++;
return count;
}
}
}
}

@ -0,0 +1,36 @@
using System;
using System.Threading;
namespace Sync_Ueb01_Counter
{
class CounterTest
{
private String id;
private Counter counter;
private object locker = new object();
public CounterTest(String id, Counter counter)
{
this.id = id;
this.counter = counter;
}
void Go()
{
while (true)
{
Thread.Sleep(100);
Console.WriteLine(id + counter.NextNumber());
}
}
static void Main()
{
Counter counter = new Counter();
CounterTest ct1 = new CounterTest("T1: ", counter);
CounterTest ct2 = new CounterTest("T2: ", counter);
CounterTest ct3 = new CounterTest("T3: ", counter);
new Thread(ct1.Go).Start();
new Thread(ct2.Go).Start();
new Thread(ct3.Go).Start();
}
}
}

@ -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,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Sync_Ueb02_Latch
{
interface ISynch
{
void Acquire();
void Release();
}
}

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb02_Latch
{
class Latch : ISynch
{ private ManualResetEvent waitHandle = new(false);
public void Acquire()
{
this.waitHandle.WaitOne();
}
public void Release()
{
this.waitHandle.Set();
}
}
}

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb02_Latch
{
class RaceHorse
{
private ISynch startSignal;
private int nr;
public RaceHorse(int nr, ISynch startSignal)
{
this.nr = nr;
this.startSignal = startSignal;
}
public void Run()
{
Console.WriteLine("Rennpferd " + nr + " geht in die Startbox.");
startSignal.Acquire();
Console.WriteLine("Rennpferd " + nr + " läuft los.");
Thread.Sleep(new Random().Next(3000));
Console.WriteLine("Rennpferd " + nr + " ist im Ziel.");
}
}
}

@ -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,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb02_Latch
{
class Turf
{
static void Main()
{
ISynch startBox = new Latch();
for (int i = 1; i <= 5; i++)
{
new Thread(new RaceHorse(i, startBox).Run).Start();
}
Console.WriteLine("Start...");
startBox.Release();
}
}
}

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb03_WaitPool
{
class MyThread
{
private Object synch;
public MyThread(Object synch)
{
this.synch = synch;
}
public void Run()
{
Console.WriteLine("warten...");
lock (synch)
{
Monitor.Wait(this);
}
Console.WriteLine("...aufgewacht");
}
}
}

@ -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,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Sync_Ueb03_WaitPool
{
class TestWaitPool
{
static void Main()
{
Object synch = new Object();
MyThread myThread = new MyThread(synch);
new Thread(myThread.Run).Start();
Thread.Sleep(1000);
Monitor.Pulse(synch);
}
}
}

@ -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…
Cancel
Save