Yurttas/PL/OOL/Java/F/07/04/Reader.java

From ZCubes Wiki
Jump to navigation Jump to search
 1/**
 2 *  Copyright(C) 1998
 3 *  All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4 *
 5 *  Permission to use, copy, modify, and distribute this
 6 *  software and its documentation for EDUCATIONAL purposes
 7 *  and without fee is hereby granted provided that this
 8 *  copyright notice appears in all copies.
 9 *
10 *  @date   : January 1, 1998.
11 *  @author : Salih Yurttas.
12 */
13
14
15public class Reader extends Thread {
16
17  private final int RUNS=10;
18
19  private Buffer slot;
20  private int number;
21  private RWMonitor monitor;
22
23  public Reader(RWMonitor monitor,
24                Buffer slot,
25                int number) {
26    this.slot = slot;
27    this.number = number;
28    this.monitor = monitor;
29  }
30
31  public void run() {
32    int value;
33
34    for(int i=0; i<RUNS; i++) {
35      monitor.startRead();
36      value = slot.get();
37      monitor.endRead();
38
39      System.out.println("Reader #"+
40                         this.number+
41                         " got: "+
42                         value);
43      try {
44        Thread.sleep(((long)Math.random())*5);
45      }
46      catch(InterruptedException sleepExcept) {}
47    }
48  }
49
50}