Notes
  • Introduce
  • Go
    • Grammar
      • Basic
      • Goroutines & Channels
      • Test
    • System Library
      • Module
      • sync
      • context
      • net
    • Concurrency in Go
    • The Go Memory Model
    • Code Snippet
  • Rust
    • The Rust Programming Language
    • Rust by Example
  • JAVA
    • Preface
    • Grammar
      • Basic
      • Data Types
      • Operator
      • Exceptions
    • Class Libraries
      • Collection
      • Stream
      • IO
      • NIO
      • RMI
    • Concurrency
      • Preface
      • JMM
      • Synchronized & CAS
      • Deadlock
      • Thread
      • Lock & Condition
      • Utility Class
      • Thread-safe Collection
      • Atomic Class
      • Fork/Join
      • Concurrency Design Patterns
        • Immutable
        • Copy-on-Write
        • ThreadLocal
        • Multitheading If
        • Division
    • JVM
      • Class & Instance Initialization
      • Runtime Data Area
      • Garbage Collection
    • Web Container
      • Tomcat Architecture
      • Jetty Architecture
    • Spring
    • Tuning
      • Programming
  • Computer Science
    • Computer Organization
    • Algorithm
      • Complexity
      • Linear List
      • Sort
      • Binary Search
      • Skip List
      • Hash Table
      • Tree
      • Graph
      • String Matching
      • Bloom Filter
      • Greedy Algorithm
      • Divide and Conquer
      • Back Tracking
      • Dynamic Programming
    • Network Protocol
      • Pysical Layer
      • Data Link Layer
      • Network Layer
      • Transport Layer
      • Application layer
      • HTTP
      • HTTP/2 in Action
    • Operating System
      • Basic
      • System Initialization
      • Diagnostic Tools
      • CPU Diagnosis
      • Memory Diagnosis
      • Disk Diagnosis
      • Network Diagnosis
      • Monitor System
    • Design Patterns
      • UML
      • OOP
      • Principle
      • Refactoring & Specification
      • Creational
        • Singleton
        • Factory
        • Builder
        • Prototype
      • Structural
        • Proxy
        • Bridge
        • Decorator
        • Adapter
        • Facade
        • Composite
        • FlyWeight
      • Behavioral
        • Observer
        • Template Method
        • Strategy
        • State
        • Iterator
        • Chain of Responsibility
    • Distributed System
      • Protocol & Algorithm
      • Transcation
      • Theory
      • Resource Management
      • Scheduling
      • Computing
      • Message Queue
      • Cache
      • Consistent Hashing
  • database
    • InfluxDB
      • In-Memory Index
      • Meta
    • MySQL
      • SQL
      • Architecture
      • Log
      • Transaction
      • Indexing
      • Lock
      • Storage
    • Redis
    • Elasticsearch
      • Local Debug
    • HBase
    • Kafka
    • ZooKeeper
  • Reading
    • RocketMQ
    • 演说之禅
    • So Good They Can't Ignore You
    • 学会提问
    • Lecture
  • Other
    • v2ray
    • Kubernetes
    • Git
    • Maven
    • Anaconda And Conda
    • Fuck! Shit!
      • Remove Final by Reflection
      • Ingress Host
      • ExecuterService submit
  • Open source contribution
Powered by GitBook
On this page
  • 思路
  • 方案
  • 案例

Was this helpful?

  1. JAVA
  2. Concurrency
  3. Concurrency Design Patterns

Immutable

PreviousConcurrency Design PatternsNextCopy-on-Write

Last updated 6 years ago

Was this helpful?

思路

并发问题出现的根源是多个线程同时读写同一共享变量。若只有读,没有写,则不会存在并发问题。

所以解决并发问题最简单的办法就是使共享变量只读。可以使用不变性让对象不可写,即对象创建之后,状态不再发生变化。

方案

实现一个不可变类的方法:

  • 将 class 声明为 final。

  • 所有成员变量声明为 final,并且只允许存在只读方法。

  • 对于 getter 或其它可能会返回内部状态的方法,使用 copy-on-write 的方式。

  • 构造对象时,成员变量使用深度拷贝来初始化,而不是直接赋值。

  • 就算对象属性是 final 的,但是若属性是引用类型,那么属性的属性还是可能被修改,所以需要上面的 3、4点。

案例

Java 中的不可变类有和 Long、Integer、Double 等基础类的。

String 有字符串替换操作,看上去与不可变性矛盾,实际上它是通过创建一个新的字符串来达到替换的目的,并没有修改原对象的属性。

由此可见,不可变类需要提供类似修改的功能,方式是创建一个新的不可变对象。

但是这样可能会造成对象创建太多了,可以利用来减少对象的创建。

享元模式
String
包装类