/*****************************************************************************/ /* */ /* The Readers Writers Problem */ /* */ /* Masaaki Mizuno */ /* (c) 1997, 2001, 2002 */ /* */ /*****************************************************************************/ // Writer inherits Thread (Approach 1) // Reader implements Runnable (Appraoch 2) import java.util.*; class SharedPool { public final static int NUM_READERS = 18; public final static int NUM_WRITERS = 6; public static RW_Monitor rw_monitor = new RW_Monitor(); public static Random random = new Random(); public final static int reader_sleep_max = 10000; public final static int writer_sleep_max = 10000; public final static int reader_access_max = 500; public final static int writer_access_max = 700; } class RW_Monitor { private int nr = 0; private int nw = 0; private Object or = new Object(); private Object ow = new Object(); private int nr_count = 0; private int nw_count = 0; public void startRead() { synchronized (or) { if (! startReadCheck()) try { or.wait(); } catch (InterruptedException e) {} } } private synchronized boolean startReadCheck() { if (nw == 0) { nr++; return true; } else { nr_count++; return false; } } public void finishRead() { synchronized(this) { nr--; } signal_w(); } public void startWrite() { synchronized (ow) { if (! startWriteCheck()) try { ow.wait(); } catch (InterruptedException e) {} } } private synchronized boolean startWriteCheck() { if ((nr == 0) && (nw == 0)) { nw++; return true; } else { nw_count++; return false; } } public void finishWrite() { synchronized(this) { nw--; } signal_w(); signal_r(); } private void signal_r() { synchronized (or) { while (signal_check_r()) or.notify(); } } private synchronized boolean signal_check_r() { if ((nr_count > 0) && (nw == 0)) { nr++; nr_count--; return true; } else return false; } private void signal_w() { synchronized(ow) { while (signal_check_w()) ow.notify(); } } private synchronized boolean signal_check_w() { if ((nw_count > 0) && ((nr == 0) && (nw == 0))) { nw++; nw_count--; return true; } else return false; } } class Writer extends Thread { // Approach 1 (Writer inherits Thread) private int id; public Writer(int i) { id = i; } public void run() { try{ while(true) { Thread.sleep((int) (SharedPool.writer_sleep_max*SharedPool.random.nextDouble())); System.out.println("Writer[" + id + "] arrives at " + new Date()); SharedPool.rw_monitor.startWrite(); System.out.println("Writer[" + id + "] STARTS writing at " + new Date()); Thread.sleep((int) (SharedPool.writer_access_max*SharedPool.random.nextDouble())); System.out.println("Writer[" + id + "] FINISHES writing at " + new Date()); SharedPool.rw_monitor.finishWrite(); } } catch(Exception e) {} } } class Reader implements Runnable { // Approach 2 (Reader implements Runnable) private int id; public Reader(int i) { id = i; } public void run() { try{ while(true) { Thread.sleep((int) (SharedPool.reader_sleep_max*SharedPool.random.nextDouble())); System.out.println("Reader[" + id + "] arrives at " + new Date()); SharedPool.rw_monitor.startRead(); System.out.println("Reader[" + id + "] STARTS READING at " + new Date()); Thread.sleep((int) (SharedPool.reader_access_max*SharedPool.random.nextDouble())); System.out.println("Reader[" + id + "] FINISHES READING at " + new Date()); SharedPool.rw_monitor.finishRead(); } } catch(Exception e) {} } } public class RW_NNA { public static void main(String[] args) { Writer[] writer = new Writer[SharedPool.NUM_WRITERS]; Reader[] reader = new Reader[SharedPool.NUM_READERS]; Thread t; /* initialize processes*/ for(int i = 0; i < SharedPool.NUM_WRITERS ; i++) { // Approach 1 writer[i] = new Writer(i); writer[i].start(); } for(int i = 0; i < SharedPool.NUM_READERS ; i++) { // Approach 2 reader[i] = new Reader(i); t = new Thread(reader[i]); t.start(); } } } --------------080509080104030907030504--