Multi-thread son's salt buying Problem

Source: Internet
Author: User

I just saw a multi-thread interview question and tried to write it.

Title: Simulate Mom's cooking. When cooking, I find that there is no salt, and ask my son to buy salt. Mom can continue cooking only after the salt is bought.

Problem Analysis: Since it is object-oriented programming, it is necessary to analyze the objects involved first. Obviously, this issue involves mothers, sons, and salt. Both Mom and son are thread classes, and shared resources are salt classes. Therefore, the methods of salt classes must be set as synchronization methods. The Code is as follows:

1 package COM. sync; 2/** 3 * Subject: Simulate Mom's cooking. When cooking, I find there is no salt. Ask my son to buy salt. 4 * the mother can continue the cooking process only after the salt is bought. 5 * @ author administrator 6*7 */8 9 // salt 10 class salt {11 private int saltnum = 0; // Number of salts, suppose there is no salt at first 12 13 // Cooking requires edible salt. Assume that a cooking requires 10 grams of salt 14 public synchronized int subsalt () {15 while (saltnum <= 0) {// If the salt is lost, wait for 16 system. out. println ("the salt is not enough. Wait ..... "); 17 try {18 this. wait (); 19} catch (interruptedexception e) {20 E. printstacktrace (); 21} 22} 23 minutes y (); 24 saltnum = saltnum-10; 25 system. out. println ("mom used 10 grams of salt for cooking! Remaining "+ saltnum +" grams of salt! "); 26 return saltnum; 27} 28 // salt, each time you buy saltnum grams of salt 29 public synchronized void addsalt (INT num) {30 While (saltnum> = 10) {// you do not need to buy 31 try {32 this. wait (); 33} catch (interruptedexception e) {34 E. printstacktrace (); 35} 36} 37 try {// suppose it takes 10 seconds to buy salt 38 Thread. sleep (10000); 39} catch (interruptedexception e) {40 E. printstacktrace (); 41} 42 saltnum = saltnum + num; 43 system. out. println ("son bought 100 grams of salt! "); 44 notify (); // After the salt is bought, wake up the waiting mother to continue cooking 45} 46} 47 48 // mother 49 class mother implements runnable {50 private salt; 51 public mother (salt) {52 this. salt = salt; 53} 54 public void run () {55 while (true) {56 salt. subsalt (); 57 try {58 thread. sleep (2000); 59} catch (interruptedexception e) {60 E. printstacktrace (); 61} 62} 63} 64 65} 66 67 // son 68 class son implements runnable {69 private salt; 70 public son (salt) {71 This. salt = salt; 72} 73 public void run () {74 while (true) {75 try {76 thread. sleep (3000); 77} catch (interruptedexception e) {78 E. printstacktrace (); 79} 80 salt. addsalt (100); 81} 82} 83 84} 85 86 public class saltandcook {87 88 public static void main (string [] ARGs) {89 salt = new salt (); 90 mother M = new mother (SALT); 91 son = new son (SALT); 92 new thread (m ). start (); 93 new thread (son ). start (); 94} 95 96}

 

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.