Conclusion: If the collection is not thread-safe, the data loss problem occurs when inserting data in multithreaded situations. Java code
- Import java.util.ArrayList;
- Import java.util.List;
- Threads that implement the Runnable interface
- Public class Hellothread implements Runnable {
- String name;
- List<string> v;
- Hellothread (String name, list<string> v) {
- this.name = name;
- this.v = v;
- }
- public Void Run () {
- SYSTEM.OUT.PRINTLN (name + "Start");
- While (true) {
- V.add (name + ". Add");
- SYSTEM.OUT.PRINTLN (name + "List size is" + v.size ());
- try {
- Thread.Sleep (10);
- } catch (Interruptedexception e) {
- System.out.println (E.getmessage ());
- }
- }
- }
- public static void Main (String args[]) throws interruptedexception {
- List<string> v = new arraylist<string> ();
- Hellothread hello1 = new Hellothread ("Hello1", V);
- Hellothread Hello2 = new Hellothread ("Hello2", V);
- Hellothread Hello3 = new Hellothread ("Hello3", V);
- Thread H1 = new Thread (HELLO1);
- Thread H2 = new Thread (HELLO2);
- Thread h3 = new Thread (HELLO3);
- H1.start ();
- H2.start ();
- H3.start ();
- }
- }
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
- Import Java.util.Vector;
- Threads that implement the Runnable interface
- Public class Hellothread implements Runnable {
- String name;
- Vector<string> v;
- Hellothread (String name, vector<string> v) {
- this.name = name;
- this.v = v;
- }
- public Void Run () {
- SYSTEM.OUT.PRINTLN (name + "Start");
- While (true) {
- V.add (name + ". Add");
- SYSTEM.OUT.PRINTLN (name + "vector size is" + v.size ());
- try {
- Thread.Sleep (10);
- } catch (Interruptedexception e) {
- System.out.println (E.getmessage ());
- }
- }
- }
- public static void Main (String args[]) throws interruptedexception {
- Vector<string> v = new vector<string> ();
- Hellothread hello1 = new Hellothread ("Hello1", V);
- Hellothread Hello2 = new Hellothread ("Hello2", V);
- Hellothread Hello3 = new Hellothread ("Hello3", V);
- Thread H1 = new Thread (HELLO1);
- Thread H2 = new Thread (HELLO2);
- Thread h3 = new Thread (HELLO3);
- H1.start ();
- H2.start ();
- H3.start ();
- }
- }
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