Code I wrote word for Word, reproduced please indicate the source, thank you for your cooperation!
Multi-threaded ticketing problem is a cliché, the author is a Java beginner, once to find out this problem in degrees Niang searched a lot of the code on this issue, found the correct code is still very limited.
Paste replication is a serious phenomenon, and most of the code problems are mainly in the printing of tickets more than the number of problems (some will print negative numbers), as well as the appearance of the individual surface appears to be correct, but in fact, the program is also a thread waiting to be executed (in the Eclipse console to see the obvious "red dot" has been lit) The following is the author's own writing about the problem of the code, due to the limited personal level, inevitably some omissions, I hope everyone mo stingy enlighten!
Source:
1 ImportJava.text.NumberFormat;2 3 Public classSelltickets {4 Public Static voidMain (string[] args) {5 6 //Create ticket objects and initialize the total number of tickets7Ticket ticket=NewTicket (1000);8 9 //Create a ticket thread, set the window name, and then start the thread, set four windows hereTen NewThread (Ticket, "window a"). Start (); One NewThread (Ticket, "Window B"). Start (); A NewThread (Ticket, "Window C"). Start (); - NewThread (Ticket, "window D"). Start (); - the } - } - - /** + * Multithreading is implemented in a way that implements the Runnable interface - */ + classTicketImplementsRunnable { A at //Total number of tickets - intTotal ; - -Ticket (intTotal ) { - This. total=Total ; - } in - //Initial Ticket Number 1 to intnum = 1; + - @Override the Public voidrun () { * while(true) { $ Panax Notoginseng synchronized("") { - Try { the //any thread that gets the "lock" will have to decide whether or not to have the ticket, to prevent the waiting thread from printing the ticket more . + if(Num > Total)return; A the //gets the current thread name +String ThreadName =Thread.CurrentThread (). GetName (); - $ //Format Ticket $String ticketnum = Formatticketnum (num++); - - //Print train ticket, sleep 20 ms simulate print ticket time theThread.Sleep (20); -System.out.println (ThreadName + "sold train tickets No." +ticketnum);Wuyi the //when a thread sells the last ticket, the "ticket is sold out" prompt is released - if(Num >Total ) { WuSystem.out.println ("Ticket is sold out!")); - return; About } $}Catch(interruptedexception e) { - e.printstacktrace (); - } - } A } + } the - /** $ * Format Ticket number the */ the StaticString Formatticketnum (intnum) { the theNumberFormat NF =numberformat.getintegerinstance (); -Nf.setminimumintegerdigits (3); in returnNf.format (num); the } the}
Printing results:
1Window D sold train tickets no.0012Window D sold train tickets no.0023Window D sold train tickets no.0034Window D sold train tickets no.0045Window D sold train tickets no.0056Window D sold train tickets no.0067Window D sold train tickets no.0078Window D sold train tickets no.0089Window D sold train tickets no.009TenWindow D sold train tickets no.010 OneWindow D sold train tickets no.011 AWindow D sold train tickets no.012 -Window D sold train tickets no.013 -Window D sold train tickets no.014 theWindow D sold train tickets no.015 -Window D sold train tickets no.016 -Window D sold train tickets no.017 -Window D sold train tickets no.018 +Window D sold train tickets no.019 -Window D sold train tickets no.020 +Window D sold train tickets no.021 AWindow D sold train tickets no.022 atWindow D sold train tickets no.023 -Window D sold train tickets no.024 -Window D sold train tickets no.025 -Window D sold train tickets no.026 -Window D sold train tickets no.027 -Window D sold train tickets no.028 inWindow D sold train tickets no.029 -Window D sold train tickets no.030 toWindow D sold train tickets no.031 +Window D sold train tickets no.032 -Window D sold train tickets no.033 theWindow D sold train tickets no.034 *Window C sold train tickets no.035 $Window C sold train tickets no.036Panax NotoginsengWindow C sold train tickets no.037 -Window C sold train tickets no.038 theWindow C sold train tickets no.039 +Window C sold train tickets no.040 AWindow a sold train tickets no.041 theWindow a sold train tickets no.042 +Window a sold train tickets no.043 -Window a sold train tickets no.044 $Window a sold train tickets no.045 $Window a sold train tickets no.046 -Window a sold train tickets no.047 -Window B sold train tickets no.048 theWindow B sold train tickets no.049 -Window B sold train tickets no.050WuyiWindow B sold train tickets no.051 theWindow B sold train tickets no.052 -Window B sold train tickets no.053 Wu ... ... .... -Window D sold train tickets no.974 AboutWindow D sold train tickets no.975 $Window D sold train tickets no.976 -Window C sold train tickets no.977 -Window C sold train tickets no.978 -Window C sold train tickets no.979 AWindow C sold train tickets no.980 +Window C sold train tickets no.981 theWindow C sold train tickets no.982 -Window C sold train tickets no.983 $Window C sold train tickets no.984 theWindow C sold train tickets no.985 theWindow a sold train tickets no.986 theWindow a sold train tickets no.987 theWindow a sold train tickets no.988 -Window a sold train tickets no.989 inWindow a sold train tickets no.990 theWindow a sold train tickets no.991 theWindow B sold train tickets no.992 AboutWindow B sold train tickets no.993 theWindow B sold train tickets no.994 theWindow B sold train tickets no.995 theWindow B sold train tickets no.996 +Window B sold train tickets no.997 -Window B sold train tickets no.998 theWindow a sold train tickets no.999BayiWindow C sold train tickets no.1,000 theTickets are sold out!
The commonplace multi-threading issue