jakubradzikowski97
jakubradzikowski97

Reputation: 37

The difference between read/write locks vs synchronized shown on basic code

In my homework I need to show the difference between read/write lock and 'synchronized' key word usage in code. I really don't know how to do it and what is the clear way to understand that difference. I also need to show the time difference between execution of the same task in both ways. Here is code that I tried (without synchronized though)

public class Main {

    public static void main(String[] args) {

        Number number = new Number(5);

        Thread t1 = new Thread(){
            public void run(){
                System.out.println(number.getData());
                number.changaData(10);
                System.out.println(number.getData());
            }};
            Thread t2 = new Thread(){
                public void run(){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(number.getData());
                    number.changaData(20);
                    System.out.println(number.getData());
                }};

                t2.start();
                t1.start();
    }
}


public class Number {

    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Lock readLock = rwl.readLock();
    private final Lock writeLock = rwl.writeLock();
    int value;

    public Number(int value) {
        this.value = value;
    }

    public int getData() {
        readLock.lock();
        try {
            return value;
        }
        finally {
            readLock.unlock();
        }
    }

    public int changaData(int change) {
        writeLock.lock();
        try {
            value = change;
            return value;
        }
        finally {
            writeLock.unlock();
        }
    }
}

Upvotes: 1

Views: 1589

Answers (1)

ejaksla
ejaksla

Reputation: 128

The difference between synchronized and read/write locks is such that when you are using synchronized it allows only one thread at a time to access. With read/write lock you can have many readers at the same time (given that there are no write locks already in) so you can get better concurrent performance under some cases, especially when having many reads here.

You should add many more threads that are accessing this object to test performance.

And you can simply count time between finishing and starting of operations to measure performance (in example - Long startTime = System.nanoTime();).

Read up here to see how to check whether thread has ended so you can measure execution time : How to know if other threads have finished?


edit to answer comment: Hey, my answer is a bit simplified (ok, very, as multithreading is hard) as I was writing this in between doing stuff, so, for now, I can link you with some other resources which provide more in-depth look.

very simple example as per your existing class:

class Number {

    private int value;

    public Number(int value) {
        this.value = value;
    }

    public synchronized int getValue() {
        return value;
    }

    public synchronized int changeData(int change) {
        value = change;
        return value;
    }
}

Upvotes: 4

Related Questions