ReentrantLock doesn't work out

2019-03-03 07:11发布

问题:

I can't figure out why the code doesn't work properly. The problem is that ReentrantLock doesn't lock methods invocation in ThreadClass.run()

Resource-class which methods are assumed to be locked in ThreadClass

public class ResourceClass {

    private int i; 

    public void setIncrement() {
        i++;
    }

    public int getIncrement() {
        return i;
    }

}

Thread-class

public class ThreadClass implements Runnable {

    private ResourceClass resource;
    private ReentrantLock reentrantLock = new ReentrantLock();

    ThreadClass(ResourceClass r) {
        resource = r;
    }

    public void run() {
        reentrantLock.lock();
        try {
            resource.setIncrement();
            System.out.println(resource.getIncrement());
        } finally {
            reentrantLock.unlock();
        }
    }

}

Main-class

public class MainClass {

    public static void main(String[] args) {

        ResourceClass resource = new ResourceClass();

        Thread thread = new Thread(new ThreadClass(resource));
        thread.start();
        Thread thread2 = new Thread(new ThreadClass(resource));
        thread2.start();
        Thread thread3 = new Thread(new ThreadClass(resource));
        thread3.start();
    }

} 

It is supposed that the code surrounded by lock in run() must be "synchronised" thus results that only one thread has access to the methods of Resourse-object. At practice it doesn't. The code results repeated numbers which means that two threads have access to the methods at the same time. I know that it is really simple issue but I can't understand how to solve it. Thanks for assistance.

Update:

I got that. That piece of code works just fine (I deleted setIncrement() and put all the relevant logic into getIncrement()):

public int getIncrement() {
        reentrantLock.lock();
        int incrementResult = i++;
        reentrantLock.unlock();
        return incrementResult;
} 

回答1:

You are creating a new ReentrantLock per runnable meaning there is no synchronization. You will need to share the one lock with each Runnable instance.