java - 有效的 Java 项目 66 : Why to synchronize both read and write methods?

标签 java multithreading effective-java

在 Effective Java -> Item 66 中,Joshua 强调需要同步读写操作以避免 active 失败。

在这个特定示例中,我认为同步写入方法是多余的。即使删除同步写入方法后,程序运行和终止也没有任何问题。 需要同步才能看到对象的一致状态,这是通过同步读取方法实现的。

请让我知道您对此的看法。

import java.util.concurrent.TimeUnit;

public class StopThread {

private static boolean stopRequested;

public static void main(String[] args) throws InterruptedException {
    new Thread(new Runnable() {

        @Override
        public void run() {
            int i = 0;
            while (!isStopRequested())
                i++;
        }
    }).start();
    ;
    TimeUnit.SECONDS.sleep(1);
    setStopRequested(true);
}

private static synchronized boolean isStopRequested() {
    return stopRequested;
}

private static void setStopRequested(boolean stopRequested) {
    StopThread.stopRequested = stopRequested;
}
}

最佳答案

您提到的示例最适合演示如何在没有同步(或 volatile )的情况下无法保证何时将线程本地内存中的值刷新到主内存,但是这个例子当然不是最适合演示“读写并发问题”的。

我想你可能误解了示例的目的,目的是为了展示在没有同步的情况下线程通信的效果。阅读以下同一项目 #66 的摘录:

The actions of the synchronized methods in StopThread would be atomic even without synchronization. In other words, the synchronization on these methods is used solely for its communication effects, not for mutual exclusion.

你认为它工作的原因是因为在没有同步的情况下,当线程本地内存中的值被刷新到主内存时,JVM 没有“保证”,这意味着它可能根本不冲洗或可能冲洗但不能保证“何时”。当你运行它时,值会被刷新,但它不一定总是被刷新,所以这就是“保证”出现的地方,如果你使用同步(或 volatile,取决于场景),那么 JVM 保证“< strong>happens-before”关系,这只是保证将值从线程本地内存刷新到主内存将“发生在”任何线程可以从主内存读取值之前。

在没有同步的情况下检查读写相关并发问题的影响的一个更好的例子可能是流行的银行账户借方贷方示例,下面是快速示例:

public class AccountDebitCredit {

    private int accountBalance = 100;

    public static void main(String[] args) throws InterruptedException {
        final AccountDebitCredit accountDebitCredit = new AccountDebitCredit();

        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    // if you remove synchronization from t1 and t2, then there would be concurrency issues.
                    synchronized (accountDebitCredit) {
                        accountDebitCredit.accountBalance = accountDebitCredit.accountBalance + 100;
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    // if you remove synchronization from t1 and t2, then there would be concurrency issues.
                    synchronized (accountDebitCredit) {
                        accountDebitCredit.accountBalance = accountDebitCredit.accountBalance - 100;
                    }
                }
            }
        });

        System.out.println(accountDebitCredit.accountBalance);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(accountDebitCredit.accountBalance);
    }

}

关于java - 有效的 Java 项目 66 : Why to synchronize both read and write methods?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491517/

相关文章:

java - 如何让布局上下移动

java - Hibernate session 实例

c++ - 使用pthreads实现线程池

java - 进程运行的线程数大于tomcat maxThreads

c++ - 为什么在重新执行我的多线程代码后输出不一样?

java - 为 future 可能的可扩展性保留一个非最终值类

java - Java规则引擎的优缺点

java - Java中根据3个自定义字符串对ArrayList进行排序

java : Function objects as strategies

java - Joshua Bloch 在 Effective Java 中解释的枚举类型