并发编程与高并发解决方案学习(同步容器)

ArrayList->Vector,Stack

HashMap->HashTable(key,value不能为null)

Collections.synchronizedXXX(List、Set、Map)


[java]  view plain  copy
  1. import com.mmall.concurrency.annoations.ThreadSafe;  
  2. import lombok.extern.slf4j.Slf4j;  
  3.   
  4. import java.util.List;  
  5. import java.util.Vector;  
  6. import java.util.concurrent.CountDownLatch;  
  7. import java.util.concurrent.ExecutorService;  
  8. import java.util.concurrent.Executors;  
  9. import java.util.concurrent.Semaphore;  
  10.   
  11. @Slf4j  
  12. @ThreadSafe  
  13. public class VectorExample1 {  
  14.     // 请求总数  
  15.     public static int clientTotal = 5000;  
  16.     // 同时并发执行的线程数  
  17.     public static int threadTotal = 200;  
  18.     private static List<Integer> list = new Vector<>();  
  19.     public static void main(String[] args) throws Exception {  
  20.         ExecutorService executorService = Executors.newCachedThreadPool();  
  21.         final Semaphore semaphore = new Semaphore(threadTotal);  
  22.         final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);  
  23.         for (int i = 0; i < clientTotal; i++) {  
  24.             final int count = i;  
  25.             executorService.execute(() -> {  
  26.                 try {  
  27.                     semaphore.acquire();  
  28.                     update(count);  
  29.                     semaphore.release();  
  30.                 } catch (Exception e) {  
  31.                     log.error("exception", e);  
  32.                 }  
  33.                 countDownLatch.countDown();  
  34.             });  
  35.         }  
  36.         countDownLatch.await();  
  37.         executorService.shutdown();  
  38.         log.info("size:{}", list.size());  
  39.     }  
  40.   
  41.     private static void update(int i) {  
  42.         list.add(i);  
  43.     }  
  44. }  

同步容器不一定是线程安全的,例如:

[java]  view plain  copy
  1. import com.mmall.concurrency.annoations.NotThreadSafe;  
  2.   
  3. import java.util.Vector;  
  4.   
  5. @NotThreadSafe  
  6. public class VectorExample2 {  
  7.     private static Vector<Integer> vector = new Vector<>();  
  8.     public static void main(String[] args) {  
  9.         while (true) {  
  10.             for (int i = 0; i < 10; i++) {  
  11.                 vector.add(i);  
  12.             }  
  13.             Thread thread1 = new Thread() {  
  14.                 public void run() {  
  15.                     for (int i = 0; i < vector.size(); i++) {  
  16.                         vector.remove(i);  
  17.                     }  
  18.                 }  
  19.             };  
  20.             Thread thread2 = new Thread() {  
  21.                 public void run() {  
  22.                     for (int i = 0; i < vector.size(); i++) {  
  23.                         vector.get(i);  
  24.                     }  
  25.                 }  
  26.             };  
  27.             thread1.start();  
  28.             thread2.start();  
  29.         }  
  30.     }  
  31. }  


[java]  view plain  copy
  1. import java.util.Iterator;  
  2. import java.util.Vector;  
  3.   
  4. public class VectorExample3 {  
  5.     // java.util.ConcurrentModificationException  
  6.     private static void test1(Vector<Integer> v1) { // foreach  
  7.         for(Integer i : v1) {  
  8.             if (i.equals(3)) {  
  9.                 v1.remove(i);  
  10.             }  
  11.         }  
  12.     }  
  13.   
  14.     // java.util.ConcurrentModificationException  
  15.     private static void test2(Vector<Integer> v1) { // iterator  
  16.         Iterator<Integer> iterator = v1.iterator();  
  17.         while (iterator.hasNext()) {  
  18.             Integer i = iterator.next();  
  19.             if (i.equals(3)) {  
  20.                 v1.remove(i);  
  21.             }  
  22.         }  
  23.     }  
  24.   
  25.     // success  
  26.     private static void test3(Vector<Integer> v1) { // for  
  27.         for (int i = 0; i < v1.size(); i++) {  
  28.             if (v1.get(i).equals(3)) {  
  29.                 v1.remove(i);  
  30.             }  
  31.         }  
  32.     }  
  33.   
  34.     public static void main(String[] args) {  
  35.         Vector<Integer> vector = new Vector<>();  
  36.         vector.add(1);  
  37.         vector.add(2);  
  38.         vector.add(3);  
  39.         test1(vector);  
  40.     }  
  41. }  

HashTable 线程安全

[java]  view plain  copy
  1. import com.mmall.concurrency.annoations.ThreadSafe;  
  2. import lombok.extern.slf4j.Slf4j;  
  3.   
  4. import java.util.HashMap;  
  5. import java.util.Hashtable;  
  6. import java.util.Map;  
  7. import java.util.concurrent.CountDownLatch;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.Semaphore;  
  11.   
  12. @Slf4j  
  13. @ThreadSafe  
  14. public class HashTableExample {  
  15.   
  16.     // 请求总数  
  17.     public static int clientTotal = 5000;  
  18.   
  19.     // 同时并发执行的线程数  
  20.     public static int threadTotal = 200;  
  21.   
  22.     private static Map<Integer, Integer> map = new Hashtable<>();  
  23.   
  24.     public static void main(String[] args) throws Exception {  
  25.         ExecutorService executorService = Executors.newCachedThreadPool();  
  26.         final Semaphore semaphore = new Semaphore(threadTotal);  
  27.         final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);  
  28.         for (int i = 0; i < clientTotal; i++) {  
  29.             final int count = i;  
  30.             executorService.execute(() -> {  
  31.                 try {  
  32.                     semaphore.acquire();  
  33.                     update(count);  
  34.                     semaphore.release();  
  35.                 } catch (Exception e) {  
  36.                     log.error("exception", e);  
  37.                 }  
  38.                 countDownLatch.countDown();  
  39.             });  
  40.         }  
  41.         countDownLatch.await();  
  42.         executorService.shutdown();  
  43.         log.info("size:{}", map.size());  
  44.     }  
  45.   
  46.     private static void update(int i) {  
  47.         map.put(i, i);  
  48.     }  
  49. }  

Collections提供的同步容器

[java]  view plain  copy
  1. import com.google.common.collect.Lists;  
  2. import com.mmall.concurrency.annoations.ThreadSafe;  
  3. import lombok.extern.slf4j.Slf4j;  
  4.   
  5. import java.util.Collections;  
  6. import java.util.List;  
  7. import java.util.Vector;  
  8. import java.util.concurrent.CountDownLatch;  
  9. import java.util.concurrent.ExecutorService;  
  10. import java.util.concurrent.Executors;  
  11. import java.util.concurrent.Semaphore;  
  12.   
  13. @Slf4j  
  14. @ThreadSafe  
  15. public class CollectionsExample1 {  
  16.   
  17.     // 请求总数  
  18.     public static int clientTotal = 5000;  
  19.   
  20.     // 同时并发执行的线程数  
  21.     public static int threadTotal = 200;  
  22.   
  23.     private static List<Integer> list = Collections.synchronizedList(Lists.newArrayList());  
  24.   
  25.     public static void main(String[] args) throws Exception {  
  26.         ExecutorService executorService = Executors.newCachedThreadPool();  
  27.         final Semaphore semaphore = new Semaphore(threadTotal);  
  28.         final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);  
  29.         for (int i = 0; i < clientTotal; i++) {  
  30.             final int count = i;  
  31.             executorService.execute(() -> {  
  32.                 try {  
  33.                     semaphore.acquire();  
  34.                     update(count);  
  35.                     semaphore.release();  
  36.                 } catch (Exception e) {  
  37.                     log.error("exception", e);  
  38.                 }  
  39.                 countDownLatch.countDown();  
  40.             });  
  41.         }  
  42.         countDownLatch.await();  
  43.         executorService.shutdown();  
  44.         log.info("size:{}", list.size());  
  45.     }  
  46.   
  47.     private static void update(int i) {  
  48.         list.add(i);  
  49.     }  
  50. }  

[java]  view plain  copy
  1. import com.google.common.collect.Sets;  
  2. import com.mmall.concurrency.annoations.ThreadSafe;  
  3. import lombok.extern.slf4j.Slf4j;  
  4.   
  5. import java.util.Collections;  
  6. import java.util.Set;  
  7. import java.util.concurrent.CountDownLatch;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.Semaphore;  
  11.   
  12. @Slf4j  
  13. @ThreadSafe  
  14. public class CollectionsExample2 {  
  15.   
  16.     // 请求总数  
  17.     public static int clientTotal = 5000;  
  18.   
  19.     // 同时并发执行的线程数  
  20.     public static int threadTotal = 200;  
  21.   
  22.     private static Set<Integer> set = Collections.synchronizedSet(Sets.newHashSet());  
  23.   
  24.     public static void main(String[] args) throws Exception {  
  25.         ExecutorService executorService = Executors.newCachedThreadPool();  
  26.         final Semaphore semaphore = new Semaphore(threadTotal);  
  27.         final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);  
  28.         for (int i = 0; i < clientTotal; i++) {  
  29.             final int count = i;  
  30.             executorService.execute(() -> {  
  31.                 try {  
  32.                     semaphore.acquire();  
  33.                     update(count);  
  34.                     semaphore.release();  
  35.                 } catch (Exception e) {  
  36.                     log.error("exception", e);  
  37.                 }  
  38.                 countDownLatch.countDown();  
  39.             });  
  40.         }  
  41.         countDownLatch.await();  
  42.         executorService.shutdown();  
  43.         log.info("size:{}", set.size());  
  44.     }  
  45.   
  46.     private static void update(int i) {  
  47.         set.add(i);  
  48.     }  
  49. }  
[java]  view plain  copy
  1. import com.mmall.concurrency.annoations.ThreadSafe;  
  2. import lombok.extern.slf4j.Slf4j;  
  3.   
  4. import java.util.Collections;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import java.util.concurrent.CountDownLatch;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.Semaphore;  
  11.   
  12. @Slf4j  
  13. @ThreadSafe  
  14. public class CollectionsExample3 {  
  15.   
  16.     // 请求总数  
  17.     public static int clientTotal = 5000;  
  18.   
  19.     // 同时并发执行的线程数  
  20.     public static int threadTotal = 200;  
  21.   
  22.     private static Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<>());  
  23.   
  24.     public static void main(String[] args) throws Exception {  
  25.         ExecutorService executorService = Executors.newCachedThreadPool();  
  26.         final Semaphore semaphore = new Semaphore(threadTotal);  
  27.         final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);  
  28.         for (int i = 0; i < clientTotal; i++) {  
  29.             final int count = i;  
  30.             executorService.execute(() -> {  
  31.                 try {  
  32.                     semaphore.acquire();  
  33.                     update(count);  
  34.                     semaphore.release();  
  35.                 } catch (Exception e) {  
  36.                     log.error("exception", e);  
  37.                 }  
  38.                 countDownLatch.countDown();  
  39.             });  
  40.         }  
  41.         countDownLatch.await();  
  42.         executorService.shutdown();  
  43.         log.info("size:{}", map.size());  
  44.     }  
  45.   
  46.     private static void update(int i) {  
  47.         map.put(i, i);  
  48.     }  
  49. }  
注意 如果使用了foreach或者iterator遍历我们的循环的时候,尽量不要在操作过程中做remove相关的更新操作,如果真有这个需求,我们建议,在遍历的过程中做好编辑,在遍历完之后对包含标记的元素做删除操作。
[java]  view plain  copy
  1. import java.util.Iterator;  
  2. import java.util.Vector;  
  3.   
  4. public class VectorExample3 {  
  5.   
  6.     // java.util.ConcurrentModificationException  
  7.     private static void test1(Vector<Integer> v1) { // foreach  
  8.         for(Integer i : v1) {  
  9.             if (i.equals(3)) {  
  10.                 v1.remove(i);  
  11.             }  
  12.         }  
  13.     }  
  14.   
  15.     // java.util.ConcurrentModificationException  
  16.     private static void test2(Vector<Integer> v1) { // iterator  
  17.         Iterator<Integer> iterator = v1.iterator();  
  18.         while (iterator.hasNext()) {  
  19.             Integer i = iterator.next();  
  20.             if (i.equals(3)) {  
  21.                 v1.remove(i);  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     // success  
  27.     private static void test3(Vector<Integer> v1) { // for  
  28.         for (int i = 0; i < v1.size(); i++) {  
  29.             if (v1.get(i).equals(3)) {  
  30.                 v1.remove(i);  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     public static void main(String[] args) {  
  36.   
  37.         Vector<Integer> vector = new Vector<>();  
  38.         vector.add(1);  
  39.         vector.add(2);  
  40.         vector.add(3);  
  41.         test3(vector);  
  42.     }  
  43. }  

猜你喜欢

转载自blog.csdn.net/friends99/article/details/79826489