Reader Writer Using Synchonized Method


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package readerwriterusing_synchonizedmethod;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mahi
 */
public class ReaderWriterUsing_synchonizedmethod {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        ReaderWriter myReaderWriter = new ReaderWriter();
        Reader rt = new Reader(myReaderWriter);
        Writer wt = new Writer(myReaderWriter);
        
        rt.start();
        wt.start();
    }
    
}

class ReaderWriter{

    private int readerCount;
    private int writerCount;
    private boolean dbWriting;

    public ReaderWriter() {
        readerCount = 0;
        writerCount = 0;
        dbWriting = false;
    }
    
    public synchronized void aquiReaderLock(){
        try {
                while(dbWriting){
                wait();
                }
                readerCount++;
                
        } catch (InterruptedException ex) {
            Logger.getLogger(ReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public synchronized  void releaseReaderLock(){
        readerCount --;
        if(readerCount == 0){
            notifyAll();
        }
    }
    
    public synchronized void aquirWriterLock(){
        try {
               if(dbWriting || readerCount>0){
               wait();
               }
               writerCount++;
               dbWriting = true;
        } catch (InterruptedException ex) {
            System.out.println("Error on reader thread...");
        }
    }
    
    public synchronized void releaseWriterLock(){
        dbWriting = false;
        writerCount --;
        notifyAll();
    }
}


class Reader extends Thread{
    ReaderWriter rw;

    public Reader(ReaderWriter rw) {
        this.rw = rw;
    }

    @Override
    public void run() {
        try {
             while(true){
             System.out.println("Reader wahts to read now...!");
             rw.aquiReaderLock();
             sleep(50);
             rw.releaseReaderLock();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
        }
            
    }
    
}

class Writer extends Thread{

    ReaderWriter rw;

    public Writer(ReaderWriter rw) {
        this.rw = rw;
    }

    @Override
    public void run() {
        try {
            while(true){
                System.out.println("Writer wants to write now...");
                rw.aquirWriterLock();
                sleep(50);
                rw.releaseWriterLock();
            }
        } catch (InterruptedException ex) {
            System.out.println("Error on write thread...");
        }
    }
}


EmoticonEmoticon