关于java同步的问题;何时/如何/到什么程度 [英] questions around synchronization in java; when/how/to what extent

查看:25
本文介绍了关于java同步的问题;何时/如何/到什么程度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个多线程程序,但在同步的几个方面遇到了困难.我已经阅读了 oracle/sun 主页上的多线程教程,以及关于 SO 的一些问题,所以我相信我对同步是什么有了一个概念.但是,正如我所提到的,有几个方面我不太确定如何弄清楚.我在下面以明确问题的形式将它们表述出来:

问题 1: 我有一个单例类,其中包含用于检查有效标识符的方法.事实证明,此类需要保留集合以跟踪 2 种不同标识符类型之间的关联.(如果标识符这个词听起来很复杂;这些只是字符串).我选择实现两个 MultiValueMap 实例来实现这种多对多关系.我不确定这些集合是否必须是线程安全的,因为集合只会在创建单例类的实例时更新,但我注意到在文档中它说:><块引用>

请注意 MultiValueMap 不是同步的,也不是线程安全的.如果您希望从多个线程并发使用此映射,则必须使用适当的同步.当没有同步的并发线程访问该类时,该类可能会抛出异常.

谁能详细说明这种适当的同步"?究竟是什么意思?我真的不能在同步的 HashMap 上使用 MultiValueMap.decorate(),或者我误解了什么?

问题 2: 我有另一个类扩展 HashMap 来保存我的实验值,这些值在软件启动时被解析.此类旨在为我的分析提供适当的方法,例如 permutation()randomization()filtering(criteria) 等.由于我想尽可能地保护我的数据,这个类被创建和更新一次,上面提到的所有方法都返回新的集合.同样,我不确定这个类是否需要线程安全,因为它不应该从多个线程更新,但是这些方法肯定会从多个线程调用,并且为了安全"我已经添加synchronized 修饰符到我的所有方法.你能预见到任何问题吗?我应该注意哪些潜在问题?

谢谢,

解决方案

答案 1: 您的单例类不应将其内部使用的集合公开给其他对象.相反,它应该提供适当的方法来公开您想要的行为.例如,如果您的对象中有一个 Map,则没有公共或受保护的方法来返回该 Map.而是有一个方法,它接受一个键并返回 Map 中的相应值(以及一个设置键值的可选方法).如果需要,这些方法然后可以成为线程安全的.

注意,即使对于您不打算写入的集合,我也不认为您应该假设读取必须是线程安全的,除非它们被记录为如此.集合对象可能会维护一些您看不到的内部状态,但可能会在读取时被修改.

答案 2: 首先,我认为在这里使用继承不一定是正确的.我会有一个提供你的方法的类,并且有一个 HashMap 作为私有成员.只要您的方法不更改对象或 HashMap 的内部状态,它们就不必同步.

I am working on my first mutlithreaded program and got stuck about a couple of aspects of synchronization. I have gone over the multi-threading tutorial on oracle/sun homepage, as well as a number of questions here on SO, so I believe I have an idea of what synchronization is. However, as I mentioned there are a couple of aspects I am not quite sure how to figure out. I formulated them below in form of clear-cut question:

Question 1: I have a singleton class that holds methods for checking valid identifiers. It turns out this class needs to hold to collections to keep track of associations between 2 different identifier types. (If the word identifier sounds complicated; these are just strings). I chose to implement two MultiValueMap instances to implement this many-to-many relationship. I am not sure if these collections have to be thread-safe as the collection will be updated only at the creation of the instance of the singleton class but nevertheless I noticed that in the documentation it says:

Note that MultiValueMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.

Could anyone elaborate on this "appropriate synchronization"? What exactly does it mean? I can't really use MultiValueMap.decorate() on a synchronized HashMap, or have I misunderstood something?

Question 2: I have another class that extends a HashMap to hold my experimental values, that are parsed in when the software starts. This class is meant to provide appropriate methods for my analysis, such as permutation(), randomization(), filtering(criteria) etc. Since I want to protect my data as much as possible, the class is created and updated once, and all the above mentioned methods return new collections. Again, I am not sure if this class needs to be thread-safe, as it's not supposed to be updated from multiple threads, but the methods will most certainly be called from a number of threads, and to be "safe" I have added synchronized modifier to all my methods. Can you foresee any problems with that? What kind of potential problems should I be aware of?

Thanks,

解决方案

Answer 1: Your singleton class should not expose the collections it uses internally to other objects. Instead it should provide appropriate methods to expose the behaviours you want. For example, if your object has a Map in it, don't have a public or protected method to return that Map. Instead have a method that takes a key and returns the corresponding value in the Map (and optionally one that sets the value for the key). These methods can then be made thread safe if required.

NB even for collections that you do not intend to write to, I don't think you should assume that reads are necessarily thread safe unless they are documented to be so. The collection object might maintain some internal state that you don't see, but might get modified on reads.

Answer 2: Firstly, I don't think that inheritance is necessarily the correct thing to use here. I would have a class that provides your methods and has a HashMap as a private member. As long as your methods don't change the internal state of the object or the HashMap, they won't have to be synchronised.

这篇关于关于java同步的问题;何时/如何/到什么程度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆