首页 \ 问答 \ 有必要同步线程安全单例的方法(Is necessary synchronize thread safe singleton's methods)

有必要同步线程安全单例的方法(Is necessary synchronize thread safe singleton's methods)

假设我有一个单身人士:

public class MySinglton {

    private static volatile MySinglton s;
    private int x; 

    public static MySinglton getInstance() {
        if (s != null) return s;
        synchronized (MySinglton.class) {
            if (s == null) {
                s = new MySinglton();
            }
        }
        return s;
    }

    public void setX(int x){
        this.x = x;
    }

} 

好的,方法getInstance是线程安全的。 我的问题是。 是否有必要修改方法setX,或者它是线程安全的,因为getInsatnce方法是线程安全的。 如果不是更好。

    synchronized public void setX(int x){
        this.x = x;
    }

    public void setX(int x){
        synchronized (this) {
            this.x = x;
        }
    }

或最后

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public void setX(int x){
        readWriteLock.readLock().lock();
        this.x = x;
        readWriteLock.readLock().unlock();
    }

Let say that I have a singleton:

public class MySinglton {

    private static volatile MySinglton s;
    private int x; 

    public static MySinglton getInstance() {
        if (s != null) return s;
        synchronized (MySinglton.class) {
            if (s == null) {
                s = new MySinglton();
            }
        }
        return s;
    }

    public void setX(int x){
        this.x = x;
    }

} 

Ok, method getInstance is thread safe. My question is. Is it necessary to modify method setX, or it is thread safe because getInsatnce method is thread safe. If it is not what is better.

    synchronized public void setX(int x){
        this.x = x;
    }

    public void setX(int x){
        synchronized (this) {
            this.x = x;
        }
    }

or finally

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public void setX(int x){
        readWriteLock.readLock().lock();
        this.x = x;
        readWriteLock.readLock().unlock();
    }

原文:https://stackoverflow.com/questions/28928886
更新时间:2023-06-15 12:06

最满意答案

好吧修好了。 我的authz文件丢失* = r


Ok got it fixed. My authz file was missing * = r

相关问答

更多
  • svn diff有几种用法 一个,如果他们是 diff OLD-URL[@OLDREV] NEW-URL[@NEWREV] 当项目的目录结构发生变化时,它比-r选项更好。 尝试这个: svn diff https://svnhost.localdomain/project1/conf/files@2 https://svnhost.localdomain/project1/conf/files@4 svn diff has several forms of usage one if them is d ...
  • 默认情况下,svn:protocol不进行任何身份验证,因此不识别远程用户。 由于TortoiseSVN不需要进行身份验证,因此不会记录任何用户。 SVN关键字不必对此做很多事情:如果将$ Author $放入文件中,它将扩展为提交的作者,而不是设置用户。 在关键字语法中,不支持冒号。 因此,如果要使用svnserve进行身份验证,则应首先将anon-access设置为none或read,以强制执行身份验证提示。 然后,您需要设置密码数据库; svnserve仅支持CRAM-MD5内置版。 我建议您先尝试使 ...
  • 这个问题与我对什么svn log显示不了解有关。 没有提供其他参数,它从日志的工作副本输出日志消息,而不是从实际的存储库。 因此,发出svn update将使工作副本与存储库保持最新,然后svn log将反映所有最近的提交。 咄! ;-) The problem had to do with my poor understanding of what svn log was showing. With no other arguments supplied, it outputs log messages ...
  • 当我打开Zend Studio 8时,它创建了另一个工作区。 我在那里开了一个新的SVN项目,并且工作正常。 然后我决定将工作区更改为原始工作区,并配置所有项目。 没有数字。 所以我打开了Zend Studio 9,将工作区更改为用8创建的工作区,并显示了数字。 所以问题出在工作区,我可能需要重建... When I opened Zend Studio 8, it created another workspace. I opened a new SVN project there, and that w ...
  • 在我看来,你应该运行svn log --verbose --revision 60524 https://svn.example.com/repos/或svn log --verbose --revision 60524 https://svn.example.com/repos/branches或指定实际受r60524影响的另一个分支。 我可能会误解这个问题,但看起来您提供的URL不正确。 修订版r60524未触及/Release分支,因此命令不显示任何内容。 因此,我建议对存储库的根目录运行命令并添加- ...
  • 好吧修好了。 我的authz文件丢失* = r Ok got it fixed. My authz file was missing * = r
  • 在更深的目录中提交单个文件后,您需要在顶级运行svn update 。 如果不这样做,您将拥有一个混合修订工作副本,而顶级(您运行svn log的地方)没有看到最新版本号。 You need to run svn update at the top level after committing the single file at a deeper directory. Without doing so, you have a mixed-revision working copy and the top ...
  • 是的,TortoiseSVN缓存日志消息并更改路径。 您应该能够通过TortoiseSVN设置|清除缓存 日志缓存| 缓存存储库 。 阅读TortoiseSVN手册| 日志缓存和缓存存储库以获取更多信息。 注意:日志缓存是一个非常有用的功能。 请勿禁用它,并在输入日志消息时小心。 Yes, TortoiseSVN caches the log messages and changed paths. You should be able to clear the cache via TortoiseSVN S ...
  • SVN无法自行显示。 您需要编写自己的程序来解析svn log的输出并查询AD以进行转换。 如果您使用--xml开关,这将更容易,因为您不必执行尽可能多的文本解析操作 - 您可以使用XML解析库,这将更容易。 附注:贵公司的新政策让我感到难过。 SVN can't display it on its own. You will need to write your own program to parse the output of svn log and query AD to do the transl ...
  • 每个版本库都有Subversion版本 记录任何repo子树只显示修订版本,这会影响此子树(并忽略所有外部树) 以举例说明 \branches>svn up Updating '.': ... Updated to revision 5. 版本库HEAD是r5 branches>svn log -q ------------------------------------------------------------------------ r2 | lazybadger | 2013-11-28 00 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)