Thread safety policy

An object whose state cannot be changed after creation is called immutable. Immutable objects are inherently thread-safe. Constants (variables) of immutable objects are created in constructors, and since their state can never be changed, they are always thread-safe.

Conditions that an immutable object must meet

  • The state of an object cannot be changed after it is created.
  • All fields of the object are of finA type.
  • The object was created correctly (the this reference did not escape during object creation)

In concurrent programming practice, this reference escape (“this”escape) is when an object’s this reference is published before it has been constructed

final

Final keywords: class, method, variable.

  • decorator

Member properties ina final class can be set to final if necessary, but all member methods ina final class are implicitly specified as final. It is generally not recommended to set a class to final.

  • Modification methods

Locking methods cannot be modified by inherited classes

  • Modify variables

Basic datatype variables that cannot be modified after initialization. Reference type variables cannot point to other references after initialization.

// Thread unsafe package com.rumenz.task.single; import com.google.common.collect.Maps; import java.util.Map; public class ImmutableExample1 { private final static Integer a=1; private final static Integer b=2; Private final static map <Integer,Integer> map= maps.newhashMap (); private final static map <Integer,Integer> map= maps.newhashmap (); static { map.put(1, 1); } public static void main(String[] args) { //a=10; //b=20; Map.put (2, 2); // Thread is not safe}}Copy the code

Collections

Java provides the Collections utility class, which provides a variety of methods that are not allowed to be modified.

The Collections. UnmodifiableXXX: Collection, List, Set, Map…

// Thread-safe package com.rumenz.task.single; import com.google.common.collect.Maps; import java.util.Collections; import java.util.Map; public class ImmutableExample1 { private final static Integer a=1; private final static Integer b=2; Private static map <Integer,Integer> map= maps.newhashMap (); private static map <Integer,Integer> map= maps.newhashmap (); static { map.put(1, 1); / / processing map can not be modified after the map = Collections. UnmodifiableMap (map); } public static void main(String[] args) {map.put(2, 2); } } Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableMap.put(Collections.java:1457) at com.rumenz.task.single.ImmutableExample1.main(ImmutableExample1.java:31)Copy the code

Collections.unmodifiableMapThe source code

public class Collections { public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) { return new UnmodifiableMap<>(m); } private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable { @Override public boolean remove(Object key, Object value) { throw new UnsupportedOperationException(); } @Override public boolean replace(K key, V oldValue, V newValue) { throw new UnsupportedOperationException(); }}}Copy the code

Guava

Google’s Guava offers Collections similar to those in Java

ImmutableXXX: Collection, List, Set, Map…

<dependency> <groupId>com.google.guava</groupId> <artifactId> <version>23.0</version> </dependency>Copy the code
Public class ImmutableExample3 {private Final static ImmutableList<Integer> list = ImmutableList. Of (1, 2, 3); private final static List<Integer> lists = ImmutableList.of(1, 2, 3); private final static ImmutableSet set = ImmutableSet.copyOf(list); private final static ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 2, 3, 4); private final static ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder() .put(1, 2).put(3, 4).put(5, 6).build(); public static void main(String[] args) { System.out.println(map2.get(3)); }}Copy the code