有必要同步线程安全单例的方法(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日志命令行不显示单个合并修订版(svn log -r)(SVN log command line does not show single merged revision ( svn log -r ))[2022-03-27]
在我看来,你应该运行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日志不能递归地工作(svn log not working recursively)[2023-05-29]
在更深的目录中提交单个文件后,您需要在顶级运行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 show other than username)[2022-12-28]
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 ... -
为什么show log和svn update之间的版本#不同?(Why revision # different between show log and svn update?)[2023-09-08]
每个版本库都有Subversion版本 记录任何repo子树只显示修订版本,这会影响此子树(并忽略所有外部树) 以举例说明 \branches>svn up Updating '.': ... Updated to revision 5. 版本库HEAD是r5 branches>svn log -q ------------------------------------------------------------------------ r2 | lazybadger | 2013-11-28 00 ...