Java多线程编程 - 原子操作(Java multi-thread programming - atomic operation)
我正在学习使用Java进行多线程编程。 这是我得到的困惑。
class Cache<K, V> { private ConcurrentMap<K, V> cache; private ConcurrentLinkedQueue<K> lru; public Cache () { // initiate cache and lru } public put (K key, V value) { // some pre-processing synchronized (this) { cache.put(key, value); lru.add(key); } // some post-processing } }
这是一个非常简单的缓存,其中包含最近最少使用的记录(lru)。 显然我需要使这两个操作成为原子。 否则,缓存和lru的状态很可能会有所不同。
现在假设我想要一个计时器任务来清理缓存,说它会清理一半的缓存。 我的问题是我的代码是否确保这两个操作(put cache和add lru)对于clean任务看起来是原子的? 我可以做以下的事情:
class CleanTask { Cache cache; // the reference of Cache public void run () { // some pre-processing for (int i = 0; i < n; i++) { // Just suppose I need remove n element synchronized (XXX) { cache.getCache().remove(cache.getLru().poll()); } } } }
我应该把什么放在XXX?
非常感谢!!!
I am on my way learning multi-thread programming with Java. Here is a confusion I got.
class Cache<K, V> { private ConcurrentMap<K, V> cache; private ConcurrentLinkedQueue<K> lru; public Cache () { // initiate cache and lru } public put (K key, V value) { // some pre-processing synchronized (this) { cache.put(key, value); lru.add(key); } // some post-processing } }
Here is some very simple cache with a least recently used record (lru). Obviously I need to make these two operations atomic. Otherwise it's very likely the state of cache and lru would be different.
Now assume I want to have a timer task to clean the cache, say it will clean half the cache. My question is does my code above ensure these two operations (put cache and add lru) appear atomic to the clean task? Can I do something like below:
class CleanTask { Cache cache; // the reference of Cache public void run () { // some pre-processing for (int i = 0; i < n; i++) { // Just suppose I need remove n element synchronized (XXX) { cache.getCache().remove(cache.getLru().poll()); } } } }
And what should I put in XXX?
Thanks a lot!!!
原文:https://stackoverflow.com/questions/14116572
最满意答案
当它在一个解决方案上运行时,它包含所有引用的DLL,然后过滤掉任何不属于您的解决方案项目的DLL。 因此,如果您使用此命令,则应包含引用的DLL:
metrics.exe / f: drive:\ solutiondirectory * .dll /o:results.xml / acm:无
编辑:我通过.Net Reflector运行Metrics.exe,结果发现有一个比较模式的开关,可以让你的例子成功。 “/ acm:”开关的有效值为:
- 没有
- StrongNameIgnoringVersion
- 强名称
When it runs it over a solution it includes all the referenced DLL's then filters out any that aren't part of your solutions projects. So if you used this command it should include the referenced DLLs:
metrics.exe /f:drive:\solutiondirectory*.dll /o:results.xml /acm:None
Edit: I ran Metrics.exe through .Net Reflector and it turns out that there is a switch for the compare mode that allows your example to succeed. Valid values for the "/acm:" switch are:
- None
- StrongNameIgnoringVersion
- StrongName
相关问答
更多-
发生这种情况是因为控件类的构造函数也在设计时运行。 其中设置了DataSource 和 Items属性,它们的值将像通常属性一样被序列化。 所以你在Designer.cs代码中看到它们。 通常是看不见的,而不是像它在这种情况下那样产生异常。 您告诉设计器序列化程序不要使用[DesignerSerializationVisibility]属性执行此操作: [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] publ ...
-
您必须创建通用应用程序或Windows运行时应用程序才能访问该命名空间。 选择其中任何一个(请注意,某些项目类型可以启用另一个功能子集): You have to create an Universal app or Windows Runtime app to be able to access that namespace. Pick any of these (note that some project types can have another subset of features enable ...
-
当它在一个解决方案上运行时,它包含所有引用的DLL,然后过滤掉任何不属于您的解决方案项目的DLL。 因此,如果您使用此命令,则应包含引用的DLL: metrics.exe / f: drive:\ solutiondirectory * .dll /o:results.xml / acm:无 编辑:我通过.Net Reflector运行Metrics.exe,结果发现有一个比较模式的开关,可以让你的例子成功。 “/ acm:”开关的有效值为: 没有 StrongNameIgnoringVersion 强名称 ...
-
组件没有显示出来,因为它们在当前的测试运行中没有加载。 添加一些在每个其他程序集中使用类型的简单测试。 The assemblies are not showing up as they are not loaded during the current test run. Add some simple tests that use a type in each of the other assemblies.
-
您需要Visual Studio ULTIMATE才能创建代码映射。 http://msdn.microsoft.com/en-us/library/jj739835.aspx You need Visual Studio ULTIMATE to create Code Maps. http://msdn.microsoft.com/en-us/library/jj739835.aspx
-
是的,这很有可能。 它被称为构建服务器。 TeamCity,CC.Net和TFS(以及更多)都提供了编译代码的日常工作,并执行诸如计算度量和代码分析等其他内容。 Yes, it's very possible. It's called a build server. TeamCity, CC.Net and TFS (and more) all offer the abiity to run a job each day that compiles your code and does other thin ...
-
Visual Studio:未为签名的程序集生成代码覆盖率(Visual Studio: Code coverage not generated for signed assemblies)[2022-01-26]
无法检测已签名程序集的原因是检测过程会更改程序集的内容。 这将改变程序集的散列,从而使数字签名无效。 最好的方法是取消分析运行的程序集。 The reason you can not instrument a signed assembly is that the instrumentation process changes the contents of the assembly. This would change the hash of the assembly hence invalidating ... -
正如Lucas在评论中指出的那样, Assembly.LoadFrom仅适用于.NET程序集,我试图加载的程序集是COM dll。 我最终要做的是将它们添加到一个项目中,该项目生成了dop的Interop版本,然后我将其添加到GAC中,以便我们最初想要使用它的程序可以访问它。 感谢发表在评论中的每个人,我都知道这个解决方案。 As Lucas pointed out in the comments, Assembly.LoadFrom only works on .NET assemblies and th ...
-
Visual Studio 2015 - 自动解析缺失的程序集和使用(Visual Studio 2015 — autoresolve missing assemblies and usings)[2022-09-19]
VS本身无法解决丢失的程序集。 在这里,您可能缺少System.Windows.Forms程序集。 VS只能解决using s缺失的问题。 正如评论中所述,ReSharper还能够解析装配体。 VS on its own is not able to resolve missing assemblies. Here, you're probably missing System.Windows.Forms assembly. VS can only resolve missing usings. As to ... -
似乎VS Code现在可以使用新的扩展名ms-vscode来实现C#支持。 以前建议使用Legacy C#支持 Unity3D。 我不确切知道此建议是否仍然有效,但我尝试卸载旧版并安装正常版本。 经过一些后台工作后,OmniSharp解析了我的文件,现在它可以从程序集中打开类! 看看这张图片,它已经打开了AnimationCurve类作为[元数据]! 我现在正在尝试使用Unity3D 5.5.0b2 beta,因此我不确定这是否适用于Unity3D的生产版本。 顺便说一句,我还从项目中删除了dotBunny ...