Java多线程之~~~线程安全容器的非阻塞容器



Java多线程之~~~线程安全容器的非阻塞容器

在并发编程中,会经常遇到使用容器。但是如果一个容器不是线程安全的,那么他在多线程的插入或者删除的过程

 

中就会出现各种问题,就是不同步的问题。所以JDK提供了线程安全的容器,他能保证容器在多线程的情况下安全的插

 

入和删除。当然,线程安全的容器分为两种,第一种为非阻塞似的,非阻塞的意思是当请求一个容器为空或者这个请求

 

不能执行的时候,就会报出异常,第二种阻塞的意思是,不能执行的命令不会报出异常,他会等待直到他能执行。下面

 

我们实现一个例子,这个例子就是多个线程去大量的插入容器数据,而另一个线程去大量的pop出数据。

 

 


 

代码如下

 

  1. package com.bird.concursey.charpet9;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. public class AddTask implements Runnable {
  4.     private ConcurrentLinkedDeque<String> list;
  5.     public AddTask(ConcurrentLinkedDeque<String> list) {
  6.         super();
  7.         this.list = list;
  8.     }
  9.     @Override
  10.     public void run() {
  11.         String name = Thread.currentThread().getName();
  12.         for(int i = 0; i < 1000; i++) {
  13.             list.add(name + i);
  14.         }
  15.     }
  16. }

 

package com.bird.concursey.charpet9;

import java.util.concurrent.ConcurrentLinkedDeque;

public class AddTask implements Runnable {

	private ConcurrentLinkedDeque<String> list;

	public AddTask(ConcurrentLinkedDeque<String> list) {
		super();
		this.list = list;
	}

	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		for(int i = 0; i < 1000; i++) {
			list.add(name + i);
		}
	}

}

 

 

 

 

 

  1. package com.bird.concursey.charpet9;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. public class PollTask implements Runnable {
  4.     private ConcurrentLinkedDeque<String> list;
  5.     public PollTask(ConcurrentLinkedDeque<String> list) {
  6.         super();
  7.         this.list = list;
  8.     }
  9.     @Override
  10.     public void run() {
  11.         for(int i = 0; i < 5000; i++) {
  12.             list.pollFirst();
  13.             list.pollLast();
  14.         }
  15.     }
  16.     public static void main(String[] args) {
  17.         ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<String>();
  18.         Thread threads[] = new Thread[100];
  19.         for(int i = 0; i < 100; i++) {
  20.             AddTask task = new AddTask(list);
  21.             threads[i] = new Thread(task);
  22.             threads[i].start();
  23.         }
  24.         System.out.printf(“Main: %d AddTask threads have been launched\n”,threads.length);
  25.         for(int i = 0; i < threads.length; i++) {
  26.             try {
  27.                 threads[i].join();
  28.             } catch (InterruptedException e) {
  29.                 e.printStackTrace();
  30.             }
  31.         }
  32.         System.out.printf(“Main: Size of the List: %d\n”,list.size());
  33.         for (int i=0; i< threads.length; i++){
  34.             PollTask task = new PollTask(list);
  35.             threads[i] = new Thread(task);
  36.             threads[i].start();
  37.         }
  38.         System.out.printf(“Main: %d PollTask threads have been launched\n”,threads.length);
  39.         for(int i = 0; i < threads.length; i++) {
  40.             try {
  41.                 threads[i].join();
  42.             } catch (InterruptedException e) {
  43.                 e.printStackTrace();
  44.             }
  45.         }
  46.         System.out.printf(“Main: Size of the List: %d\n”,list.size());
  47.     }
  48. }