简体   繁体   English

基本代码中显示了读/写锁定与同步之间的区别

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

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();). 而且,您可以简单地计算操作完成和开始之间的时间以衡量性能(例如-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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM