Last change
on this file since 394 was 2, checked in by Rick van der Zwet, 15 years ago |
Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)
|
File size:
1.4 KB
|
Rev | Line | |
---|
[2] | 1 | /*
|
---|
| 2 | * Rick van der Zwet
|
---|
| 3 | * 0433373
|
---|
| 4 | * Java programming assigment 1.2
|
---|
| 5 | * Licence: BSD
|
---|
| 6 | * $Id: Database.java 270 2007-10-13 13:45:29Z rick $
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | class Database
|
---|
| 10 | {
|
---|
| 11 | // Read lock, call next person
|
---|
| 12 | // Write lock, call next person when done
|
---|
| 13 | private int readlock = 0;
|
---|
| 14 | private boolean writelock = false;
|
---|
| 15 | private boolean writerequest = false;
|
---|
| 16 |
|
---|
| 17 | private String value = new String("INIT");
|
---|
| 18 |
|
---|
| 19 | Database () {}; // constructor
|
---|
| 20 |
|
---|
| 21 | synchronized void get_readlock ()
|
---|
| 22 | {
|
---|
| 23 | while (writelock || writerequest)
|
---|
| 24 | {
|
---|
| 25 | try { wait(); } catch (InterruptedException e)
|
---|
| 26 | {
|
---|
| 27 | System.out.println("interruptedException caught");
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 | readlock++;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | synchronized String read ()
|
---|
| 34 | {
|
---|
| 35 | return (value);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | synchronized void release_readlock ()
|
---|
| 39 | {
|
---|
| 40 | readlock--;
|
---|
| 41 | notify();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | synchronized void get_writelock()
|
---|
| 45 | {
|
---|
| 46 | writerequest = true;
|
---|
| 47 | while (writelock && readlock > 0)
|
---|
| 48 | {
|
---|
| 49 | try { wait(); } catch (InterruptedException e)
|
---|
| 50 | {
|
---|
| 51 | System.out.println("interruptedException caught");
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | writelock = true;
|
---|
| 55 | writerequest = false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | synchronized void write (String text)
|
---|
| 59 | {
|
---|
| 60 | value = value + ":" + text;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | synchronized void release_writelock()
|
---|
| 64 | {
|
---|
| 65 | writelock = false;
|
---|
| 66 | notify();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.