The application of locks in Java concurrent collection operations.

Source: Internet
Author: User

Here is an example of a list combination,

First look at the following code:

public static arraylist<string>datas=new arraylist<string> ();//Initialize data public static void InitData () {for ( int i=0;i<20;i++) {datas.add ("" +i);}} Thread 1, reading the collection's data public static  thread Thread1=new thread () {public void run () {//int size=datas.size (); for (String data: Datas) {System.out.println (data);}};};/ /thread 2, delete the collection's data private static thread Thread2=new thread () {public void run () {int size=datas.size (); for (int i=0;i<size;i + +) {datas.remove (0); System.out.println ("Remove");}};};/ /Startup program public        static void Main (string[] args) {initdata (); Thread1.start (); Thread2.start ();}



If you run this way, you will definitely get an exception. Let's look at the results of the execution
Remove
Remove
1
Remove
Remove
Remove
Remove
Remove
Remove
Exception in Thread "Thread-0" remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Java.util.ConcurrentModificationException
At Java.util.arraylist$itr.checkforcomodification (arraylist.java:819)
At Java.util.arraylist$itr.next (arraylist.java:791)


In a real-world programming environment, you will often encounter both the need to traverse a collection and the need to delete it in another thread. So how to solve it?

The following is to solve this problem, we use the Reentrantreadwritelock located in the java.util.concurrent.locks bag;

For Short Read and write locks, it has two core methods, namely Readlock () and Writelock (), which are read and write locks. The prerequisite for acquiring a read lock is that the write lock is not locked. The prerequisite for obtaining a write lock is that the read lock is not locked. This means that no one is writing or modifying data while reading. No one is reading the data while writing or modifying the data. As if we were editing a Word document, in the edit state, it is not allowed to move, copy. Based on this principle, we then improve the code as follows:

public static final Readwritelock lock = new Reentrantreadwritelock (false);p ublic static arraylist<string>datas= New Arraylist<string> ();p ublic static void InitData () {for (int i=0;i<20;i++) {datas.add ("" +i);}} public static  thread Thread1=new thread () {public void run () {Lock.readlock (). Lock (); for (String Data:datas) { System.out.println ("T1  " +data); Lock.readlock (). Unlock ();};}; public static  thread Thread3=new thread () {public void run () {Lock.readlock (). Lock (); for (String Data:datas) { System.out.println ("T3  " +data); Lock.readlock (). Unlock ();};}; private static thread Thread2=new thread () {public void run () {int size=datas.size (), Lock.writelock (). Lock (); for (int i=0 ; i<size;i++) {datas.remove (0); System.out.println ("Remove"); Lock.writelock (). Unlock ();};}; public static void Main (string[] args) {initdata (); Thread1.start (); Thread2.start (); Thread3.start ();}



The result of the program execution is:

T1 0
T1 1
T1 2
T1 3
T1 4
T1 5
T1 6
T1 7
T1 8
T1 9
T1 10
T1 11
T1 12
T1 13
T1 14
T1 15
T1 16
T1 17
T1 18
T1 19
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove


You may wonder why there is no T3 print? Since the read lock is released, it is immediately occupied by the write lock, the thread that writes the lock clears the collection, so when the thread is 3, there is no data, and a few more times, you will find that there is an execution result is all remove. There is no print, this is because thread 2 was executed first. If we do this in this order, it will be different:
public static void Main (string[] args) {initdata (); Thread1.start (); Thread3.start (); Thread2.start ();}



T3 0
T1 0
T3 1
T1 1
T3 2
T1 2
T3 3
T1 3
T3 4
T1 4
T3 5
T1 5
T3 6
T1 6
T3 7
T1 7
T3 8
T1 8
T3 9
T1 9
T3 10
T1 10
T3 11
T1 11
T3 12
T1 12
T3 13
T1 13
T3 14
T1 14
T3 15
T1 15
T1 16
T1 17
T3 16
T1 18
T1 19
T3 17
T3 18
T3 19
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove
Remove

you can see that the read lock is not exclusive in different threads. OK, let's introduce you to this place.

The application of locks in Java concurrent collection operations.

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.