A thread-safe and insecure auxiliary understanding can take this example to run it yourself to see

Source: Internet
Author: User
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

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.