Simulation of ticketing system with Java multithreaded operation

Source: Internet
Author: User


Class Threadsale implements Runnable{int tickets = 100;public void run () {while (true) {if (tickets>0) { System.out.println (Thread.CurrentThread (). GetName () + "Sales Ticket" +tickets--+ "No."); Elsesystem.exit (0);}} public class DemoThread {public static void main (string[] args) {Threadsale t=new threadsale ();//Instantiate threads thread thread t1=new thread (T, "Ticket No. 1th"); Thread t2=new thread (T, "Ticket No. 2nd"); Thread t3=new thread (T, "Ticket No. 3rd"); Thread t4=new thread (T, "4th ticket Window"); T1.start (); T2.start (); T3.start (); T4.start ();}}

The result of the operation is:


Ticket No. 1th Ticket No. 100th
Ticket No. 3rd Ticket No. 98th
Ticket No. 4th Ticket No. 99th
Ticket No. 2nd Ticket No. 100th
Ticket No. 4th Ticket No. 95th
Ticket No. 3rd Ticket No. 96th
Ticket No. 1th Ticket No. 97th
Ticket No. 3rd Ticket No. 92nd
Ticket No. 4th Ticket No. 93rd
Ticket No. 2nd Ticket No. 94th
Ticket No. 4th Ticket No. 89th
Ticket No. 3rd Ticket No. 90th
Ticket No. 1th Ticket No. 91st
Ticket No. 3rd Ticket No. 86th
Ticket No. 4th Ticket No. 87th
Ticket No. 2nd Ticket No. 88th
Ticket No. 4th Ticket No. 83rd
Ticket No. 3rd Ticket No. 84th
Ticket No. 1th Ticket No. 85th
Ticket No. 3rd Ticket No. 80th
Ticket No. 4th Ticket No. 81st
Ticket No. 2nd Ticket No. 82nd
Ticket No. 4th Ticket No. 77th
Ticket No. 3rd Ticket No. 78th
Ticket No. 1th Ticket No. 79th
Ticket No. 3rd Ticket No. 74th
Ticket No. 4th Ticket No. 75th
Ticket No. 2nd Ticket No. 76th
Ticket No. 4th Ticket No. 71st
Ticket No. 3rd Ticket No. 72nd
Ticket No. 1th Ticket No. 73rd
Ticket No. 3rd Ticket No. 68th
Ticket No. 4th Ticket No. 69th
Ticket No. 2nd Ticket No. 70th
Ticket No. 4th Ticket No. 65th
Ticket No. 3rd Ticket No. 66th
Ticket No. 1th Ticket No. 67th
Ticket No. 3rd Ticket No. 62nd
Ticket No. 4th Ticket No. 63rd
Ticket No. 2nd Ticket No. 64th
Ticket No. 4th Ticket No. 59th
Ticket No. 3rd Ticket No. 60th
Ticket No. 1th Ticket No. 61st
Ticket No. 3rd Ticket No. 56th
Ticket No. 4th Ticket No. 57th
Ticket No. 2nd Ticket No. 58th
Ticket No. 4th Ticket No. 53rd
Ticket No. 3rd Ticket No. 54th
Ticket No. 1th Ticket No. 55th
Ticket No. 3rd Ticket No. 50th
Ticket No. 4th Ticket No. 51st
Ticket No. 2nd Ticket No. 52nd
Ticket No. 4th Ticket No. 47th
Ticket No. 3rd Ticket No. 48th
Ticket No. 1th Ticket No. 49th
Ticket No. 3rd Ticket No. 44th
Ticket No. 4th Ticket No. 45th
Ticket No. 2nd Ticket No. 46th
Ticket No. 4th Ticket No. 41st
Ticket No. 3rd Ticket No. 42nd
Ticket No. 1th Ticket No. 43rd
Ticket No. 3rd Ticket No. 38th
Ticket No. 4th Ticket No. 39th
Ticket No. 2nd Ticket No. 40th
Ticket No. 4th Ticket No. 35th
Ticket No. 3rd Ticket No. 36th
Ticket No. 1th Ticket No. 37th
Ticket No. 3rd Ticket No. 32nd
Ticket No. 4th Ticket No. 33rd
Ticket No. 2nd Ticket No. 34th
Ticket No. 4th Ticket No. 29th
Ticket No. 3rd Ticket No. 30th
Ticket No. 1th Ticket No. 31st
Ticket No. 3rd Ticket No. 26th
Ticket No. 4th Ticket No. 27th
Ticket No. 2nd Ticket No. 28th
Ticket No. 4th Ticket No. 23rd
Ticket No. 3rd Ticket No. 24th
Ticket No. 1th Ticket No. 25th
Ticket No. 3rd Ticket No. 20th
Ticket No. 4th Ticket No. 21st
Ticket No. 2nd Ticket No. 22nd
Ticket No. 4th Ticket No. 17th
Ticket No. 3rd Ticket No. 18th
Ticket No. 1th Ticket No. 19th
Ticket No. 3rd Ticket No. 14th
Ticket No. 4th Ticket No. 15th
Ticket No. 2nd Ticket No. 16th
Ticket No. 4th Ticket No. 11th
Ticket No. 3rd Ticket No. 12th
Ticket No. 1th Ticket No. 13th
Ticket No. 3rd Ticket No. 8th
Ticket No. 4th Ticket No. 9th
Ticket No. 2nd Ticket No. 10th
Ticket No. 4th Ticket No. 5th
Ticket No. 3rd Ticket No. 6th
Ticket No. 1th Ticket No. 7th
Ticket No. 3rd Ticket No. 2nd
Ticket No. 4th Ticket No. 3rd
Ticket No. 2nd Ticket No. 4th
Ticket No. 1th Ticket No. 1th





Simulation of ticketing system with Java multithreaded operation

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.