public class Test {//linkedlist thread is unsafe//list<integer>list=new linkedlist<integer> ();//vector Thread Safety list< Integer> list=new vector<integer> ();p ublic static void Main (string[] args) {test test=new test (); Threada TA = Test.new Threada (); threadb TB = Test.new threadb (); Ta.start (); Tb.start (); Try{ta.join (); Tb.join ();} catch (Interruptedexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("List size:" +test.list.size ());} Class Threada extends thread{@Overridepublic void Run () {super.run (); for (int i=0;i<100;i++) {list.add (i);}}} Class Threadb extends thread{@Overridepublic void Run () {super.run (); for (int i=0;i<100;i++) {list.add (i);}}}}
When the list is LinkedList, no matter how the result will not be 200, single if the vector instance out of the list result is always 200.
A thread-safe and insecure auxiliary understanding can take this example to run it yourself to see