Multithreading 13--Multi-threaded concurrent purchase-inefficient thread-safe ticket container

Source: Internet
Author: User
Tags ticket

1. Final list<string> synchronizedlist = collections.synchronizedlist (tickets);//list becomes thread-safe

1  Public classTickets {2      Public Static voidMain (string[] args) {3         //Final Vector<string> tickets = new vector<string> ();4         FinalList<string> tickets =NewArraylist<>();5          for(inti = 1; I <= 100; i++) {6Tickets.add ("Train ticket" +i);7         }8         Finallist<string> synchronizedlist = collections.synchronizedlist (tickets);//list becomes thread-safe9 Ten         //Create 10 Threads One          for(inti = 1; I <= 10; i++) { A             NewThread ("Threads" +i) { -                  Public voidrun () { -                      while(true){ the                         if(Tickets.isempty ()) Break; -System.out.println (Thread.CurrentThread (). GetName () + "purchased train tickets" +synchronizedlist.remove (0)); -                     } -                 } + }.start (); -         } +     } A     /* at thread 1 purchased train tickets train Tickets 1 - thread 1 purchased train tickets train Tickets 2 - thread 1 purchased train tickets train Tickets 3 - thread 1 purchased train tickets train Tickets 4 - thread 1 purchased train tickets train tickets 5 - thread 1 purchased train tickets train tickets 6 in thread 1 purchased train tickets train tickets 7 - thread 1 purchased train tickets train tickets 8 to thread 1 purchased train tickets train tickets 9 + thread 1 purchased train tickets for train tickets - thread 1 purchased train ticket train tickets one by one the thread 1 purchased train tickets for train tickets * thread 1 purchased train tickets for train tickets $ thread 1 purchased train tickets for train ticketsPanax Notoginseng thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets + thread 1 purchased train tickets for train tickets A thread 1 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets + thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets $ thread 1 purchased train tickets for train tickets $ thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train ticketsWuyi thread 1 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets Wu thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets About thread 1 purchased train tickets for train tickets $ thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets - thread 1 purchased train tickets for the train ticket - thread 1 purchased train tickets for train tickets A thread 1 purchased train tickets for train tickets + thread 1 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets - thread 3 purchased train tickets for train tickets $ thread 2 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets the thread 2 purchased train tickets train tickets the thread 3 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets - thread 1 purchased train tickets for train tickets in thread 2 purchased train tickets for train tickets the thread 3 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets About thread 1 purchased train tickets for train tickets the Thread 2 purchased train ticket train tickets the thread 3 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets + thread 1 purchased train tickets for train tickets - thread 2 purchased train tickets for train tickets the thread 3 purchased train tickets for train ticketsBayi thread 2 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets - thread 3 purchased train tickets train tickets - thread 2 purchased train tickets for train tickets the thread 1 purchased train tickets train tickets the Thread 4 purchased train tickets for train tickets the thread 2 purchased train tickets for train tickets the thread 3 purchased train tickets for train tickets - thread 2 purchased train tickets for train tickets the Thread 4 purchased train tickets for train tickets the thread 1 purchased train tickets for train tickets the Thread 4 purchased train tickets for train tickets94 thread 2 purchased train tickets for train tickets the thread 3 purchased train tickets train tickets the thread 2 purchased train tickets for train tickets the Thread 4 purchased train tickets for train tickets98 thread 1 purchased train tickets train tickets About Thread 5 purchased train tickets for train tickets - Thread 4 purchased train tickets for train tickets101 thread 2 purchased train tickets for train tickets102 thread 3 purchased train tickets for train tickets103 thread 2 purchased train tickets for train tickets104 Thread 4 purchased train tickets for train tickets the Thread 5 purchased train tickets for train tickets106 thread 1 purchased train tickets for train tickets Bayi107 Thread 5 purchased train tickets for train tickets108 Thread 4 purchased train tickets train tickets109 thread 2 purchased train tickets train tickets the thread 3 purchased train tickets for train tickets111 thread 2 purchased train tickets for train tickets the Thread 4 purchased train tickets for train tickets113 Thread 5 purchased train tickets for train tickets the Thread 4 purchased train tickets for train tickets the Thread 6 purchased train tickets train tickets 94 the thread 1 purchased train tickets for train tickets117 Thread 4 purchased train tickets train tickets 98118 thread 2 purchased train tickets for train tickets119 Thread 5 purchased train tickets for train tickets - thread 3 purchased train tickets for train tickets121 Thread 6 purchased train tickets for train tickets122 thread 1 purchased train tickets for train tickets*/123 124}
View Code

Multithreading 13--Multi-threaded concurrent purchase-inefficient thread-safe ticket container

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.