User Tools

Site Tools


readers-writers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

readers-writers [2007/10/05 12:24] – external edit 127.0.0.1readers-writers [2009/04/26 19:42] (current) franck
Line 1: Line 1:
-====== A solution to the readers-writers problem in Java ====== +A solution to the readers-writers problem using semaphores in Java. 
- +First, a Reader.
-We start with the database. +
 <code java> <code java>
 +package semaphore;
 + 
 +import java.util.Random;
 +import java.util.concurrent.Semaphore;
 + 
 /** /**
-  This class represents a database.  There are many  + * A reader
-  competing threads wishing to read and write It is  + * 
-  acceptable to have multiple processes reading at the  + * @author Franck van Breugel
-  same time, but if one thread is writing then no other  +
-  process may either read or write.+
  */  */
-public class Database+public class Reader extends Thread
 { {
-  private int readers; // number of active readers +  private static int readers = 0; 
 +  private static final Semaphore mutex = new Semaphore(1)
 +     
   /**   /**
-    Initializes this database+   Initializes this reader
-  */ +   */ 
-  public Database()+  public Reader()
   {   {
-    this.readers = 0;+    super();
   }   }
 +                                                                                
   /**   /**
-    Read from this database. +   * Reads
- +   */ 
-    @param number Number of the reader+  public void run()
-  */ +
-  public void read(int number)+
   {   {
-    synchronized(this) 
-    { 
-      this.readers++; 
-      System.out.println("Reader " + number + " starts reading."); 
-    } 
- 
-    final int DELAY = 5000; 
     try     try
     {     {
-      Thread.sleep((int) (Math.random() * DELAY)); +      Reader.mutex.acquire(); 
-    } +      Reader.readers++; 
-    catch (InterruptedException e) {} +      if (Reader.readers == 1)
- +
-    synchronized(this) +
-    { +
-      System.out.println("Reader " number " stops reading."); +
-      this.readers--+
-      if (this.readers == 0)+
       {       {
-        this.notifyAll();+        Writer.writer.acquire();
       }       }
-    } +      Reader.mutex.release(); 
-  } +      // read 
- +      Reader.mutex.acquire(); 
-  /** +      Reader.readers--; 
-    Writes to this database. +      if (Reader.readers == 0)
- +
-    @param number Number of the writer+
-  *+
-  public synchronized void write(int number+
-  { +
-    while (this.readers != 0) +
-    { +
-      try+
       {       {
-        this.wait();+        Writer.writer.release();
       }       }
-      catch (InterruptedException e) {} +      Reader.mutex.release();
-    } +
-    System.out.println("Writer " + number + " starts writing."); +
- +
-    final int DELAY = 5000; +
-    try +
-    { +
-      Thread.sleep((int) (Math.random() * DELAY));+
     }     }
     catch (InterruptedException e) {}     catch (InterruptedException e) {}
- +  }
-    System.out.println("Writer " + number + " stops writing."); +
-    this.notifyAll(); +
- }+
 } }
 </code> </code>
- +Thena Writer.
-Nextwe present the reader. +
 <code java> <code java>
 +package semaphore;
 + 
 +import java.util.Random;
 +import java.util.concurrent.Semaphore;
 + 
 /** /**
-   This class represents a reader+ * A writer. 
-*/ + * 
-public class Reader extends Thread+ * @author Franck van Breugel 
 + */ 
 +public class Writer extends Thread
 { {
-  private static int readers 0; // number of readers +  public static final Semaphore writer new Semaphore(1, true)
- +    
-  private int number; +
-  private Database database+
   /**   /**
-    Creates a Reader for the specified database. +   * Initializes this writer
- +   */ 
-    @param database database from which to be read+  public Writer()
-  */ +
-  public Reader(Database database)+
   {   {
-    this.database = database; +    super();
-    this.number = Reader.readers++;+
   }   }
 +     
   /**   /**
-    Reads+   * Writes
-  */+   */
   public void run()   public void run()
   {   {
-    while (true)+    try
     {     {
-      final int DELAY = 5000; +      Writer.writer.acquire(); 
-      try +      // write 
-      { +      Writer.writer.release();
-        Thread.sleep((int) (Math.random() * DELAY)); +
-      } +
-      catch (InterruptedException e) {} +
-      this.database.read(this.number);+
     }     }
 +    catch (InterruptedException e) {}
   }   }
 } }
 </code> </code>
- +And finally an app.
-Next, we present the writer. +
 <code java> <code java>
 +package semaphore;
 + 
 /** /**
-  This class represents writer+ This app creates number of readers and writers. 
-*/ + * 
-public class Writer extends Thread+ * @author Franck van Breugel 
 + */ 
 +public class ReadersWriters
 { {
-  private static int writers = 0; // number of writers 
- 
-  private int number; 
-  private Database database; 
- 
   /**   /**
-    Creates a Writer for the specified database+   * @param args[0] numbers of readers
- +   * @param args[1] number of writers
-    @param database database to which to write+   */ 
-  */ +  public static void main(String[] args)
-  public Writer(Database database)+
   {   {
-    this.database = database; +    try
-    this.number = Writer.writers++; +
-  } +
- +
-  /** +
-    Writes. +
-  */ +
-  public void run() +
-  { +
-    while (true)+
     {     {
-      final int DELAY 5000+      int readers Integer.parseInt(args[0])
-      try+      int writers = Integer.parseInt(args[1]); 
 +      for (int i = 0; i < writers; i++)
       {       {
-        Thread.sleep((int) (Math.random() * DELAY));+        new Writer().start(); 
 +      } 
 +      for (int i = 0; i < readers; i++) 
 +      { 
 +        new Reader().start();
       }       }
-      catch (InterruptedException e) {} +    } 
-      this.database.write(this.number);+    catch (Exception e) 
 +    
 +      System.out.println("Usage: java ReadersWriters < number of readers> <number of writers>");
     }     }
   }   }
Line 170: Line 129:
 </code> </code>
  
-Finally, we present the simulator. +Below, we present another solution to the readers-writers problem.
 <code java> <code java>
 /** /**
-  This app creates specified number of readers and  + This class represents database.  There are many  
-  writers and starts them+ * competing threads wishing to read and write.  It is  
-*/ + * acceptable to have multiple processes reading at the  
-public class Simulator+ * same time, but if one thread is writing then no other  
 + * process may either read or write
 + */ 
 +public class Database
 { {
 +  private int readers; // number of active readers
 + 
   /**   /**
-    Creates the specified number of readers and writers and starts them. +   * Initializes this database
- +   */ 
-    @param args[0] The number of readers+  public Database()
-    @param args[1] The number of writers. +
-  */ +
-  public static void main(String[] args)+
   {   {
-    if (args.length < 2)+    this.readers = 0; 
 +  } 
 +  
 +  /** 
 +   * Read from this database. 
 +   */ 
 +  public void read(int number) 
 +  { 
 +    synchronized(this)
     {     {
-      System.out.println("Usage: java Simulator <number of readers> <number of writers>");+      this.readers++;
     }     }
-    else+    // read 
 +    synchronized(this)
     {     {
-      final int READERS = Integer.parseInt(args[0])+      this.readers--
-      final int WRITERS = Integer.parseInt(args[1]); +      if (this.readers == 0)
-      Database database new Database(); +
-      for (int i = 0; i < READERS; i++)+
       {       {
-        new Reader(database).start();+        this.notifyAll();
       }       }
-      for (int = 0; i < WRITERS; i++)+    } 
 +  } 
 +  
 +  /** 
 +   * Writes to this database. 
 +   */ 
 +  public synchronized void write(int number) 
 +  { 
 +    while (this.readers != 0) 
 +    { 
 +      try
       {       {
-        new Writer(database).start();+        this.wait();
       }       }
 +      catch (InterruptedException e) {}
     }     }
 +    // write
 +    this.notifyAll();
   }   }
 } }
 </code> </code>
  
readers-writers.1191587067.txt.gz · Last modified: 2009/04/26 19:42 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki