Arraylist,vector Thread Safety Testing

Source: Internet
Author: User

Conclusion: If the collection is not thread-safe, the data loss problem occurs when inserting data in multithreaded situations. Java code
  1. Import java.util.ArrayList;
  2. Import java.util.List;
  3. Threads that implement the Runnable interface
  4. Public class Hellothread implements Runnable {
  5. String name;
  6. List<string> v;
  7. Hellothread (String name, list<string> v) {
  8. this.name = name;
  9. this.v = v;
  10. }
  11. public Void Run () {
  12. SYSTEM.OUT.PRINTLN (name + "Start");
  13. While (true) {
  14. V.add (name + ". Add");
  15. SYSTEM.OUT.PRINTLN (name + "List size is" + v.size ());
  16. try {
  17. Thread.Sleep (10);
  18. } catch (Interruptedexception e) {
  19. System.out.println (E.getmessage ());
  20. }
  21. }
  22. }
  23. public static void Main (String args[]) throws interruptedexception {
  24. List<string> v = new arraylist<string> ();
  25. Hellothread hello1 = new Hellothread ("Hello1", V);
  26. Hellothread Hello2 = new Hellothread ("Hello2", V);
  27. Hellothread Hello3 = new Hellothread ("Hello3", V);
  28. Thread H1 = new Thread (HELLO1);
  29. Thread H2 = new Thread (HELLO2);
  30. Thread h3 = new Thread (HELLO3);
  31. H1.start ();
  32. H2.start ();
  33. H3.start ();
  34. }
  35. }

Results:

Hello3start
Hello3 list size is 1
Hello1start
Hello1 List size is 2
Hello2start
Hello2 List size is 3
Hello3 List size is 4
Hello1 List size is 5
Hello2 List size is 4
Hello3 List size is 6
Hello1 List size is 8
Hello2 List size is 7
Hello1 List size is 9
Hello3 List size is 10
Hello2 List size is 9

Added 12 times, but size was only 10, not safe.

Changed to a thread-safe vector:

Java code
  1. Import Java.util.Vector;
  2. Threads that implement the Runnable interface
  3. Public class Hellothread implements Runnable {
  4. String name;
  5. Vector<string> v;
  6. Hellothread (String name, vector<string> v) {
  7. this.name = name;
  8. this.v = v;
  9. }
  10. public Void Run () {
  11. SYSTEM.OUT.PRINTLN (name + "Start");
  12. While (true) {
  13. V.add (name + ". Add");
  14. SYSTEM.OUT.PRINTLN (name + "vector size is" + v.size ());
  15. try {
  16. Thread.Sleep (10);
  17. } catch (Interruptedexception e) {
  18. System.out.println (E.getmessage ());
  19. }
  20. }
  21. }
  22. public static void Main (String args[]) throws interruptedexception {
  23. Vector<string> v = new vector<string> ();
  24. Hellothread hello1 = new Hellothread ("Hello1", V);
  25. Hellothread Hello2 = new Hellothread ("Hello2", V);
  26. Hellothread Hello3 = new Hellothread ("Hello3", V);
  27. Thread H1 = new Thread (HELLO1);
  28. Thread H2 = new Thread (HELLO2);
  29. Thread h3 = new Thread (HELLO3);
  30. H1.start ();
  31. H2.start ();
  32. H3.start ();
  33. }
  34. }

Results:

Hello1start
Hello1 Vector size is 1
Hello2start
Hello2 Vector size is 2
Hello3start
Hello3 Vector size is 3
Hello1 Vector size is 4
Hello2 Vector size is 5
Hello3 Vector size is 6
Hello1 Vector size is 7
Hello2 Vector size is 8
Hello3 Vector size is 9
Hello1 Vector size is 10
Hello2 Vector size is 11
Hello3 Vector size is 12
Hello1 Vector size is 13
Hello3 Vector size is 15
Hello2 Vector size is 15
Hello1 Vector size is 16

Is there a thread-unsafe phenomenon? No

This is not a thread unsafe, add 16 times, size is 16, is exactly the performance of thread safety, but is to wait two threads are added after the size (), so are 15, skipped 14.

The same program more than a few times to appear, and the other about the vector thread-safety

All Vector methods is synchronized themselves, so as long as your is only synchronizing around a single method, your own Synchronization is not necessary. If you had several method calls, which depend on each other, e.g. something as to vec.get(vec.size()-2) get the second last element, yo U has to use your own synchronization since otherwise, the vector could change between vec.size () and Vec.get ().

All vector methods are synchronized to themselves, so if you just sync a single method, your extra sync measures will be lost. If several methods need to be called, and they are dependent on each other, such as Vec.get (Vec.size ()-2), is to get the second-to-last element, then must add their own synchronization measures, because otherwise, vectors may be in vec.size () and Vec.get () Change between

Arraylist,vector Thread Safety Testing

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.