Results 1 to 3 of 3
  1. #1
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Efficient Read/Write Lock

    I'm trying to write an efficient read write lock... the guarded value will be read constantly but written VERY rarely... Thoughts? Do you think this is a valid solution?

    Java Code:
    public class SyncRef<E>  {
    	private final ReentrantLock barrier = new ReentrantLock();
    	private volatile boolean locked = false;
    	private E value;
    	public SyncRef(E initialValue) {
    		value = initialValue;
    	}
    	public void lock() {
    		barrier.lock();
    		locked = true;
    	}
    	public void unlock() {
    		locked = false;
    		barrier.unlock();
    	}
    	public void set(E newValue) {
    		value = newValue;
    	}
    	public E get() {
    		if (locked) {
    			barrier.lock();
    			barrier.unlock();
    		}
    		return value;
    	}	
    }
    (I'm assuming Java 1.5+ if you were wondering)

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It's not valid. The following could happen:

    a thread calls 'lock' and 'barrier.lock' is called, but not yet "locked = true". A 2nd thread calls unlock and 'locked' is set to false'. A 3rd thread calls 'get', and the value is returned, even though the barrier has been locked.


    Might I suggest something else?

    Java Code:
    public class SyncRef<E>  {
    	private volatile boolean locked = false;
    	private E value;
    	public SyncRef(E initialValue) {
    		value = initialValue;
    	}
    	public synchronized void lock() {
    		locked = true;
    	}
    	public synchronized void unlock() {
    		locked = false;
                    notify();
    	}
    	public void set(E newValue) {
    // !!! seems like you want protection around this too, to keep value from changing while locked?
    		value = newValue;
    	}
    	public synchronized E get() {
    		while(locked) {
                       try {
                          wait();
                       } catch (InterruptedException ex) {....might want to do something here...}
    		}
    		return value;
    	}	
    }

  3. #3
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Thanks for your input!

    I don't want protection around the set, I guess I should've mentioned that =P

    I also wanted to avoid using synchronized... I basically wanted to use the double-check locking algorithm so locking only occurs if a write is occurring...

    I can make this work no problem, but I want to depend on volatile and avoid using synchronized (or locks) at all costs when reading the value.

    I'm also assuming that unlock() will only be called after every lock(), so you're scenario wouldn't occur because the first thread would wait until the second thread released the lock anyway.

    Thanks!

Similar Threads

  1. Read (using Odbc)and write using (POI api)
    By zuzi in forum New To Java
    Replies: 4
    Last Post: 12-10-2010, 11:00 AM
  2. XML read/write in Java
    By bogdy.laurentiu in forum XML
    Replies: 5
    Last Post: 05-31-2010, 06:08 PM
  3. A more efficient way to read from a text file
    By matzahboy in forum New To Java
    Replies: 13
    Last Post: 10-14-2009, 11:35 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM
  5. How to write efficient maintainable code.
    By Zosden in forum Advanced Java
    Replies: 9
    Last Post: 05-01-2008, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •