首页 \ 问答 \ AtomicReference 困难和替代方案(AtomicReference difficulties and alternatives)

AtomicReference 困难和替代方案(AtomicReference difficulties and alternatives)

因此,在我为了好玩而编写的各种程序中,我遇到了并发修改异常。

在我试图解决这个问题的天真尝试中,我使用了某种并发收集的Atomicinstead。 我有点熟悉为什么这是套管错误。 本质上,ArrayList的各个元素不同步,可以通过不同的线程随意修改。


有人可以总结一下

  • 当我尝试对集合进行原子引用时会发生什么错误
  • 什么是对数组或列表的原子引用的合法用例
  • 有哪些更好的替代方案可以存储可供多线程使用的游戏实例


  • So in various programs that I have been writing for fun, I have come across concurrent modification exceptions.

    In my naive attempt to solve this problem I used an Atomicinstead of some sort of concurrent collection. I am somewhat familiar with why this is casing errors. Essentially the individual elements of the ArrayList are not synchronized and can be modified at whim by different threads.


    Could someone summarize for me

  • What errors occur when i try to make an atomic reference to a collection
  • What is a legitimate use case for an atomic reference to an Array or list
  • What are some better alternatives for storing instances for a game which can be used by multiple threads


  • 原文:https://stackoverflow.com/questions/38028892
    更新时间:2023-05-10 06:05

    最满意答案

    你定义的value不是你的类的实例字段,它更像是一个静态字段。 但是python不介意你是否从实例中访问这个字段。 因此,即使您从实例访问此字段,它也不是每个实例的不同列表。 基本上,每次调用该方法时都会追加到相同的列表中。

    你必须这样做

    class A(object):
    
        def __init__(self):
            self.value = []
    
        def method(self, new_value):
            self.value.append(new_value)
    

    现在您为每个实例创建了不同的列表。

    编辑 :让我试着解释当你使用str时会发生什么。

    class A(object):
    
        self.value = 'str'
    
        def method(self):
            self.value += '1'
    

    前面代码中的最后一行与此相同:

            self.value = self.value + '1'
    

    现在,这使得它更容易看到发生了什么。 首先,python从self.value获取值。 既然没有自定义的实例字段,这会给'str' 。 将'1'添加到该字段并将其设置为名为value实例字段。 这就像

    self.value = 'str1'
    

    这与您在__init__方法中设置实例字段(在我的第一个代码片段中)是一样的。

    self.value = []
    

    这是否清楚?


    The value you are defining is not an instance field for your class, its more like a static field. But python doesn't mind if you access this field from instances. So, even if you access this field from instances, it is not a different list for each instance. Basically, you are appending to the same list every time the method is called.

    You'll have to do this

    class A(object):
    
        def __init__(self):
            self.value = []
    
        def method(self, new_value):
            self.value.append(new_value)
    

    Now you have a different list created for each instance.

    EDIT: Let me try to explain what happens when you use a str.

    class A(object):
    
        self.value = 'str'
    
        def method(self):
            self.value += '1'
    

    That last line in the previous code is the same as this:

            self.value = self.value + '1'
    

    Now, this makes it abit easier to see what's going on. First, python gets the value from self.value. Since there is no instance field defined yet on self, this will give 'str'. Add '1' to that and sets it to the instance field called value. This is like

    self.value = 'str1'
    

    which is the same as you'd set an instance field in the __init__ method (in my first snippet of code).

    self.value = []
    

    Does that make it clear?

    相关问答

    更多
  • 我不推荐它,但你可以这样做: lapply(myList, 'attr<-', which='myname', value='myStaticName') 。 旧的for循环可能是执行此任务的最清晰的方法 - 或者在创建对象时在上游执行此分配。 for (i in seq_along(myList)) attr(myList[[i]], 'myname') <- 'myStaticName' 编辑: 正如@mnel在注释中指出的那样, data.table包中的setattr也是一个有效的选项,因为它通过 ...
  • 您需要使用以下语法:

    请参阅此处的Ember.js文档,具体说明: 绑定的类名和静态类名不能组合。 这里的工作示例 You need to use the following syntax:

    See the Ember.js docs here, specifically where it says: Bound class names an ...

  • 你会这样做的 $(".confirm-add-button").on("click", function() { var klass = $('.newbtnclassname').val(), text = $('.newbtntxt').val(), obj = {}; if (klass) obj['class'] = klass; if (text) obj['text'] = text; $(".drawing-area ...
  • 相关文章

    更多
  • ArrayList问题
  • 关于ArrayList的使用问题~~~~~~~
  • 一个ArrayList里是否可以添加不同的类,一个数组里是否可以添加不同类
  • 电子商务网站搜索架构方案
  • Spark,一种快速数据分析替代方案
  • 除了Hadoop:带你了解更多大数据解决方案
  • 研磨设计模式之外观模式(Facade)(解决方案)
  • 一个较完整的关键字过滤解决方案(中)
  • Hadoop分析之二元数据备份方案的机制
  • 基于Lucene/XML的站内全文检索解决方案:WebLucene 【转】
  • 最新问答

    更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。