Java multithreading ~~~ Non-blocking containers of thread-Safe Containers

Source: Internet
Author: User

Java multithreading ~~~ Non-blocking containers of thread-Safe Containers

Containers are often used in concurrent programming. However, if a container is not thread-safe, it is inserted or deleted in multiple threads.

There will be various problems, that is, the problem of non-synchronization. Therefore, JDK provides a thread-Safe Container, which can ensure secure plug-in of containers in the case of multiple threads.

Import and delete. Of course, there are two types of thread-Safe Containers. The first type is non-blocking. Non-blocking means that when a request to a container is empty or this request

When the command cannot be executed, an exception is reported. The second blocking means that the command that cannot be executed will not report an exception, and he will wait until the command can be executed. Below

Here is an example. Multiple Threads insert a large amount of container data, while the other thread outputs a large amount of pop data.


The Code is as follows:

package com.bird.concursey.charpet9;import java.util.concurrent.ConcurrentLinkedDeque;public class AddTask implements Runnable {private ConcurrentLinkedDeque
 
   list;public AddTask(ConcurrentLinkedDeque
  
    list) {super();this.list = list;}@Overridepublic void run() {String name = Thread.currentThread().getName();for(int i = 0; i < 1000; i++) {list.add(name + i);}}}
  
 


package com.bird.concursey.charpet9;import java.util.concurrent.ConcurrentLinkedDeque;public class PollTask implements Runnable {private ConcurrentLinkedDeque
 
   list;public PollTask(ConcurrentLinkedDeque
  
    list) {super();this.list = list;}@Overridepublic void run() {for(int i = 0; i < 5000; i++) {list.pollFirst();list.pollLast();}}public static void main(String[] args) {ConcurrentLinkedDeque
   
     list = new ConcurrentLinkedDeque
    
     ();Thread threads[] = new Thread[100];for(int i = 0; i < 100; i++) {AddTask task = new AddTask(list);threads[i] = new Thread(task);threads[i].start();}System.out.printf("Main: %d AddTask threads have been launched\n",threads.length);for(int i = 0; i < threads.length; i++) {try {threads[i].join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.printf("Main: Size of the List: %d\n",list.size());for (int i=0; i< threads.length; i++){PollTask task = new PollTask(list);threads[i] = new Thread(task);threads[i].start();}System.out.printf("Main: %d PollTask threads have been launched\n",threads.length);for(int i = 0; i < threads.length; i++) {try {threads[i].join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.printf("Main: Size of the List: %d\n",list.size());}}
    
   
  
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.