Chapter 8 of Java jobs

Source: Internet
Author: User

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:
  1. Package cc. nothing2012.blogservlet;
  2. Import java. util. arraylist;
  3. Import java. util. List;
  4. /* = Question: Imitate Example 8-10, design five people to buy tickets in queue, and the Ticket Selling Rules and queuing sequence are specified =========== */
  5. /*
  6. * 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.
  7. * The Order of the queue from start to end is: Guan 50, Lin 20, Yu 5, Li 10, and Yang 5.
  8. * The Order of buying tickets is: Yu 5, Li 10, Lin 20, Guan 50, and Yang Wu.
  9. */
  10. Public class ch8_4 {
  11. Public static list <string> theresult = new arraylist <string> (); // stores the final result of the ticket
  12. Public static void main (string [] ARGs ){
  13. String [] sname = {" 50", "Lin 20", "Yu 5", "Li 10", "Yang Wu "};
  14. Cinema c = new cinema (sname );
  15. Thread [] theads = {New thread (C), new thread (C), new thread (c ),
  16. New thread (C), new thread (c )};
  17. // For (INT I = 0; I <5; I ++) {// initialize five processes
  18. // Theads [I] = new thread (C );
  19. //}
  20. For (INT I = 0; I <5; I ++ ){
  21. Theads [I]. setname (sname [I]);
  22. }
  23. For (INT I = 0; I <5; I ++ ){
  24. Theads [I]. Start ();
  25. }
  26. While (true) {// print the final result
  27. Try {
  28. Thread. Sleep (1000 );
  29. } Catch (interruptedexception e ){
  30. E. printstacktrace ();
  31. }
  32. If (theresult! = NULL ){
  33. If (theresult. Size () = 5 ){
  34. Printmessage ("last result of buying tickets ");
  35. For (INT I = 0; I <5; I ++ ){
  36. System. Out. Print (theresult. Get (I) + "/t ");
  37. }
  38. Break;
  39. }
  40. }
  41. }
  42. }
  43. // Print the information.
  44. Public static void printmessage (Object O ){
  45. System. Out. println (O );
  46. }
  47. }
  48. // ======================================
  49. Class cinema implements runnable {
  50. Ticketseller seller;
  51. String [] sname;
  52. Public Cinema (string [] sname ){
  53. Seller = new ticketseller ();
  54. This. sname = sname;
  55. }
  56. Public void run (){
  57. If (thread. currentthread (). getname (). Equals (sname [0]) {
  58. Seller. sellticket (50 );
  59. } Else if (thread. currentthread (). getname (). Equals (sname [1]) {
  60. Seller. sellticket (20 );
  61. } Else if (thread. currentthread (). getname (). Equals (sname [2]) {
  62. Seller. sellticket (5 );
  63. } Else if (thread. currentthread (). getname (). Equals (sname [3]) {
  64. Seller. sellticket (10 );
  65. } Else if (thread. currentthread (). getname (). Equals (sname [4]) {
  66. Seller. sellticket (5 );
  67. }
  68. }
  69. }
  70. // ================ Ticket seller's ticket handling
  71. Class ticketseller {
  72. Int fivenumber = 1, tennumber = 0, twentynumber = 1, tynumber = 0; // each ticket clerk has 5 yuan and 20 yuan
  73. Public synchronized void sellticket (INT receivemoney ){
  74. String sname = thread. currentthread (). getname ();
  75. 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.
  76. Fivenumber + = 1;
  77. Ch8_4.printmessage (sname + "give the ticket clerk 5 yuan, the ticket clerk sells" + sname + "One ticket does not need to be redeemed ");
  78. Ch8_4.theresult.add (sname );
  79. } Else if (receivemoney = 10 ){
  80. While (fivenumber <1 ){
  81. Ch8_4.printmessage (sname + "10 yuan for conductor ");
  82. Ch8_4.printmessage ("small ticket clerk please" + sname + "side wait for a while ");
  83. Try {
  84. Wait ();
  85. } Catch (interruptedexception e ){
  86. E. printstacktrace ();
  87. }
  88. Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
  89. }
  90. // There is change
  91. Fivenumber = fivenumber-1;
  92. Tennumber + = 1;
  93. Ch8_4.printmessage (sname + "give the conductor 10 yuan, the conductor sells to" + sname + "one ticket, redeem 5 yuan ");
  94. Ch8_4.theresult.add (sname );
  95. } Else if (receivemoney = 20 ){
  96. While (fivenumber <1 | tennumber <1 ){
  97. Ch8_4.printmessage (sname + "Give ticket conductor 20 yuan/n ticket conductor" + sname + "wait for a while ");
  98. Try {
  99. Wait ();
  100. } Catch (interruptedexception e ){
  101. E. printstacktrace ();
  102. }
  103. Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
  104. }
  105. Fivenumber = fivenumber-1;
  106. Tennumber = tennumber-1;
  107. Twentynumber + = 1;
  108. Ch8_4.printmessage (sname + "20 yuan for the conductor, sold to" + sname + "one ticket, change to 15 yuan ");
  109. Ch8_4.theresult.add (sname );
  110. } Else if (receivemoney = 50 ){
  111. String flag = "";
  112. // Boolean isok = true;
  113. While (true ){
  114. If (fivenumber> 0) {// There are two combinations of 45 blocks
  115. If (twentynumber = 2 ){
  116. Flag = "2 twenty ";
  117. Break;
  118. // Isok = false;
  119. } Else if (twentynumber = 1 & tennumber = 2 ){
  120. Flag = "1 twenty ";
  121. // Isok = false;
  122. Break;
  123. }
  124. }
  125. Ch8_4.printmessage (sname + "Give ticket conductor 50 yuan/n ticket conductor" + sname + "wait for a while ");
  126. Try {
  127. Wait ();
  128. } Catch (interruptedexception e ){
  129. E. printstacktrace ();
  130. }
  131. Ch8_4.printmessage (sname + "end wait, continue to buy tickets ");
  132. }
  133. If (flag. endswith ("2 twenty ")){
  134. Fivenumber-= 1;
  135. Twentynumber = twentynumber-2;
  136. Required tynumber + = 1;
  137. } Else if (flag. Equals ("1 twenty ")){
  138. Fivenumber = fivenumber-1;
  139. Tennumber = tennumber-2;
  140. Twentynumber = twentynumber-1;
  141. }
  142. Ch8_4.printmessage (sname + "give the conductor 50 yuan, the conductor sells to" + sname + "one ticket, change to 45 yuan ");
  143. Ch8_4.theresult.add (sname );
  144. }
  145. Policyall ();
  146. }
  147. }

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

Related Article

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.