readers-writers
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| readers-writers [2007/10/05 12:14] – created franck | readers-writers [2007/10/05 12:24] (current) – franck | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| <code java> | <code java> | ||
| /** | /** | ||
| - | | + | |
| - | * threads wishing to read and write. | + | |
| - | | + | |
| - | | + | |
| + | | ||
| */ | */ | ||
| public class Database | public class Database | ||
| { | { | ||
| - | | + | |
| - | | + | |
| - | | + | Initializes this database. |
| - | */ | + | */ |
| - | public Database() | + | public Database() |
| + | { | ||
| + | this.readers = 0; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | Read from this database. | ||
| + | |||
| + | @param number Number of the reader. | ||
| + | */ | ||
| + | public void read(int number) | ||
| + | { | ||
| + | synchronized(this) | ||
| { | { | ||
| - | | + | |
| + | System.out.println(" | ||
| } | } | ||
| - | | + | |
| - | Read from this database. | + | |
| - | + | ||
| - | | + | |
| - | | + | |
| - | public void read(int number) | + | |
| { | { | ||
| - | synchronized(this) | + | Thread.sleep((int) (Math.random() * DELAY)); |
| - | { | + | } |
| - | this.readers++; | + | catch (InterruptedException e) {} |
| - | System.out.println(" | + | |
| - | } | + | |
| - | final int DELAY = 5000; | + | |
| - | try | + | { |
| - | { | + | System.out.println(" |
| - | Thread.sleep((int) (Math.random() * DELAY)); | + | this.readers--; |
| - | } | + | if (this.readers == 0) |
| - | catch (InterruptedException e) {} | + | { |
| - | + | this.notifyAll(); | |
| - | | + | } |
| - | { | + | |
| - | System.out.println(" | + | |
| - | this.readers--; | + | |
| - | if (this.readers == 0) | + | |
| - | { | + | |
| - | this.notifyAll(); | + | |
| - | } | + | |
| - | | + | |
| } | } | ||
| + | } | ||
| - | | + | |
| - | | + | Writes to this database. |
| - | @param number Number of the writer. | + | |
| - | */ | + | */ |
| - | public synchronized void write(int number) | + | public synchronized void write(int number) |
| + | { | ||
| + | while (this.readers != 0) | ||
| { | { | ||
| - | while (this.readers != 0) | + | |
| - | { | + | { |
| - | | + | this.wait(); |
| - | { | + | } |
| - | this.wait(); | + | catch (InterruptedException e) {} |
| - | } | + | } |
| - | catch (InterruptedException e) {} | + | System.out.println(" |
| - | } | + | |
| - | System.out.println(" | + | |
| - | + | ||
| - | final int DELAY = 5000; | + | |
| - | try | + | |
| - | { | + | |
| - | Thread.sleep((int) (Math.random() * DELAY)); | + | |
| - | } | + | |
| - | catch (InterruptedException e) {} | + | |
| - | System.out.println(" | + | final int DELAY = 5000; |
| - | this.notifyAll(); | + | try |
| + | { | ||
| + | Thread.sleep((int) (Math.random() * DELAY)); | ||
| } | } | ||
| + | catch (InterruptedException e) {} | ||
| + | |||
| + | System.out.println(" | ||
| + | this.notifyAll(); | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 86: | Line 87: | ||
| <code java> | <code java> | ||
| + | /** | ||
| + | This class represents a reader. | ||
| + | */ | ||
| + | public class Reader extends Thread | ||
| + | { | ||
| + | private static int readers = 0; // number of readers | ||
| + | private int number; | ||
| + | private Database database; | ||
| + | |||
| + | /** | ||
| + | Creates a Reader for the specified database. | ||
| + | |||
| + | @param database database from which to be read. | ||
| + | */ | ||
| + | public Reader(Database database) | ||
| + | { | ||
| + | this.database = database; | ||
| + | this.number = Reader.readers++; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | Reads. | ||
| + | */ | ||
| + | public void run() | ||
| + | { | ||
| + | while (true) | ||
| + | { | ||
| + | final int DELAY = 5000; | ||
| + | try | ||
| + | { | ||
| + | Thread.sleep((int) (Math.random() * DELAY)); | ||
| + | } | ||
| + | catch (InterruptedException e) {} | ||
| + | this.database.read(this.number); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| Line 92: | Line 130: | ||
| <code java> | <code java> | ||
| + | /** | ||
| + | This class represents a writer. | ||
| + | */ | ||
| + | public class Writer extends Thread | ||
| + | { | ||
| + | private static int writers = 0; // number of writers | ||
| + | private int number; | ||
| + | private Database database; | ||
| + | |||
| + | /** | ||
| + | Creates a Writer for the specified database. | ||
| + | |||
| + | @param database database to which to write. | ||
| + | */ | ||
| + | public Writer(Database database) | ||
| + | { | ||
| + | this.database = database; | ||
| + | this.number = Writer.writers++; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | Writes. | ||
| + | */ | ||
| + | public void run() | ||
| + | { | ||
| + | while (true) | ||
| + | { | ||
| + | final int DELAY = 5000; | ||
| + | try | ||
| + | { | ||
| + | Thread.sleep((int) (Math.random() * DELAY)); | ||
| + | } | ||
| + | catch (InterruptedException e) {} | ||
| + | this.database.write(this.number); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| Line 98: | Line 173: | ||
| <code java> | <code java> | ||
| + | /** | ||
| + | This app creates a specified number of readers and | ||
| + | writers and starts them. | ||
| + | */ | ||
| + | public class Simulator | ||
| + | { | ||
| + | /** | ||
| + | Creates the specified number of readers and writers and starts them. | ||
| + | @param args[0] The number of readers. | ||
| + | @param args[1] The number of writers. | ||
| + | */ | ||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | if (args.length < 2) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | final int READERS = Integer.parseInt(args[0]); | ||
| + | final int WRITERS = Integer.parseInt(args[1]); | ||
| + | Database database = new Database(); | ||
| + | for (int i = 0; i < READERS; i++) | ||
| + | { | ||
| + | new Reader(database).start(); | ||
| + | } | ||
| + | for (int i = 0; i < WRITERS; i++) | ||
| + | { | ||
| + | new Writer(database).start(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
readers-writers.1191586499.txt.gz · Last modified: by franck
