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();

}

}

[4239 byte] By [oll3ia] at [2007-11-27 3:05:43]
# 1

> i have read/write locks written but how to compare it

> to simple synchronization use only synchronization without locks?

"simple synchronization" is stronge than read/write locks. It blocks every one access, making no difference read or write...

BTW, use [code] tag for such a big sample please ;)

_Dima_a at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...
# 2

public class Person

{

int age;

rwLockMgr lockmanager;

public Person()

{

age = 17;

lockmanager = new rwLockMgr();

}

public int getAge()

{

lockmanager.lockRead();

return age;

}

public void putAge(int newage)

{

lockmanager.lockWrite();

age = newage;

}

public void releaseLock()

{

lockmanager.releaseLock();

}

if i have a simple example like the code above with locks and i wrote it with synchronization only ( the code below)... is it correct ? what would be the use of these classes to show threads working?

public class PersonSyncOnly {

int age;

public PersonSyncOnly()

{

age = 17;

}

public synchronized int getAge()

{

return age;

}

public synchronized void putAge(int newage)

{

age = newage;

}

}

oll3ia at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...
# 3
They seem similar. To see threads just share instance of Preson among 2 threads. One setting and the other printing the age.PS Logic of lockmanager was ignored.
_Dima_a at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...
# 4
they are supposed to be similar just one with locks and other with synchronization only
oll3ia at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...
# 5
but i need run method in these two classes?
oll3ia at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...
# 6
Thread t1 = new Thread(person); Thread t2 = new Thread(person);how to do the setting part?
oll3ia at 2007-7-12 3:51:32 > top of Java-index,Core,Core APIs...