Separating into Tech and SysDev

This commit is contained in:
2022-05-07 23:24:08 +02:00
parent ae93dfdacb
commit e49df859aa
88 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
// java program to demonstrate
// use of semaphores Locks
import java.util.concurrent.*;
//A shared resource/class.
class Shared
{
static int count = 0;
}
class MyThread extends Thread
{
// We need 2 semaphores.
// This one will prevent the Producer writing twice
Semaphore semPro;
// This one will prevent the consumer reading twice
Semaphore semCon;
String threadName;
public MyThread(Semaphore mySemPro, Semaphore mySemCon, String threadName)
{
super(threadName);
// We set the semaphores from the main method
this.semPro = mySemPro;
this.semCon = mySemCon;
this.threadName = threadName;
}
@Override
public void run()
{
// run by thread "Producer"
if(this.getName().equals("Producer"))
{
System.out.println("Starting " + threadName);
for ( int i=0; i<5; i++)
{
try
{
// Wait until we may write (initially we are allowed)
// coming back here - we must wait for the "Consumer" to read
semPro.acquire();
Shared.count++;
System.out.println(threadName + " Writes: " + Shared.count);
// Signal the "Consumer" that a value is ready
semCon.release();
// Now, allowing a context switch -- if possible.
// for thread Consumer to execute
Thread.sleep(10);
}
catch (InterruptedException exc)
{
// Should anything fail - then we end up here
System.out.println(exc);
}
}
// And we are done
System.out.println("Ending " + threadName);
}
// run by thread Consumer
else
{
System.out.println("Starting " + threadName);
for (int i=0; i< 5; i++)
{
try
{
// Waiting for a value to be present (initially: Wait)
semCon.acquire();
System.out.println(threadName + " Reads: " + Shared.count);
// Signal the "Producer" that the value was read, so the buffer is now cleared
semPro.release();
}
catch (InterruptedException exc)
{
// Should we fail - here we go
System.out.println(exc);
}
}
// And we are done
System.out.println("Ending " + threadName);
}
}
}

View File

@@ -0,0 +1,35 @@
// java program to demonstrate
// use of semaphores Locks
import java.util.concurrent.*;
// Driver class
public class SemaphoreDemo
{
public static void main(String args[]) throws InterruptedException
{
// creating a Semaphore object
// A 1 means that you are allowed 1 write to start with
Semaphore semPro = new Semaphore(1);
// A 0 means that you are not allowed to read (as nobody has yet written to the buffer)
Semaphore semCon = new Semaphore(0);
// creating two threads with name A and B
// Note that thread A will increment the count
// and thread B will decrement the count
MyThread mt1 = new MyThread(semPro, semCon, "Producer");
MyThread mt2 = new MyThread(semPro, semCon, "Consumer");
// stating threads A and B
mt1.start();
mt2.start();
// waiting for threads A and B
mt1.join();
mt2.join();
// count will always remain 0 after
// both threads will complete their execution
System.out.println("count: " + Shared.count);
}
}