Multithreaded first experience in Java

Source: Internet
Author: User

This is a mock-up of station ticketing, where multiple ticketing staff sell a high-speed rail (G250) at the same time. ), the basic guarantee for ticket sales is 2 points:

1. Tickets cannot be sold more than the predetermined limit.

2. Tickets with the same seat cannot be sold.

Beginner Java, there are inappropriate places everyone crossing please advise ~

The code is as follows:

 PackageThreadjava; Public classThreadTest4 { Public Static voidMain (string[] args) {//TODO Auto-generated method stubs//Limited total of 100 votesSeller Seller =NewSeller (100); //Open Three-window ticketingWorker W1 =NewWorker (seller); Worker W2=NewWorker (seller); Worker W3=NewWorker (seller); }    }//the machine that prints the ticket, as the server can connect multiple clients to run usingclassSeller {Private int[] tickets; Private intSold_line;  PublicSeller (intticket_total) {         This. Tickets =gettickets (ticket_total);  This. Sold_line =Ticket_total; }        //This method is an exclusive method whereby only one thread is allowed to call at the same time//is also the main implementation part of Java multithreading for synchronous operation     Public synchronized intSell () {if( This. sold_line! = 0){            intTicket_pos = (int) (Math.random () * This. Sold_line); intTicket = This. Tickets[ticket_pos];  This. Sold_line = This. sold_line-1; Swap ( This. Tickets, Ticket_pos, This. Sold_line); returnticket; }Else{            return-1; }    }        Private Static voidSwapint[] A,intXinty) {        inttemp =A[x]; A[X]=A[y]; A[y]=temp; }        Private Static voidShfulle (int[] a) {intx, y;  for(x=0; x<a.length; x + +) {y= (int) (Math.random () * (x+1));        Swap (A, x, y); }    }        Private Static int[] Gettickets (intTotal ) {        inti; int[] A =New int[Total];  for(i=0; i<total; i++) {A[i]=i;        } Shfulle (a); returnA; }}//The ticket operator is activated and the ticket is opened immediately. Haha, really dedicated ~classWorkerextendsThread {Private Static intThreadID = 0; PrivateSeller Tickor;  PublicWorker (Seller Seller) {Super("Ticket Seller NO." + (+ +)ThreadID));  This. Tickor =seller;  This. Start (); }         Public voidrun () { while(true){            intTicket = This. Tickor.sell (); if(Ticket = =-1) {System.out.println ( This. GetName () + ", sold out.");  Break; }Else{System.out.println ( This. GetName () + ", Get ticket:" +ticket); }        }    }}

Result of the ticket:

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

Reference article: http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html

Multithreaded first experience in Java

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.