How to replace synchronized, wait, notify semaphores? (Producer-consumer)
Good evening,
I am wondering how can I replace the synchronized, wait and notify semaphores in the following code? And where do I need to create the semaphore variable?
import java.util.*;
class Producer
extends Thread
{
private Vector v;
public Producer(Vector v)
{
this.v = v;
}
public void run()
{
String s;
while (true) {
synchronized (v) {
s = "Value"+Math.random();
v.addElement(s);
System.out.println("Producer created "+s);
v.notify();
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nothing
}
}
}
}
class Consumer
extends Thread
{
private Vector v;
public Consumer(Vector v)
{
this.v = v;
}
public void run()
{
while (true) {
synchronized (v) {
if (v.size() < 1) {
try {
v.wait();
} catch (InterruptedException e) {
//nothing
}
}
System.out.print(
" Consumer found "+(String)v.elementAt(0)
);
v.removeElementAt(0);
System.out.println(" (remaning: "+v.size()+")");
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nothing
}
}
}
}
I would be glad if someone can help me!
Thanks in advance.
source to share
Think of a semaphore as a lock that allows multiple threads to access a shared resource. Regardless of the number that you initialize the semaphore to, this will allow many threads to access the resource at the same time. In Producer-Consumer, a resource is a shared buffer between two threads. You want to make sure that the consumer cannot access the buffer if it is not full, and the producer cannot access the buffer if it is not empty. You must start by counting 0 in the consumer semaphore and 1 in the production semaphore. Thus, the Producer must take the first step. When the Producer starts writing to the buffer, you want the down
Producer Semaphore. When the Producer is done, you wantup
the Consumer semaphore that will allow the Consumer to access the resource. When the consumer has accessed a resource, then the up
producer semaphore notifies the producer that the buffer is now empty.
Use this as a starting point: http://cs.gmu.edu/cne/modules/ipc/aqua/producer.html
source to share
import java.util.Vector;
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
Semaphore mutex = new Semaphore(1);
Vector<String> vector = new Vector<>();
new Producer(vector, mutex).start();
new Consumer(vector, mutex).start();
}
}
class Producer extends Thread {
private Vector v;
private Semaphore mutex;
public Producer(Vector v, Semaphore mutex) {
this.v = v;
this.mutex = mutex;
}
public void run() {
String s;
while (true) {
try {
mutex.acquire();
s = "Value" + Math.random();
v.addElement(s);
System.out.println("Producer created " + s);
mutex.release();
Thread.sleep((int) (100 * Math.random()));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
class Consumer extends Thread {
private Vector v;
private Semaphore mutex;
public Consumer(Vector v, Semaphore mutex) {
this.v = v;
this.mutex = mutex;
}
public void run() {
while (true) {
try {
mutex.acquire();
if (v.size() > 0) {
System.out.print(" Consumer found " + (String) v.elementAt(0));
v.removeElementAt(0);
System.out.println(" (remaning: " + v.size() + ")");
}
mutex.release();
Thread.sleep((int) (100 * Math.random()));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
source to share