Follow the example to learn about java multi-thread 8-synchronous container class, java 8-

Source: Internet
Author: User

Follow the example to learn about java multi-thread 8-synchronous container class, java 8-

We know that java has a multi-threaded and secure container class. We also know that thread security can be achieved if we manage variable states over to these thread security classes, however, we may encounter unimaginable problems.

For example:

Package com. home. thread. thread8; import java. util. Vector;/*** @ author gaoxu! */Public class VectorQueue {private static VectorQueue instance; private static Vector <String> list; private VectorQueue () {list = new Vector <String> ();} public Vector <String> getVector () {return list;} public synchronized static VectorQueue getInstace () {if (instance = null) {instance = new VectorQueue ();} return instance;} public String getLast (Vector <String> list) {int index = list. size ()-1; return (String) list. get (index);} public void delLast (Vector <String> list) {int index = list. size ()-1; list. remove (index );}}

In the above Code, both methods are processing Vector class objects. We know that Vector is a thread-safe class, so can these two methods be correctly accessed by multiple threads? Let's make an instance for verification.

Let's write an example of multi-threaded calling.

Package com. home. thread. thread8; import java. util. Vector;/*** @ author gaoxu practice! */Public class VectorMain {/*** @ param args */public static void main (String [] args) {VectorQueue vq = VectorQueue. getInstace (); for (int I = 0; I <10; I ++) {String test = new String ("" + I); vq. getVector (). add (test) ;}for (int j = 0; j <10; j ++) {ThreadTest threadTest = new ThreadTest (); threadTest. start () ;}} private static class ThreadTest extends Thread {VectorQueue vq = VectorQueue. getInstace (); public ThreadTest () {} public void run () {String data = (String) vq. getLast (vq. getVector (); vq. delLast (vq. getVector (); System. out. println (data );}}}


Let's take a look at the running results (this result can only be obtained after running for many times. This is the problem of Vector and other thread security issues)

<pre class="html" name="code">98765432Exception in thread "Thread-8" 1java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1 at java.util.Vector.get(Vector.java:694) at com.home.thread.thread8.VectorQueue.getLast(VectorQueue.java:26) at com.home.thread.thread8.VectorMain$ThreadTest.run(VectorMain.java:40)
 

This is definitely not the expected result. It indicates the problem of the actual state conditions. The get and del threads access the list. the same value may be obtained when the size is used. In this way, the above error occurs when the del operation of the two threads that obtain the same value is complete and then the get operation is completed.

So how can we modify it to get the correct value? That is to say, we adopt the client locking method. In fact, we will pay attention to synchronization when using basic non-thread-safe object classes. Let's look at the instance.

Package com. home. thread. thread8; import java. util. Vector;/*** @ author gaoxu practice! */Public class VectorQueue {private static VectorQueue instance; private static Vector <String> list; private VectorQueue () {list = new Vector <String> ();} public Vector <String> getVector () {return list;} public synchronized static VectorQueue getInstace () {if (instance = null) {instance = new VectorQueue ();} return instance;} public String getLast (Vector <String> list) {synchronized (list) {int index = list. size ()-1; return (String) list. get (index) ;}} public void delLast (Vector <String> list) {synchronized (list) {int index = list. size ()-1; list. remove (index );}}}


We use the client lock. The running result is as follows. We can see that the execution sequence of the thread is completely out of order, and the existence of the lock is also confirmed.

9876543012


In this way, I run the lock many times. Although no Exception occurs, I always feel that the Vector is untrusted. Haha! Also, HashTable is untrusted.

Next time, let's take a look at ConcurrentHashMap!

The code in the above instance can be further optimized:

Package com. home. thread. thread8; import java. util. Vector;/*** @ author gaoxu practice! */Public class VectorMain {/*** @ param args */public static void main (String [] args) {VectorQueue vq = VectorQueue. getInstace (); for (int I = 0; I <10; I ++) {String test = new String ("" + I); vq. getVector (). add (test) ;}for (int j = 0; j <10; j ++) {ThreadTest threadTest = new ThreadTest (); threadTest. start () ;}} private static class ThreadTest extends Thread {VectorQueue vq = VectorQueue. getInstace (); public ThreadTest () {} public void run () {String data = (String) vq. getLast (); vq. delLast (); System. out. println (data );}}}


Package com. home. thread. thread8; import java. util. Vector;/*** @ author gaoxu practice! */Public class VectorQueue {private static VectorQueue instance; private static Vector <String> list; private VectorQueue () {list = new Vector <String> ();} public Vector <String> getVector () {return list;} public synchronized static VectorQueue getInstace () {if (instance = null) {instance = new VectorQueue ();} return instance;} public String getLast () {synchronized (list) {int index = list. size ()-1; return (String) list. get (index) ;}} public void delLast () {synchronized (list) {int index = list. size ()-1; list. remove (index );}}}


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.