Last change
on this file since 422 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.3 KB
|
Rev | Line | |
---|
| 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 |
|
---|
| 16 | private String value = new String("INIT");
|
---|
| 17 |
|
---|
| 18 | Database () {}; // constructor
|
---|
| 19 |
|
---|
| 20 | synchronized void get_readlock ()
|
---|
| 21 | {
|
---|
| 22 | while (writelock)
|
---|
| 23 | {
|
---|
| 24 | try { wait(); } catch (InterruptedException e)
|
---|
| 25 | {
|
---|
| 26 | System.out.println("interruptedException caught");
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | readlock++;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | synchronized String read ()
|
---|
| 33 | {
|
---|
| 34 | return (value);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | synchronized void release_readlock ()
|
---|
| 38 | {
|
---|
| 39 | readlock--;
|
---|
| 40 | notify();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | synchronized void get_writelock()
|
---|
| 44 | {
|
---|
| 45 | while (writelock && readlock > 0)
|
---|
| 46 | {
|
---|
| 47 | try { wait(); } catch (InterruptedException e)
|
---|
| 48 | {
|
---|
| 49 | System.out.println("interruptedException caught");
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | writelock = true;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | synchronized void write (String text)
|
---|
| 56 | {
|
---|
| 57 | value = value + ":" + text;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | synchronized void release_writelock()
|
---|
| 61 | {
|
---|
| 62 | writelock = false;
|
---|
| 63 | notify();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.