Java中的线程--并发库中的集合

  线程中的知识点基本都已经学完了,看看Java5并发库中提供的集合。。。

一、可堵塞队列

队列包含固定长度的队列和不固定长度的队列

ArrayBlockQueue中只有put()方法和take()方法才具有阻塞功能

1、阻塞队列的功能和效果,代码如下:

 1 import java.util.concurrent.ArrayBlockingQueue;
 2 import java.util.concurrent.BlockingQueue;
 3 
 4 /**
 5  * @className: BlockingQueueTest
 6  * @description: 可阻塞队列的应用
 7  * @author: ssc
 8  * @date: 2019年6月22日 上午11:07:22
 9  */
10 public class BlockingQueueTest {
11 
12     public static void main(String[] args) {
13         BlockingQueue queue = new ArrayBlockingQueue(3);
14         for (int i = 0; i < 2; i++) {
15             new Thread() {
16                 @Override
17                 public void run() {
18                     while (true) {
19                         try {
20                             Thread.sleep((long) (Math.random() * 10000));
21                             System.out.println(Thread.currentThread().getName() + "准备放数据");
22                             // 往对列中放数据
23                             queue.put(1);
24                             System.out.println(Thread.currentThread().getName() + "已经放了数据,队列目前有" + queue.size() + "个数据");
25                         } catch (Exception e) {
26                             e.printStackTrace();
27                         }
28                     }
29                 }
30             }.start();
31         }
32         
33         new Thread() {
34             @Override
35             public void run() {
36                 while (true) {
37                     try {
38                         Thread.sleep(10000);
39                         System.out.println(Thread.currentThread().getName() + "准备取数据");
40                         // 从队列中取出数据
41                         queue.take();
42                         System.out.println(Thread.currentThread().getName() + "已经取走数据,队列目前有" + queue.size() + "个数据");
43                     } catch (Exception e) {
44                         e.printStackTrace();
45                     }
46                 }
47             }
48         }.start();
49         
50     }
51 
52 }

2、堵塞队列来实现通知的功能

 代码示例如下:

 1 import java.util.concurrent.ArrayBlockingQueue;
 2 import java.util.concurrent.BlockingQueue;
 3 
 4 public class Business {
 5 
 6     private BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
 7     private BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
 8     
 9     // 这种写法是匿名构造方法 它在构造方法中优先级是最高的,所有构造方法之前首先执行
10     {
11         try {
12             queue2.put(1);
13         } catch (InterruptedException e) {
14             e.printStackTrace();
15         }
16     }
17     public void sub(int i) {
18 
19         try {
20             // 队列1 要放入值
21             queue1.put(1);
22             for (int j = 1; j <= 10; j++) {
23                 System.out.println("sub thread sequece of " + j + ", loop of " + i);
24             }
25             // 队列2 要把值取出来
26             queue2.take();
27             
28         } catch (InterruptedException e) {
29             e.printStackTrace();
30         }
31     }
32 
33     public void main(int i) {
34         try {
35             queue2.put(1);
36             for (int j = 1; j <= 100; j++) {
37                 System.out.println("main thread sequece of " + j + ", loop of " + i);
38             }
39             queue1.take();
40         } catch (InterruptedException e) {
41             e.printStackTrace();
42         }
43         
44     }
45 }

二、同步集合(并发集合)类

传统集合在并发访问时是有问题的

Java5中提供了一些同步集合类:

ConcurrentMap

CopyOnWriteArrayList

CopyOnWriteArraySet

就是这些集合类是线程安全的,即使在多线程的环境下,也不会存在并发问题,用法是和基本的集合类是一样的,只不过JDK中实现了线程同步的代码!!!

猜你喜欢

转载自www.cnblogs.com/ssh-html/p/11070683.html
今日推荐