jpf:start
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| jpf:start [2007/11/23 14:00] – franck | jpf:start [2008/01/21 20:37] (current) – franck | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| More information can be found | More information can be found | ||
| [[http:// | [[http:// | ||
| + | |||
| + | Although it describes an old version of JPF, | ||
| + | [[http:// | ||
| + | manual may be useful. | ||
| To configure JPF, you may want to use a | To configure JPF, you may want to use a | ||
| Line 23: | Line 27: | ||
| To check some properties of our solution to the | To check some properties of our solution to the | ||
| readers-writers problem by means of JPF, we | readers-writers problem by means of JPF, we | ||
| - | modified our solution as follows. | + | modified our solution as follows |
| + | and assertions have been added). | ||
| <code java> | <code java> | ||
| + | /** | ||
| + | * This class represents a database. | ||
| + | * threads wishing to read and write. | ||
| + | * 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 | ||
| + | private int writers; // number of active writers | ||
| + | |||
| + | /** | ||
| + | | ||
| + | */ | ||
| + | public Database() | ||
| + | { | ||
| + | this.readers = 0; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | Read from this database. | ||
| + | |||
| + | | ||
| + | */ | ||
| + | public void read(int number) | ||
| + | { | ||
| + | synchronized(this) | ||
| + | { | ||
| + | this.readers++; | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | final int DELAY = 5000; | ||
| + | try | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException e) {} | ||
| + | |||
| + | assert((this.writers == 1 && this.readers == 0) || this.writers == 0); | ||
| + | |||
| + | synchronized(this) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | this.readers--; | ||
| + | if (this.readers == 0) | ||
| + | { | ||
| + | this.notifyAll(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | | ||
| + | |||
| + | | ||
| + | */ | ||
| + | public synchronized void write(int number) | ||
| + | { | ||
| + | while (this.readers != 0) | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | this.wait(); | ||
| + | } | ||
| + | catch (InterruptedException e) {} | ||
| + | } | ||
| + | System.out.println(" | ||
| + | this.writers++; | ||
| + | |||
| + | final int DELAY = 5000; | ||
| + | try | ||
| + | { | ||
| + | Thread.sleep((int) (Math.random() * DELAY)); | ||
| + | } | ||
| + | catch (InterruptedException e) {} | ||
| + | |||
| + | assert((this.writers == 1 && this.readers == 0) || this.writers == 0); | ||
| + | this.writers--; | ||
| + | System.out.println(" | ||
| + | this.notifyAll(); | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
jpf/start.1195826420.txt.gz · Last modified: by franck
