多线程第四章-常见并发工具及阻塞队列
ConcurrentLinkedQueue无界队列
ConcurrentLinkedQueue是一个非阻塞的无界队列,内部的队列使用单向链表方式实现,其中有两个volatile类型的Node节点分别用来存放队列的首、尾节点。从下面的无参构造函数可知,默认头、尾节点都是指向item为null的哨兵节点。新元素会被插入队列末尾,出队时从队列头部获取一个元素
LinkedBlockingQueue
LinkedBlockingQueue是一个使用独占锁实现的阻塞队列
CountDownLatch
在日常开发中经常会遇到需要在主线程中开启多个线程去并行执行任务,并且主线程需要等待所有子线程执行完毕后再进行汇总的场景。在CountDownLatch出现之前一般都使用线程的join()方法来实现这一点,但是join方法不够灵活,不能够满足不同场景的需要,所以JDK开发组提供了CountDownLatch这个类
public class CountDownLatchDemo {
private static CountDownLatch countDownLatch = new CountDownLatch(2);
private static ExecutorService executor = Executors.newFixedThreadPool(2);
public static void main(String[] args) throws InterruptedException {
executor.submit(() -> {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
System.out.println("thread one executor end");
});
executor.submit(() -> {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
System.out.println("thread two executor end");
});
System.out.println("main thread wait child thread end");
countDownLatch.await();
System.out.println("main thread end");
executor.shutdown();
}
}
CurrentHashMap
public class CurrentHashMapDemo {
private Map<String, List<String>> map = new ConcurrentHashMap<>();
private ExecutorService executor = Executors.newCachedThreadPool();
private ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) {
CurrentHashMapDemo demo = new CurrentHashMapDemo();
// demo.put_1();
demo.put_2();
}
private void put_2() {
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device1");
list.add("device2");
List<String> oldList = map.putIfAbsent("topic1", list);
try {
if (null != oldList) {
oldList.addAll(list);
}
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device11");
list.add("device22");
List<String> oldList = map.putIfAbsent("topic1", list);
try {
if (null != oldList) {
oldList.addAll(list);
}
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device11");
list.add("device22");
List<String> oldList = map.putIfAbsent("topic2", list);
try {
if (null != oldList) {
oldList.addAll(list);
}
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.shutdown();
}
private void put_1() {
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device1");
list.add("device2");
map.put("topic1", list);
try {
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device11");
list.add("device22");
map.put("topic1", list);
try {
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.execute(() ->{
List<String> list = new ArrayList<String>();
list.add("device11");
list.add("device22");
map.put("topic2", list);
try {
System.out.println(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
executor.shutdown();
}
}

浙公网安备 33010602011771号