Java create multi-threaded and line Shuo complete vector

Source: Internet
Author: User

 Public classTest { Public Staticvector<string> data =NewVector<string>();  Public Static voidMain (string[] args) { for(inti = 0; I < 100; i++) {Data.add ("Data" +i); }                 for(inti = 0; I < 3; i++) {Thread T=NewThread () { Public voidrun () {String str= "";  while(Data.size () > 0 &&! (str = data.remove (0)). Equals ("") {System.out.println ( This. GetId () + "| |" +str); //pause for 1 seconds                        Try{Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();            }                    }                }            };        T.start (); }    }}

About Collection Security:

    • ArrayList thread is unsafe, vector thread is safe;
    • HashMap thread is unsafe, hashtable thread safety;
    • StringBuilder thread is unsafe, stringbuffer thread safe.

About creating Threads:

Method one: As on, anonymous thread

Method Two: Create a class mythread inherit the thread. Use the following:

MyThread t=new MyThread ();

T.start ();

Method Three: Create Class Mythread Implement Interface runnable. Use the following:

Thread T=new thread (new MyThread);

T.start ();

To create a thread, refer to: http://www.importnew.com/20672.html

Java create multi-threaded and line Shuo complete vector

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.