1、为什么需要提供StingBuilder 和StringBuffer
StingBuilder 线程不安全 StringBuffer 线程安全 效率低因为加了synchronized 在类中声明变量要要考虑多线程问题 方法内部使用局部变量堆栈封闭 没有并发问题 stringBuilder 长度变化有概率小于5000 package com.current.flame.commonUnsafe; import com.current.flame.annoations.ThreadSafe; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @ThreadSafe public class StringExample1 { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static StringBuilder stringBuilder = new StringBuilder(); // @NotThreadSafe private static StringBuffer stringBuffer = new StringBuffer(); // @ThreadSafe 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(); updateString(); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("stringBuilder.size=>{},stringBuffer.size=>{} ", stringBuilder.length(), stringBuffer.length()); } private static void updateString() { stringBuffer.append("1"); stringBuilder.append("1"); } } 方法内部使用局部变量堆栈封闭 没有并发问题 package com.current.flame.commonUnsafe; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j public class StringExample2 { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static StringBuffer stringBuffer = new StringBuffer(); // @ThreadSafe 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.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("stringBuffer.size=>{} ", stringBuffer.length()); } private static void update() { StringBuilder stringBuilder = new StringBuilder(); // @NotThreadSafe stringBuffer.append("1"); stringBuilder.append("1"); log.info("stringBuilder.size=>{}", stringBuilder.length()); } }1、代码1 simpleDateFormat
package com.current.flame.commonUnsafe; import com.current.flame.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @NotThreadSafe public class DateFormatExample1 { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("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++) { executorService.execute(() -> { try { semaphore.acquire(); updateDate(); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } private static void updateDate() { try { simpleDateFormat.parse("20180208"); } catch (Exception e) { log.error("parse exception=>{}", e); } } }2、代码二 SimpleDateFormat 堆栈封闭 线程安全
package com.current.flame.commonUnsafe; import com.current.flame.annoations.ThreadSafe; 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; /** * @author haoxiansheng */ @Slf4j @ThreadSafe 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(); // 使用堆栈封闭的方式 updateDateStackClose(); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } private static void updateDateStackClose() { try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); log.info("data=>{}", simpleDateFormat.parse("20180208")); } catch (ParseException e) { log.error("parse exception=>{}", e); } } }3、DateTimeFormatter
引入依赖 <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.5</version> </dependency> 代码 package com.current.flame.commonUnsafe; import com.current.flame.annoations.ThreadSafe; import lombok.extern.slf4j.Slf4j; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @ThreadSafe 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(); updateDateTime(count); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); } private static void updateDateTime(int count ) { log.info("count=>{},date=>{}", count, DateTime.parse("20180208", dateTimeFormatter)); } }1、ArrayList
package com.current.flame.commonUnsafe; import com.current.flame.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @NotThreadSafe public class ArrayListExample { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static List<Integer> list = new ArrayList<>(); 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(); updateArrayList(count); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("list.size=>{}", list.size()); } private static void updateArrayList(int count ) { list.add(count); } }2、HashSet
package com.current.flame.commonUnsafe; import com.current.flame.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @NotThreadSafe public class HashSetExample { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static Set<Integer> set = new HashSet<>(); 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(); updateHashSet(count); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("set.size=>{}", set.size()); } private static void updateHashSet(int count ) { set.add(count); } }3、HashMap
package com.current.flame.commonUnsafe; import com.current.flame.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author haoxiansheng */ @Slf4j @NotThreadSafe public class HashMapExample { // 请求总数 public static int clientTotal = 5000; // 允许同时执行的线程并发数 public static int threadTotal = 200; private static Map<Integer, Integer> map = new HashMap<>(); 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(); updateHashMap(count); semaphore.release(); } catch (Exception e) { log.info("e=>{}", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("map.size=>{}", map.size()); } private static void updateHashMap(int count ) { map.put(count, count); } }先检查再执行: if(condition(a)){handle(a)};