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