使用ConcurrentHashSet时的线程安全引用分配 [英] Thread safe reference assignment when working with ConcurrentHashSet

查看:103
本文介绍了使用ConcurrentHashSet时的线程安全引用分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在DTO中使用ConcurrentHashSet感到有些困惑.一次有多个线程可以访问此DTO.

首例

 公共类LogDTO {私人Set< String>人= ConcurrentHashMap.newKeySet();公共无效setPerson(Set< String>人){this.person =人;}公共Set< String>getPerson(){返回this.person;}} 

这是否使线程安全?

第二种情况

 公共类LogDTO {私有volatile Set< String>人;公共无效setPerson(Set< String>人){this.person =人;}公共Set< String>getPerson(){返回this.person;}} 

还是我必须使用AtomicReference?

第三种情况

 公共类LogDTO {私有AtomicReference< Set< String>>ref =新的AtAtomicReference();公共无效setPerson(Set< String>人){this.ref.set(person);}公共Set< String>getPerson(){返回this.ref.get();}} 

如果要填充一个全新的HashSet然后将其分配给现有变量,那么使用volatile更好吗?

解决方案

第一种情况

它不是线程安全的,ConcurrentHashMap仅在使用HashMap api时提供线程安全,这意味着在HashMap中添加和删除内容.在更改对HashMap的引用时,它不会提供线程安全性;如果您想到的话,则更改对HashMap的引用与HashMap的实现方式无关.

第二和第三种情况

那些情况是线程安全的,在这些情况下,您更改了分配和检索HashMap引用的实现方式以允许线程安全.

关于使用volatile还是AtomicReference,您可以阅读

给您tl; dr:AtomicReference具有更多功能,但使用额外的内存来提供该功能,我个人认为,如果您不关心内存,请继续使用AtomicReference,它更具可读性,并且您不知道何时会发现自己需要其他功能

A bit confused about the usage of ConcurrentHashSets in DTOs. This DTO is accessed by many threads at a time.

First Case

 public class LogDTO {
      private Set<String> person = ConcurrentHashMap.newKeySet();

      public void setPerson(Set<String> person) {
       this.person = person;
      }

      public Set<String> getPerson() {
       return this.person;
      }
    }

Does this give Thread Safety?

Second Case

 public class LogDTO {

     private volatile Set<String> person;
      public void setPerson(Set<String> person) {
       this.person = person;
      }

      public Set<String> getPerson() {
       return this.person;
      }
 }

or do I have to use AtomicReference?

Third Case

 public class LogDTO {

     private AtomicReference<Set<String>> ref = new AtAtomicReference<>();

      public void setPerson(Set<String> person) {
       this.ref.set(person);
      }

      public Set<String> getPerson() {
       return this.ref.get();
      }
 }

If to populate an entirely new HashSet then assigning it to the existing variable, then is it better to use volatile?

解决方案

First Case

it is not thread safe, ConcurrentHashMap only provides thread safety when working with the HashMap api meaning adding and removing things from the HashMap. it does not provide thread safety when changing the reference to the HashMap, if you think of it, changing the reference to the HashMap has no connection to how the HashMap is implemented.

Second and Third cases

Those cases are thread safe, in those cases you changed the way assigning and retrieving the HashMap reference is implemented to allow thread safety.

in regards to whether to use volatile or AtomicReference you can read this

to give you the tl;dr: AtomicReference has more functionality but uses additional memory to provide that functionality, my personal opinion is that if you are not that concerned with memory go ahead and use AtomicReference it is more readable and you dont't know when you will find your self needing additional functionality.

这篇关于使用ConcurrentHashSet时的线程安全引用分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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