Hi guys, this is my code but I got some error. I think I made a mistake in main part. Buto I dont know how can I fix it. Anyone can help me?
I have an error in executer part. These are my errors:
The constructor Writer(ReadWriteLock) is undefined
The constructor Reader(ReadWriteLock) is undefined
Also this is my code:
public class ReaderWriter{
public static void main(String args[]){
ReadWriteLock RW = new ReadWriteLock();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
}
}
//Writer class
class Writer implements Runnable
{
private ReadWriteLock rwlock;
private int writerNum;
public Writer(int w, ReadWriteLock rw) {
writerNum = w;
rwlock = rw;
}
public void run() {
while (true){
SleepUtilities.nap();
System.out.println("writer " + writerNum + " wants to write.");
rwlock.WriteLock(writerNum);
SleepUtilities.nap();
rwlock.WriteUnLock(writerNum);
}
}
}
//Reader class
class Reader implements Runnable
{
private ReadWriteLock rwlock;
private int readerNum;
public Reader(int readerNum, ReadWriteLock database) {
this.readerNum = readerNum;
this.rwlock = database;
}
public void run() {
while (true) {
SleepUtilities.nap();
System.out.println("reader " + readerNum + " wants to read.");
rwlock.ReadLock(readerNum);
// you have access to read from the database
// let's read for awhile .....
SleepUtilities.nap();
rwlock.ReadUnLock(readerNum);
}
};
}
//Lock class
class ReadWriteLock {
private int readerCount;
private Semaphore mutex;
private Semaphore rwlock;
public ReadWriteLock() {
readerCount = 0;
mutex = new Semaphore(1);
rwlock = new Semaphore(1);
}
public void ReadLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {
}
++readerCount;
if (readerCount == 1){
try{
rwlock.acquire();
}
catch (InterruptedException e) {
}
}
System.out.println("Reader " + readerNum + " is reading. Reader count = " + readerCount);
mutex.release();
}
public void ReadUnLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {}
--readerCount;
if (readerCount == 0){
rwlock.release();
}
System.out.println("Reader " + readerNum + " is done reading. Reader count = " + readerCount);
mutex.release();
}
public void WriteLock(int writerNum) {
try{
rwlock.acquire();
}
catch (InterruptedException e) {}
System.out.println("Writer " + writerNum + " is writing.");
}
public void WriteUnLock(int writerNum) {
System.out.println("Writer " + writerNum + " is done writing.");
rwlock.release();
}
}
class SleepUtilities
{
public static void nap() {
nap(NAP_TIME);
}
public static void nap(int duration) {
int sleeptime = (int) (NAP_TIME * Math.random() );
try { RW.sleep(sleeptime*1000); }
catch (InterruptedException e) {}
}
private static final int NAP_TIME = 5;
}