并发编程-集合
普通的集合
List\Set\Map,并發的環境下,遍歷的過程中不容許更新操作(增刪改)
import java.util.ArrayList; import java.util.Iterator; import java.util.List;public class CollectionDemo {public static void main(String[] args) {List<User> list=new ArrayList<>();for (int i = 0; i < 20; i++) {User user = new User(i, "User" + i);list.add(user);}//java.util.ConcurrentModificationException // Iterator<User> it=list.iterator(); // while(it.hasNext()){ // User user=it.next(); // if("User6".equals(user.getName())) // list.remove(user); // }for (int i = 0; i < 20; i++){if(i % 2 == 0){User user = (User)list.get(i); // 每次刪除元素,后面的都會向前移動一個位置,3變成2,混亂了 list.remove(user);}}System.err.println(list);} }并發集合
非阻塞式集合(Non-Blocking Collection)? ConcurrentLinkedDeque
這類集合也包括添加和移除數據的方法。如果方法不能立即被執行,則返回null或拋出異常,但是調用這個方法的線程不會被阻塞。
實例
添加大量的數據到一個列表中;
從同一個列表中移除大量的數據。
?
import java.util.concurrent.ConcurrentLinkedDeque;public class CollectionDemo01 {public static void main(String[] args) throws InterruptedException{ConcurrentLinkedDeque<String> list=new ConcurrentLinkedDeque();//添加數據Thread[] add=new Thread[100];for (int i = 0; i < 100; i++) {add[i]=new Thread(()->{for (int j = 0; j < 10000; j++) {list.add(Thread.currentThread().getName()+":Element "+j);}});add[i].start();add[i].join();}System.out.println("after add size:"+list.size());//移除數據 Thread[] poll=new Thread[100];for (int i = 0; i < 100; i++) {poll[i]=new Thread(()->{for (int j = 0; j < 5000; j++) {list.pollLast();list.pollFirst();}});poll[i].start();poll[i].join();}System.out.println("after poll size:"+list.size());} }阻塞式集合(Blocking Collection) LinkedBlockingDeque
阻塞式集合(Blocking Collection):這類集合包括添加和移除數據的方法。當集合已滿或為空時,被調用的添加或者移除方法就不能立即被執行,那么調用這個方法的線程將被阻塞,一直到該方法可以被成功執行。
?
import java.util.Date; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit;public class BlockDequeDemo {public static void main(String[] args) {LinkedBlockingDeque<String> list=new LinkedBlockingDeque(3);Thread thread=new Thread(()->{for (int i = 0; i < 3; i++) {for (int j = 0; j < 5; j++) {String str=new String(i+":"+j);try {list.put(str.toString());} catch (InterruptedException e) {e.printStackTrace();}System.out.println("client:"+str+(new Date()));}}});thread.start();for (int i = 0; i < 5; i++) {for (int j = 0; j < 3; j++) {try {String str=list.take();System.out.println("main:take "+str+" size:"+list.size());} catch (InterruptedException e) {e.printStackTrace();}try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println("end");} }?
import com.sun.javafx.animation.TickCalculation;import java.util.Random; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit;public class CarDemo {public static void main(String[] args) {//創建SemaphoreSemaphore sp=new Semaphore(5);Thread[] car=new Thread[10];for (int i = 0; i < 10; i++) {car[i]=new Thread(()->{//請求許可try {sp.acquire();System.out.println(Thread.currentThread().getName()+"可以進停車場");} catch (InterruptedException e) {e.printStackTrace();}//使用資源try {int val= new Random().nextInt(10);TimeUnit.SECONDS.sleep(val);System.out.println(Thread.currentThread().getName()+"停留了"+val+"秒");} catch (InterruptedException e) {e.printStackTrace();}//離開(釋放資源)try {sp.release();System.out.println(Thread.currentThread().getName()+"離開停車場");} catch (Exception e) {e.printStackTrace();}},"car["+i+"]");car[i].start();}} }?
?
ArrayBlockingQueue?
ConcurrentHashMap?
ConcurrentLinkedQueue?
ConcurrentSkipListMap?
ConcurrentSkipListSet?
CopyOnWriteArrayList?
CopyOnWriteArraySet
轉載于:https://www.cnblogs.com/yintingting/p/11428575.html
總結