calendar
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
calendar [2009/04/26 19:42] – franck | calendar [2009/05/14 21:04] (current) – franck | ||
---|---|---|---|
Line 87: | Line 87: | ||
Implementations in Java of the reader-writers problem can be | Implementations in Java of the reader-writers problem can be | ||
found [[readers-writers|here]]. | found [[readers-writers|here]]. | ||
- | |||
- | A solution to the readers-writers problem using semaphores in Java. | ||
- | First, a Reader. | ||
- | <code java> | ||
- | package semaphore; | ||
- | |||
- | import java.util.Random; | ||
- | import java.util.concurrent.Semaphore; | ||
- | |||
- | /** | ||
- | * A reader. | ||
- | * | ||
- | * @author Franck van Breugel | ||
- | */ | ||
- | public class Reader extends Thread | ||
- | { | ||
- | private static int readers = 0; | ||
- | private static final Semaphore mutex = new Semaphore(1); | ||
- | |||
- | /** | ||
- | * Initializes this reader. | ||
- | */ | ||
- | public Reader() | ||
- | { | ||
- | super(); | ||
- | } | ||
- | | ||
- | /** | ||
- | * Reads. | ||
- | */ | ||
- | public void run() | ||
- | { | ||
- | try | ||
- | { | ||
- | Reader.mutex.acquire(); | ||
- | Reader.readers++; | ||
- | if (Reader.readers == 1) | ||
- | { | ||
- | Writer.writer.acquire(); | ||
- | } | ||
- | Reader.mutex.release(); | ||
- | // read | ||
- | Reader.mutex.acquire(); | ||
- | Reader.readers--; | ||
- | if (Reader.readers == 0) | ||
- | { | ||
- | Writer.writer.release(); | ||
- | } | ||
- | Reader.mutex.release(); | ||
- | } | ||
- | catch (InterruptedException e) {} | ||
- | } | ||
- | } | ||
- | </ | ||
- | Then, a Writer. | ||
- | <code java> | ||
- | package semaphore; | ||
- | |||
- | import java.util.Random; | ||
- | import java.util.concurrent.Semaphore; | ||
- | |||
- | /** | ||
- | * A writer. | ||
- | * | ||
- | * @author Franck van Breugel | ||
- | */ | ||
- | public class Writer extends Thread | ||
- | { | ||
- | public static final Semaphore writer = new Semaphore(1, | ||
- | | ||
- | /** | ||
- | * Initializes this writer. | ||
- | */ | ||
- | public Writer() | ||
- | { | ||
- | super(); | ||
- | } | ||
- | |||
- | /** | ||
- | * Writes. | ||
- | */ | ||
- | public void run() | ||
- | { | ||
- | try | ||
- | { | ||
- | Writer.writer.acquire(); | ||
- | // write | ||
- | Writer.writer.release(); | ||
- | } | ||
- | catch (InterruptedException e) {} | ||
- | } | ||
- | } | ||
- | </ | ||
- | And finally an app. | ||
- | <code java> | ||
- | package semaphore; | ||
- | |||
- | /** | ||
- | * This app creates a number of readers and writers. | ||
- | * | ||
- | * @author Franck van Breugel | ||
- | */ | ||
- | public class ReadersWriters | ||
- | { | ||
- | /** | ||
- | * @param args[0] numbers of readers. | ||
- | * @param args[1] number of writers. | ||
- | */ | ||
- | public static void main(String[] args) | ||
- | { | ||
- | try | ||
- | { | ||
- | int readers = Integer.parseInt(args[0]); | ||
- | int writers = Integer.parseInt(args[1]); | ||
- | for (int i = 0; i < writers; i++) | ||
- | { | ||
- | new Writer().start(); | ||
- | } | ||
- | for (int i = 0; i < readers; i++) | ||
- | { | ||
- | new Reader().start(); | ||
- | } | ||
- | } | ||
- | catch (Exception e) | ||
- | { | ||
- | System.out.println(" | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Below, we present another solution to the readers-writers problem. | ||
- | <code java> | ||
- | /** | ||
- | * This class represents a database. | ||
- | * competing threads wishing to read and write. | ||
- | * acceptable to have multiple processes reading at the | ||
- | * 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 | ||
- | |||
- | /** | ||
- | * Initializes this database. | ||
- | */ | ||
- | public Database() | ||
- | { | ||
- | this.readers = 0; | ||
- | } | ||
- | |||
- | /** | ||
- | * Read from this database. | ||
- | */ | ||
- | public void read(int number) | ||
- | { | ||
- | synchronized(this) | ||
- | { | ||
- | this.readers++; | ||
- | } | ||
- | // read | ||
- | synchronized(this) | ||
- | { | ||
- | this.readers--; | ||
- | if (this.readers == 0) | ||
- | { | ||
- | this.notifyAll(); | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | /** | ||
- | * Writes to this database. | ||
- | */ | ||
- | public synchronized void write(int number) | ||
- | { | ||
- | while (this.readers != 0) | ||
- | { | ||
- | try | ||
- | { | ||
- | this.wait(); | ||
- | } | ||
- | catch (InterruptedException e) {} | ||
- | } | ||
- | // write | ||
- | this.notifyAll(); | ||
- | } | ||
- | } | ||
- | </ | ||
===== April 2 ===== | ===== April 2 ===== | ||
Line 334: | Line 144: | ||
Willem Visser, Klaus Havelund, Guillaume Brat, SeungJoon Park and Flavio Lerda. [[http:// | Willem Visser, Klaus Havelund, Guillaume Brat, SeungJoon Park and Flavio Lerda. [[http:// | ||
- | More information about Java PathFinder can be found | + | |
- | [[jpf: | + | |
===== May 7 ===== | ===== May 7 ===== | ||
+ | |||
+ | More information about Java PathFinder can be found | ||
+ | [[jpf: | ||
===== May 12 ===== | ===== May 12 ===== | ||
+ | |||
+ | Information about handling native code with Java PathFinder can be found | ||
+ | [[jpf-native: | ||
===== May 14 ===== | ===== May 14 ===== |
calendar.1240774934.txt.gz · Last modified: 2009/04/26 19:42 by franck