Skip to main content

(How) can you resolve issues with thread lock code - II

Please review  the code below.  Is there an issue and how can it be resolved.

package com.javahowdoi.thread;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockIssue2 {
    private int i;
    private ReadWriteLock l = new ReentrantReadWriteLock();

    public int getI() {
        l.readLock().lock();
        int t = i;
        l.readLock().unlock();
        return t;
    }

    public void setI(int i) {
        l.writeLock().lock();
        this.i = i;
        l.writeLock().unlock();
    }

    public static void main(String[] args) {
        LockIssue li = new LockIssue();
        li.setI(100);
        System.out.println(li.getI());
    }
}

In both getI() and setI() methods, unlock the lock in try/finally section.  See example code here

Comments