First, from the result, the number does not increase with each increase of the thread, also means that different people, some people first rob the ticket, but the thread did not run in time, grab tickets or buy the remaining tickets.
Cause: Although the method is locked, but different threads run indeterminate, and in fact, the generation of this ticket is not locked limit, resulting in the first buyer, can not get tickets. -----The ticket no duplicate, but there is no priority concept, discontinuous.
Ticketnum:5Thread Name:thread1 Thread 1 starts and calls the Generate ticket method, then gets the 5th ticketTicketnum:8Thread Name:thread8Ticketnum:7Thread Name:thread7Ticketnum:1Thread Name:thread3Ticketnum:TenThread Name:threadTenTicketnum:0Thread Name:thread0Ticketnum:4Thread Name:thread2Ticketnum:6Thread Name:thread6Ticketnum:2Thread Name:thread5
Package Test;public class Starter {public static void main (string[] args) {for (int. i=0;i<50;i++) {Bookticketthread Book =new bookticketthread ("Thread" +i);}} Package Test;public class Bookticketthread extends Thread{private String name;private Data data;public bookticketthread ( String name) {This.name=name;//this.data=data;start ();} @Overridepublic void Run () {System.out.println ("Ticketnum:" +data.getticketnum () + "Thread Name:" +name); The Data.getticketnum method is locked, and this method may be called by any thread, and all threads have the same competing rights. So the route Line 1 may be forced to get ticket number fifth. }}package Test;public class data{private static int ticketnum;public synchronized static int getticketnum () {return ticket num++;}} Result:ticketnum:5 thread name:thread 1ticketnum:8 thread name:thread 8ticketnum:7 thread Name:thread 7ticketnum:1 Thread Name:thread 3ticketnum:10 thread name:thread 10ticketnum:0 thread name:thread 0ticketnum:4 thread Name:threa D 2ticketnum:6 thread name:thread 6ticketnum:2 thread name:thread 5ticketnum:13 thread Name:thread 13ticKetnum:3 thread name:thread 4ticketnum:14 thread name:thread 14ticketnum:12 thread Name:thread 12ticketnum:11 Threa D name:thread 11ticketnum:16 thread name:thread 16ticketnum:9 thread name:thread 9ticketnum:17 thread Name:thread 1 7ticketnum:18 thread name:thread 18ticketnum:15 thread name:thread 15ticketnum:19 thread Name:thread 19ticketnum:20 Thread Name:thread 20ticketnum:21 thread name:thread 21ticketnum:22 thread name:thread 22ticketnum:23 thread Name: Thread 23ticketnum:24 thread name:thread 24ticketnum:25 thread name:thread 25ticketnum:26 thread Name:thread 26ticke tnum:27 thread name:thread 27ticketnum:28 thread name:thread 28ticketnum:29 thread Name:thread 29ticketnum:30 Threa D name:thread 30ticketnum:31 thread name:thread 31ticketnum:32 thread name:thread 32ticketnum:33 thread Name:thread 33ticketnum:34 thread name:thread 34ticketnum:35 thread name:thread 35ticketnum:36 thread Name:thread 36ticketNum: Panax Notoginseng Thread Name:thread 37ticketnum:38 thread name:thread 38ticketnum:39 thread name:thread 39ticketnum:40 thread Name:thread 40ticketnum:41 Thread Name:thread 41ticketnum:42 thread name:thread 42ticketnum:43 thread name:thread 43ticketnum:44 thread Name: Thread 44ticketnum:45 thread name:thread 45ticketnum:46 thread name:thread 46ticketnum:47 thread Name:thread 47ticke tnum:48 thread Name:thread 48ticketnum:49 thread Name:thread 49
Method Two: Lock the object, the so-called to get priority, the lock method can not maintain the order of results, need to lock the whole object, to obtain the results can ensure the order of the results.
Package Test;public class Starter {public static void main (string[] args) {Data data=new data (), for (int i=0;i<50;i++) { C0>bookticketthread book=new bookticketthread ("Thread" +i,data);}} Package Test;public class Bookticketthread extends thread{private String name;private data data; Public Bookticketthread (String name,data Data) {This.name=name;this.data=data;start ();} @Overridepublic void Run () {System.out.println ("Ticketnum:" +data.getticketnum () + "Thread Name:" +name);}} Package Test;public class data{private int ticketnum;public synchronized int Getticketnum () {return ticketnum+ +;}}
Result: continuous and non-repeating.
Ticketnum:0Thread Name:thread0Ticketnum:4Thread Name:thread4Ticketnum:3Thread Name:thread3Ticketnum:2Thread Name:thread2Ticketnum:6Thread Name:thread6Ticketnum:1Thread Name:thread1Ticketnum:7Thread Name:thread7Ticketnum:5Thread Name:thread5Ticketnum:8Thread Name:thread8Ticketnum:9Thread Name:thread9Ticketnum:TenThread Name:threadTenTicketnum: OneThread Name:thread OneTicketnum: AThread Name:thread ATicketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: theThread Name:thread theTicketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: +Thread Name:thread +Ticketnum: -Thread Name:thread -Ticketnum: +Thread Name:thread +Ticketnum: AThread Name:thread ATicketnum: atThread Name:thread atTicketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread -Ticketnum: inThread Name:thread inTicketnum: -Thread Name:thread -Ticketnum: toThread Name:thread toTicketnum: +Thread Name:thread +Ticketnum: -Thread Name:thread -Ticketnum: theThread Name:thread theTicketnum: *Thread Name:thread *Ticketnum: $Thread Name:thread $Ticketnum:Panax NotoginsengThread Name:threadPanax NotoginsengTicketnum: -Thread Name:thread -Ticketnum: theThread Name:thread theTicketnum: +Thread Name:thread +Ticketnum: AThread Name:thread ATicketnum: theThread Name:thread theTicketnum: +Thread Name:thread +Ticketnum: -Thread Name:thread -Ticketnum: $Thread Name:thread $Ticketnum: $Thread Name:thread $Ticketnum: -Thread Name:thread -Ticketnum: -Thread Name:thread theTicketnum: theThread Name:thread -
The difference between the line Cheng on the object and the lock on the method (analogue train ticket online ticketing system: Multiple threads are simultaneously ticketed to ensure that each ticket is numbered consecutively and not duplicated. )