java, multithreading, how to block another method
<p>I have one class having 2 method, Read method and Write method.
I want multi-thread can get access the read method and only 1 thread get access the read method. And when the write method is accessed, the read method will be blocked. How should I do it?</p>

<p>Note: after reading the answers I wrote a little piece of code for this, I used concurrent package lock.</p>

<pre><code>public class ConcurrentLock {
private Lock wLock = new Lock();
private Lock rLock = new Lock();
private int i;
public static void main(String[] args){

}
public int Read(){
    try {
        rLock.lock();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        rLock.unlock();
    }
    return i;
}

public void Write(){
    try {
        wLock.lock();
        rLock.lock();
        ++i;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        wLock.unlock();
        rLock.unlock();
    }

}
</code></pre>

<p>}</p>
