Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读/写锁与同步锁之间的差异在基本代码中显示_Java_Multithreading_Locks - Fatal编程技术网

Java 读/写锁与同步锁之间的差异在基本代码中显示

Java 读/写锁与同步锁之间的差异在基本代码中显示,java,multithreading,locks,Java,Multithreading,Locks,在我的家庭作业中,我需要说明代码中读/写锁定和“同步”关键字用法之间的区别。我真的不知道该怎么做,也不知道怎样才能清楚地理解这种差异。我还需要显示以两种方式执行相同任务之间的时间差。下面是我在没有同步的情况下尝试的代码 public class Main { public static void main(String[] args) { Number number = new Number(5); Thread t1 = new Thread(){

在我的家庭作业中,我需要说明代码中读/写锁定和“同步”关键字用法之间的区别。我真的不知道该怎么做,也不知道怎样才能清楚地理解这种差异。我还需要显示以两种方式执行相同任务之间的时间差。下面是我在没有同步的情况下尝试的代码

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

同步锁和读/写锁之间的区别在于,当您使用同步锁时,一次只允许一个线程访问。使用读/写锁,您可以同时拥有多个读卡器,因为其中没有写锁,因此在某些情况下,您可以获得更好的并发性能,尤其是在这里有多个读卡器时

您应该添加更多访问此对象的线程以测试性能

在示例-Long startTime=System.nanoTime;中,您可以简单地计算操作完成和开始之间的时间来衡量性能

请阅读此处,了解如何检查线程是否已结束,以便测量执行时间:

编辑以回答评论: 嘿,我的答案有点简化了,好吧,非常简单,因为多线程是很难的,因为我写这篇文章是在做一些事情之间,所以,现在,我可以把你和一些其他资源链接起来,提供更深入的了解


同步锁和读/写锁之间的区别在于,当您使用同步锁时,一次只允许一个线程访问。使用读/写锁,您可以同时拥有多个读卡器,因为其中没有写锁,因此在某些情况下,您可以获得更好的并发性能,尤其是在这里有多个读卡器时

您应该添加更多访问此对象的线程以测试性能

在示例-Long startTime=System.nanoTime;中,您可以简单地计算操作完成和开始之间的时间来衡量性能

请阅读此处,了解如何检查线程是否已结束,以便测量执行时间:

编辑以回答评论: 嘿,我的答案有点简化了,好吧,非常简单,因为多线程是很难的,因为我写这篇文章是在做一些事情之间,所以,现在,我可以把你和一些其他资源链接起来,提供更深入的了解


您是否在实践中尝试过阅读Java并发中的主题?您是否在实践中尝试过阅读Java并发中的主题?谢谢您的回答!:如何在上面的示例中使用“synchronized”?谢谢!正是我所需要的谢谢你的回答如何在上面的示例中使用“synchronized”?谢谢!正是我需要的
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;
    }
}