The demo thread does not consider the full logic. For example, 50 = 20 + 20 + 5 + 5 or 50 = 10 + 10 + 20 + 5... In this example, only the two cases are determined when the value of 50 is changed to zero. The other cases are not considered. Focuses on demonstrating the thread process ....
Code:
- Package cc. nothing2012.blogservlet;
- Import java. util. arraylist;
- Import java. util. List;
- /* = Question: Imitate Example 8-10, design five people to buy tickets in queue, and the Ticket Selling Rules and queuing sequence are specified =========== */
- /*
- * Five people are allowed to buy tickets in queue, and each person buys one ticket. The conductor has 1 20 yuan and 1 5 yuan, and 5 yuan for a movie ticket.
- * The Order of the queue from start to end is: Guan 50, Lin 20, Yu 5, Li 10, and Yang 5.
- * The Order of buying tickets is: Yu 5, Li 10, Lin 20, Guan 50, and Yang Wu.
- */
- Public class ch8_4 {
- Public static list <string> theresult = new arraylist <string> (); // stores the final result of the ticket
- Public static void main (string [] ARGs ){
- String [] sname = {" 50", "Lin 20", "Yu 5", "Li 10", "Yang Wu "};
- Cinema c = new cinema (sname );
- Thread [] theads = {New thread (C), new thread (C), new thread (c ),
- New thread (C), new thread (c )};
- // For (INT I = 0; I <5; I ++) {// initialize five processes
- // Theads [I] = new thread (C );
- //}
- For (INT I = 0; I <5; I ++ ){
- Theads [I]. setname (sname [I]);
- }
- For (INT I = 0; I <5; I ++ ){
- Theads [I]. Start ();
- }
- While (true) {// print the final result
- Try {
- Thread. Sleep (1000 );
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- If (theresult! = NULL ){
- If (theresult. Size () = 5 ){
- Printmessage ("last result of buying tickets ");
- For (INT I = 0; I <5; I ++ ){
- System. Out. Print (theresult. Get (I) + "/t ");
- }
- Break;
- }
- }
- }
- }
- // Print the information.
- Public static void printmessage (Object O ){
- System. Out. println (O );
- }
- }
- // ======================================
- Class cinema implements runnable {
- Ticketseller seller;
- String [] sname;
- Public Cinema (string [] sname ){
- Seller = new ticketseller ();
- This. sname = sname;
- }
- Public void run (){
- If (thread. currentthread (). getname (). Equals (sname [0]) {
- Seller. sellticket (50 );
- } Else if (thread. currentthread (). getname (). Equals (sname [1]) {
- Seller. sellticket (20 );
- } Else if (thread. currentthread (). getname (). Equals (sname [2]) {
- Seller. sellticket (5 );
- } Else if (thread. currentthread (). getname (). Equals (sname [3]) {
- Seller. sellticket (10 );
- } Else if (thread. currentthread (). getname (). Equals (sname [4]) {
- Seller. sellticket (5 );
- }
- }
- }
- // ================ Ticket seller's ticket handling
- Class ticketseller {
- Int fivenumber = 1, tennumber = 0, twentynumber = 1, tynumber = 0; // each ticket clerk has 5 yuan and 20 yuan
- Public synchronized void sellticket (INT receivemoney ){
- String sname = thread. currentthread (). getname ();
- If (receivemoney = 5) {// the price of a ticket is 5 yuan, which is sold directly to him at exactly 5 yuan. You do not need to change the value to 0.
- Fivenumber + = 1;
- Ch8_4.printmessage (sname + "give the ticket clerk 5 yuan, the ticket clerk sells" + sname + "One ticket does not need to be redeemed ");
- Ch8_4.theresult.add (sname );
- } Else if (receivemoney = 10 ){
- While (fivenumber <1 ){
- Ch8_4.printmessage (sname + "10 yuan for conductor ");
- Ch8_4.printmessage ("small ticket clerk please" + sname + "side wait for a while ");
- Try {
- Wait ();
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
- }
- // There is change
- Fivenumber = fivenumber-1;
- Tennumber + = 1;
- Ch8_4.printmessage (sname + "give the conductor 10 yuan, the conductor sells to" + sname + "one ticket, redeem 5 yuan ");
- Ch8_4.theresult.add (sname );
- } Else if (receivemoney = 20 ){
- While (fivenumber <1 | tennumber <1 ){
- Ch8_4.printmessage (sname + "Give ticket conductor 20 yuan/n ticket conductor" + sname + "wait for a while ");
- Try {
- Wait ();
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
- }
- Fivenumber = fivenumber-1;
- Tennumber = tennumber-1;
- Twentynumber + = 1;
- Ch8_4.printmessage (sname + "20 yuan for the conductor, sold to" + sname + "one ticket, change to 15 yuan ");
- Ch8_4.theresult.add (sname );
- } Else if (receivemoney = 50 ){
- String flag = "";
- // Boolean isok = true;
- While (true ){
- If (fivenumber> 0) {// There are two combinations of 45 blocks
- If (twentynumber = 2 ){
- Flag = "2 twenty ";
- Break;
- // Isok = false;
- } Else if (twentynumber = 1 & tennumber = 2 ){
- Flag = "1 twenty ";
- // Isok = false;
- Break;
- }
- }
- Ch8_4.printmessage (sname + "Give ticket conductor 50 yuan/n ticket conductor" + sname + "wait for a while ");
- Try {
- Wait ();
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
- }
- If (flag. endswith ("2 twenty ")){
- Fivenumber-= 1;
- Twentynumber = twentynumber-2;
- Required tynumber + = 1;
- } Else if (flag. Equals ("1 twenty ")){
- Fivenumber = fivenumber-1;
- Tennumber = tennumber-2;
- Twentynumber = twentynumber-1;
- }
- Ch8_4.printmessage (sname + "give the conductor 50 yuan, the conductor sells to" + sname + "one ticket, change to 45 yuan ");
- Ch8_4.theresult.add (sname );
- }
- Policyall ();
- }
- }
The running result is as follows:
Pass 50 to ticket conductor 50 yuan
Wait for a while
Lin 20 paid 20 yuan to the conductor
The conductor asked Lin to wait for a while.
The remaining five yuan will be paid to the ticket clerk for 5 yuan. The ticket clerk does not have to redeem the remaining five yuan worth of tickets.
Close 50 and wait. Continue to buy tickets
Pass 50 to ticket conductor 50 yuan
Wait for a while
Wait till Lin 20th, continue to buy tickets
Lin 20 paid 20 yuan to the conductor
The conductor asked Lin to wait for a while.
Li 10 gave the conductor 10 yuan, and the conductor sold the eleven tickets to Li for 5 yuan.
Close 50 and wait. Continue to buy tickets
Pass 50 to ticket conductor 50 yuan
Wait for a while
Wait till Lin 20th, continue to buy tickets
Lin 20 paid 20 yuan to the conductor. The conductor sold 21 tickets to Lin, with a change of 15 yuan.
Close 50 and wait. Continue to buy tickets
Pass 50 to ticket conductor 50 yuan
Wait for a while
Yang Wu gave the ticket clerk 5 yuan, but the ticket clerk did not have to redeem the five-day ticket for Yang.
Close 50 and wait. Continue to buy tickets
Pass 50 to the conductor for 50 yuan. The conductor sells fifty-Eleven tickets, with a change of 45 yuan.
Final Result
Yu Wu, Li Shilin, 20 Yang wuguan, 50