Home >Java >javaTutorial >Example analysis of Java thread safety strategy
Conditions that immutable objects need to meet
(1) After the object is created, its state cannot be modified
(2) All fields of the object are final types
(3) The object is created correctly (during object creation, this reference does not overflow)
For immutability Object, please refer to the String class in JDK
final keywords: class, method, variable
(1) Modified class: This class cannot be inherited, String class, wrapper class of basic type (such as Integer, Long, etc.) are all final types. Member variables in a final class can be set to final type as needed, but all member methods in a final class will be implicitly designated as final methods.
(2) Modification method: The locking method is not modified by the inherited class; efficiency. Note: A private method of a class will be implicitly designated as a final method
(3) Modified variables: basic data type variables (the value cannot be modified after it is initialized), reference type variables (cannot be modified after initialization) Then point to other objects)
provides a Collections class in the JDK, which provides many methods starting with unmodifiable, as follows:
Collections.unmodifiableXXX: Collection, List, Set , Map...
The XXX in the Collections.unmodifiableXXX method can be Collection, List, Set, Map...
At this time, pass the Collection, List, Set, and Map we created ourselves In the Collections.unmodifiableXXX method, it becomes immutable. At this time, if you modify the elements in Collection, List, Set, or Map, a java.lang.UnsupportedOperationException exception will be thrown.
In Google's Guava, there are many classes starting with Immutable, as follows:
ImmutableXXX, XXX can be Collection, List, Set, Map...
Note: To use Google's Guava, you need to add the following dependency packages in Maven:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency>
(1) Ad-hoc thread closure: program control implementation, worst case, ignore
(2) Stack closure: local variables, no concurrency issues
(3) ThreadLocal thread closure: a particularly good closure method
1. StringBuilder -> StringBuffer
StringBuilder: thread unsafe;
StringBuffer: thread unsafe;
String concatenation When it comes to multi-threaded operations, use StringBuffer to implement
In a specific method, define a string splicing object, which can be implemented using StringBuilder. Because when local variables are defined and used inside a method, the stack is closed. Only one thread will use the variable. It does not involve the operation of variables by multiple threads. Just use StringBuilder.
2. SimpleDateFormat -> JodaTime
SimpleDateFormat: Thread-unsafe, you can put the instantiation of its object into a specific time formatting method to achieve Thread safety
JodaTime: Thread safety
The thread-unsafe code example of SimpleDateFormat is as follows:
package io.binghe.concurrency.example.commonunsafe; import lombok.extern.slf4j.Slf4j; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; @Slf4j public class DateFormatExample { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); //请求总数 public static int clientTotal = 5000; //同时并发执行的线程数 public static int threadTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for(int i = 0; i < clientTotal; i++){ executorService.execute(() -> { try{ semaphore.acquire(); update(); semaphore.release(); }catch (Exception e){ log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } public static void update(){ try { simpleDateFormat.parse("20191024"); } catch (ParseException e) { log.error("parse exception", e); } } }
Just modify it to the following code.
package io.binghe.concurrency.example.commonunsafe; import lombok.extern.slf4j.Slf4j; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; @Slf4j public class DateFormatExample2 { //请求总数 public static int clientTotal = 5000; //同时并发执行的线程数 public static int threadTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for(int i = 0; i < clientTotal; i++){ executorService.execute(() -> { try{ semaphore.acquire(); update(); semaphore.release(); }catch (Exception e){ log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } public static void update(){ try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); simpleDateFormat.parse("20191024"); } catch (ParseException e) { log.error("parse exception", e); } } }
For JodaTime, you need to add the following dependency packages in Maven:
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9</version> </dependency>
The sample code is as follows:
package io.binghe.concurrency.example.commonunsafe; import lombok.extern.slf4j.Slf4j; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; @Slf4j public class DateFormatExample3 { //请求总数 public static int clientTotal = 5000; //同时并发执行的线程数 public static int threadTotal = 200; private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for(int i = 0; i < clientTotal; i++){ final int count = i; executorService.execute(() -> { try{ semaphore.acquire(); update(count); semaphore.release(); }catch (Exception e){ log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } public static void update(int i){ log.info("{} - {}", i, DateTime.parse("20191024", dateTimeFormatter)); } }
3. ArrayList, HashSet, HashMap and other Collections collection classes It is a thread-unsafe class
4. Check first and then execute: if(condition(a)){handle(a);}
Note: This way of writing is thread-unsafe! ! ! ! !
Two threads perform this operation at the same time and judge the if condition at the same time, and the a variable is shared by threads. If both threads meet the if condition, the two threads will execute the handle at the same time. (a) statement, at this time, the handle(a) statement may not be thread-safe.
The unsafe point is that in the two operations, even if the previous execution process is thread-safe, the subsequent process is also thread-safe, but the gap between the previous and later execution processes is not atomic, so it will also cause Thread unsafe issues.
In the actual process, when encountering the if(condition(a)){handle(a);} class, consider whether a is shared by threads. If it is shared by threads, it needs to be executed throughout the execution. Add a lock to the method, or ensure that the two operations (if judgment and code execution) before and after if(condition(a)){handle(a);} are atomic.
ArrayList: Thread-safe;
Vector: Synchronous operation , but thread unsafe situations may occur. Examples of thread unsafe code are as follows:
public class VectorExample { private static Vector<Integer> vector = new Vector<>(); public static void main(String[] args) throws InterruptedException { while (true){ for(int i = 0; i < 10; i++){ vector.add(i); } Thread thread1 = new Thread(new Runnable() { @Override public void run() { for(int i = 0; i < vector.size(); i++){ vector.remove(i); } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { for(int i = 0; i < vector.size(); i++){ vector.get(i); } } }); thread1.start(); thread2.start(); } } }
Stack: Inherited from Vector, first in, last out.
HashMap: thread unsafe;
HashTable: thread safe, pay attention to when using HashTable, Neither Key nor Value can be null;
Note: When traversing the collection, do not update the collection. When you need to delete elements in the collection, you can traverse the collection, mark the elements that need to be deleted first, and then perform the deletion operation after the collection traversal is completed. For example, the following sample code:
public class VectorExample3 { //此方法抛出:java.util.ConcurrentModificationException private static void test1(Vector<Integer> v1){ for(Integer i : v1){ if(i == 3){ v1.remove(i); } } } //此方法抛出:java.util.ConcurrentModificationException private static void test2(Vector<Integer> v1){ Iterator<Integer> iterator = v1.iterator(); while (iterator.hasNext()){ Integer i = iterator.next(); if(i == 3){ v1.remove(i); } } } //正常 private static void test3(Vector<Integer> v1){ for(int i = 0; i < v1.size(); i++){ if(i == 3){ v1.remove(i); } } } public static void main(String[] args) throws InterruptedException { Vector<Integer> vector = new Vector<>(); vector.add(1); vector.add(2); vector.add(3); //test1(vector); //test2(vector); test3(vector); } }
J.U.C表示的是java.util.concurrent报名的缩写。
ArrayList:线程不安全;
CopyOnWriteArrayList:线程安全;
写操作时复制,当有新元素添加到CopyOnWriteArrayList数组时,先从原有的数组中拷贝一份出来,然后在新的数组中进行写操作,写完之后再将原来的数组指向到新的数组。整个操作都是在锁的保护下进行的。
CopyOnWriteArrayList缺点:
(1)每次写操作都需要复制一份,消耗内存,如果元素特别多,可能导致GC;
(2)不能用于实时读的场景,适合读多写少的场景;
CopyOnWriteArrayList设计思想:
(1)读写分离
(2)最终一致性
(3)使用时另外开辟空间,解决并发冲突
注意:CopyOnWriteArrayList读操作时,都是在原数组上进行的,不需要加锁,写操作时复制,当有新元素添加到CopyOnWriteArrayList数组时,先从原有的集合中拷贝一份出来,然后在新的数组中进行写操作,写完之后再将原来的数组指向到新的数组。整个操作都是在锁的保护下进行的。
CopyOnWriteArraySet:线程安全的,底层实现使用了CopyOnWriteArrayList。
ConcurrentSkipListSet:JDK6新增的类,支持排序。可以在构造时,自定义比较器,基于Map集合。在多线程环境下,ConcurrentSkipListSet中的contains()方法、add()、remove()、retain()等操作,都是线程安全的。但是,批量操作,比如:containsAll()、addAll()、removeAll()、retainAll()等操作,并不保证整体一定是原子操作,只能保证批量操作中的每次操作是原子性的,因为批量操作中是以循环的形式调用的单步操作,比如removeAll()操作下以循环的方式调用remove()操作。如下代码所示:
//ConcurrentSkipListSet类型中的removeAll()方法的源码 public boolean removeAll(Collection<?> c) { // Override AbstractSet version to avoid unnecessary call to size() boolean modified = false; for (Object e : c) if (remove(e)) modified = true; return modified; }
所以,在执行ConcurrentSkipListSet中的批量操作时,需要考虑加锁问题。
注意:ConcurrentSkipListSet类不允许使用空元素(null)。
ConcurrentHashMap:线程安全,不允许空值
ConcurrentSkipListMap:是TreeMap的线程安全版本,内部是使用SkipList跳表结构实现
(1)ConcurrentSkipListMap中的Key是有序的,ConcurrentHashMap中的Key是无序的;
(2)ConcurrentSkipListMap支持更高的并发,对数据的存取时间和线程数几乎无关,也就是说,在数据量一定的情况下,并发的线程数越多,ConcurrentSkipListMap越能体现出它的优势。
注意:在非对线程下尽量使用TreeMap,另外,对于并发数相对较低的并行程序,可以使用Collections.synchronizedSortedMap,将TreeMap进行包装;对于高并发程序,使用ConcurrentSkipListMap提供更高的并发度;在多线程高并发环境中,需要对Map的键值对进行排序,尽量使用ConcurrentSkipListMap。
The above is the detailed content of Example analysis of Java thread safety strategy. For more information, please follow other related articles on the PHP Chinese website!