Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code to copy:
- /************** File name: Semaphore.java ***************/
- import java.util.Date;
- class Semaphore
- {
- private int sem;
- public synchronized void swait()
- {
- while (sem <= 0)
- {
- try
- {
- wait(500);
- } catch (Exception e)
- {
- System.exit(0);
- }
- ;
- } // II end while
- sem--; // II decrease value of sem
- }// end swait
- //
- public synchronized void signal()
- {
- sem++;
- notify();
- } // end signal
- //
- // constructor
- public Semaphore(int intval)
- {
- sem = intval; // initialize attribute sem
- } // end class Semaphore
- }
- /************** File name: Producer.java ***************/
- class Producer extends Thread
- {
- SharedBuffer buffer;
- Semaphore full;
- Semaphore empty;
- Semaphore mutex;
- Producer(SharedBuffer b, Semaphore f, Semaphore e, Semaphore m)
- {
- buffer = b;
- full = f;
- empty = e;
- mutex = m;
- }
- public void run()
- {
- try
- {
- char value = 'a';
- while (true)
- {
- empty.swait();
- mutex.swait();
- buffer.insert(value);
- System.out.println("Producer Value:" + value);
- mutex.signal();
- full.signal();
- Thread.sleep(100);
- value += 2;
- if(value > 100 )
- {
- value = 0;
- }
- }
- } catch (InterruptedException e)
- {
- System.out.println("Producer: But I wasn't done!!!");
- }
- }
- }
- /************** File name: Consumer.java ***************/
- class Consumer extends Thread {
- SharedBuffer buffer;
- Semaphore full;
- Semaphore empty;
- Semaphore mutex;
- Consumer(SharedBuffer b, Semaphore f, Semaphore e, Semaphore m) {
- buffer = b;
- full = f;
- empty = e;
- mutex = m;
- }
- public void run() {
- try {
- char value;
- while(true) {
- full.swait();
- mutex.swait();
- value = buffer.retrieve();
- mutex.signal();
- empty.signal();
- System.out.println("Consumer Value: " + value);
- Thread.sleep(200);
- }
- } catch (InterruptedException e) {
- System.out.println("Consumer: But I wasn't done!!!");
- }
- }
- }
- /************** File name: SharedBuffer.java ***************/
- // Create a class named SharedBuffer
- class SharedBuffer
- {
- // Create an array of type char
- private char[] buffer;
- private int in = 0;
- private int out = 0;
- private int count = 0;
- private int size;
- SharedBuffer(int size)
- {
- this.size = size;
- buffer = new char[size];
- }
- public void insert(char o)
- {
- buffer[in] = o;
- ++count;
- in = (in + 1) % size;
- }
- public char retrieve()
- {
- int ret_val = buffer[out];
- --count;
- out = (out + 1) % size;
- return ((char) ret_val);
- }
- }
- /************** File name: ProdconSync.java ***************/
- // Create a class named ProdconSync
- public class ProdconSync
- {
- // number of slots in buffer
- static final int N = 100;
- static SharedBuffer sharedBuff = new SharedBuffer(N);
- // Create a main method to run the program.
- public static void main(String args[])
- {
- Semaphore mutex = new Semaphore(1);
- Semaphore full = new Semaphore(0);
- Semaphore empty = new Semaphore(N);
- Producer prod = new Producer(sharedBuff, mutex, full, empty);
- Consumer cons = new Consumer(sharedBuff, mutex, full, empty);
- prod.start();
- cons.start();
- } // end main
- } // end class ProdconSync
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement