read/write locks
i have read/write locks written but how to compare it to simple synchronization use only synchronization without locks?
public class RWLock
{
private int givenLocks;
private int waitingWriters;
public static boolean TRACE = false;
private Object mutex;
public RWLock()
{
mutex = new Object();
givenLocks = 0;
waitingWriters = 0;
}
public void getReadLock()
{
synchronized(mutex)
{
try
{
while((givenLocks == -1) || (waitingWriters != 0))
{
if(TRACE)
System.out.println(Thread.currentThread().toString() + "waiting for readlock");
mutex.wait();
}
}
catch(java.lang.InterruptedException e)
{
System.out.println(e);
}
givenLocks++;
if(TRACE)
System.out.println(Thread.currentThread().toString() + " got readlock, GivenLocks = " + givenLocks);
}
}
public void getWriteLock()
{
synchronized(mutex)
{
waitingWriters++;
try
{
while(givenLocks != 0)
{
if(TRACE)
System.out.println(Thread.currentThread().toString() + "waiting for writelock");
mutex.wait();
}
}
catch(java.lang.InterruptedException e)
{
System.out.println(e);
}
waitingWriters--;
givenLocks = -1;
if(TRACE)
System.out.println(Thread.currentThread().toString() + " got writelock, GivenLocks = " + givenLocks);
}
}
public void releaseLock()
{
synchronized(mutex)
{
if(givenLocks == 0)
return;
if(givenLocks == -1)
givenLocks = 0;
else
givenLocks--;
if(TRACE)
System.out.println(Thread.currentThread().toString() + " released lock, GivenLocks = " + givenLocks);
mutex.notifyAll();
}
}
}
class Data
{
public RWLock lock;
int data;
private boolean ready;
Data()
{
lock = new RWLock();
data = 0;
ready = false;
}
}
class ReadThread extends Thread
{
Data status;
boolean sleep;
ReadThread(Data data , boolean sleep)
{
this.status = data;
this.sleep = sleep;
}
public void run()
{
Thread.yield();
//System.out.println(Thread.currentThread() + " Going to read");
status.lock.getReadLock();
//System.out.println(Thread.currentThread() + " got readLock");
if(sleep == true)
{
try
{
//System.out.println(Thread.currentThread() + " going to Sleep");
Thread.yield();
//Thread.sleep(1000);
}
catch(Exception e){}
}
//System.out.println(Thread.currentThread() + " releasing readLock");
status.lock.releaseLock();
}
}
class WriteThread extends Thread
{
Data status;
boolean sleep;
WriteThread(Data data , boolean sleep)
{
this.status = data;
this.sleep = sleep;
}
public void run()
{
Thread.yield();
System.out.println(Thread.currentThread() + " Going to write");
status.lock.getWriteLock();
System.out.println(Thread.currentThread() + " got writeLock");
if(sleep == true)
{
try
{
System.out.println(Thread.currentThread() + " going to Sleep");
Thread.yield();
//Thread.sleep(2000);
}
catch(Exception e){}
}
System.out.println(Thread.currentThread() + " releasing writeLock");
status.lock.releaseLock();
}
}
public class locks extends Thread
{
Data status;
locks()
{
status = new Data();
}
public static void main(String arg[])
{
locks testLock = new locks();
testLock.status.lock.TRACE = true;
WriteThread wt1 = new WriteThread(testLock.status, true);
WriteThread wt2 = new WriteThread(testLock.status, true);
ReadThread rt1 = new ReadThread(testLock.status, true);
ReadThread rt2 = new ReadThread(testLock.status, true);
wt1.start();
rt1.start();
wt2.start();
rt2.start();
Thread.yield();
}
}

