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