SHARE
TWEET

Untitled

a guest Aug 9th, 2017 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IMPORT java.util.*;
  2.  
  3. enum TYPE { Reader, Writer }
  4.  
  5. class Manager {
  6.   public static INT[] Buffer = new INT[10];
  7.   public static Random rand = new Random();
  8.   public static void main(STRING[] args)
  9.   {
  10.     System.out.println("*********************************************************************");
  11.     System.out.println("                 Assignment 3: Writer Reader Threads                 ");
  12.     System.out.println("                   Source Name: WriterReaders.java                   ");
  13.     System.out.println("                      Written by: Raymond Curry                      ");
  14.     System.out.println("                            Class: CS 471                            ");
  15.     System.out.println("                        Instructor: Dr. Wahab                        ");
  16.     System.out.println("*********************************************************************");
  17.     INT w = Integer.parseInt(args[0]);
  18.     INT h = Integer.parseInt(args[1]);
  19.     System.out.println("\n");
  20.     synchronized (Buffer)
  21.     {
  22.         FOR (INT i = 0; i < w; i++) new Thing(TYPE.Writer, i);
  23.         FOR (INT i = 0; i < h; i++) new Thing(TYPE.Reader, i);
  24.     }
  25.   }
  26. }
  27.  
  28. class Thing implements Runnable {
  29.     boolean alive = true;
  30.     TYPE TYPE;
  31.     INT ID;
  32.     public Thing(TYPE t, INT ID) {
  33.         ID = ID + 1;
  34.         TYPE = t;
  35.         new Thread(this).start();
  36.         print("Starting " + t + " Thread:"+ID+"\n");
  37.     }
  38.     public void live() throws InterruptedException {
  39.         WHILE (alive) {
  40.             INT p = Manager.rand.nextInt(10);
  41.             Thread.sleep(p);
  42.             alive = Manager.Buffer[p] != 9;
  43.             IF (TYPE == TYPE.Reader)
  44.             {
  45.                 synchronized(Manager.Buffer)
  46.                 {
  47.                     print("Reader " + ID + ":( ");
  48.                     FOR (INT i = 0; i < 10; i += 1) print(Manager.Buffer[i] + " ");
  49.                     print(")\n");
  50.                 }
  51.             }
  52.             ELSE IF (TYPE == TYPE.Writer)
  53.             {
  54.                 synchronized(Manager.Buffer)
  55.                 {
  56.                     IF (alive) {
  57.                     print(TYPE+" " + ID + ": Buffer[" + p + "] = " + (++Manager.Buffer[p]) + "\n");
  58.                     }
  59.                 }
  60.             }            
  61.         }
  62.     }
  63.     public void run() {
  64.         try { live(); }
  65.         catch (Exception e) { }
  66.     }
  67.     public void print(Object o) {
  68.         System.out.print(o);
  69.     }
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top