readers-writers
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| readers-writers [2007/10/05 12:19] – franck | readers-writers [2007/10/05 12:24] (current) – franck | ||
|---|---|---|---|
| Line 87: | 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 93: | 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 99: | 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.1191586799.txt.gz · Last modified: by franck
