Use Java to write this program Use each of the concurrency techniques: 1.semapho
ID: 3699518 • Letter: U
Question
Use Java to write this program
Use each of the concurrency techniques: 1.semaphores 2.message passing
To solve the following concurrency problem:
A parking lot with a capacity of N cars is on an island. A ferry with a capacity of M cars shuttles cars between the mainland and the island. The ferry may cross whenever there are no cars waiting to get on the ferry (even if it is not full) provided that there are cars waiting on the other side. The ferry may also cross whenever it is full or (in the case of crossing to the island) when the number of cars in the ferry would fill the parking lot.
Explanation / Answer
1A
// 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
{
Semaphore sem;
String threadName;
public MyThread(Semaphore sem, String threadName)
{
super(threadName);
this.sem = sem;
this.threadName = threadName;
}
@Override
public void run() {
// run by thread A
if(this.getName().equals("A"))
{
System.out.println("Starting " + threadName);
try
{
// First, get a permit.
System.out.println(threadName + " is waiting for a permit.");
// acquiring the lock
sem.acquire();
System.out.println(threadName + " gets a permit.");
// Now, accessing the shared resource.
// other waiting threads will wait, until this
// thread release the lock
for(int i=0; i < 5; i++)
{
Shared.count++;
System.out.println(threadName + ": " + Shared.count);
// Now, allowing a context switch -- if possible.
// for thread B to execute
Thread.sleep(10);
}
} catch (InterruptedException exc) {
System.out.println(exc);
}
// Release the permit.
System.out.println(threadName + " releases the permit.");
sem.release();
}
// run by thread B
else
{
System.out.println("Starting " + threadName);
try
{
// First, get a permit.
System.out.println(threadName + " is waiting for a permit.");
// acquiring the lock
sem.acquire();
System.out.println(threadName + " gets a permit.");
// Now, accessing the shared resource.
// other waiting threads will wait, until this
// thread release the lock
for(int i=0; i < 5; i++)
{
Shared.count--;
System.out.println(threadName + ": " + Shared.count);
// Now, allowing a context switch -- if possible.
// for thread A to execute
Thread.sleep(10);
}
} catch (InterruptedException exc) {
System.out.println(exc);
}
// Release the permit.
System.out.println(threadName + " releases the permit.");
sem.release();
}
}
}
// Driver class
public class SemaphoreDemo
{
public static void main(String args[]) throws InterruptedException
{
// creating a Semaphore object
// with number of permits 1
Semaphore sem = new Semaphore(1);
// 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(sem, "A");
MyThread mt2 = new MyThread(sem, "B");
// 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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.