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();
        }
    }
}
 
     
    