为了账号安全,请及时绑定邮箱和手机立即绑定

这段程序为啥在没有汉堡包时,无法输出if判断下的输出语句??急急急

这段程序为啥在没有汉堡包时,无法输出if判断下的输出语句??急急急

qq_沉默_47 2017-01-15 16:21:17
public class MakerAndSaler { public static void main(String[] args) { HMaker maker = new HMaker(); Hseller seller = new Hseller(); maker.start(); seller.start(); } } class box { static Object obj = new Object(); static int sale = 0; static int production = 5; static int totalMaterial = 10; } class HMaker extends Thread { // box myBox = new box(); public void make() { synchronized (box.obj) { box.production++; box.obj.notify(); } } public void run() { while (box.production < box.totalMaterial) { if (box.production > 0) { { System.out.println("厨师:" + "汉堡包来了(总共)" + (box.production - box.sale) + "个"); } } try { sleep(5000); } catch (Exception e) { } make(); } } } class Hseller extends Thread { public void sell() { if (box.production == 0) { System.out.println("营业员:顾客朋友们,请稍等一下,汉堡包没了"); } try { box.obj.wait(); } catch (Exception e) { } box.sale++; System.out.println("营业员:顾客好,汉堡包上来了,(总共卖了" + box.sale + "个"); } /*  * public void sell() { synchronized (box.obj) { if (box.production == 0) {  * System.out.println("营业员:顾客朋友们,请稍等一下,汉堡包没了"); } try { box.obj.wait(); }  * catch (Exception e) {  *   * } box.production++; System.out.println("营业员:顾客好,汉堡包上来了,(总共卖了" + box.sale  * + "个"); } }  */ public void run() { while (box.production > box.sale) { try { sleep(1000); } catch (Exception e) { } sell(); } } }public class MakerAndSaler { public static void main(String[] args) { HMaker maker = new HMaker(); Hseller seller = new Hseller(); maker.start(); seller.start(); } } class box { static Object obj = new Object(); static int sale = 0; static int production = 5; static int totalMaterial = 10; } class HMaker extends Thread { // box myBox = new box(); public void make() { synchronized (box.obj) { box.production++; box.obj.notify(); } } public void run() { while (box.production < box.totalMaterial) { if (box.production > 0) { { System.out.println("厨师:" + "汉堡包来了(总共)" + (box.production - box.sale) + "个"); } } try { sleep(5000); } catch (Exception e) { } make(); } } } class Hseller extends Thread { public void sell() { if (box.production == 0) { System.out.println("营业员:顾客朋友们,请稍等一下,汉堡包没了"); } try { box.obj.wait(); } catch (Exception e) { } box.sale++; System.out.println("营业员:顾客好,汉堡包上来了,(总共卖了" + box.sale + "个"); } /*  * public void sell() { synchronized (box.obj) { if (box.production == 0) {  * System.out.println("营业员:顾客朋友们,请稍等一下,汉堡包没了"); } try { box.obj.wait(); }  * catch (Exception e) {  *   * } box.production++; System.out.println("营业员:顾客好,汉堡包上来了,(总共卖了" + box.sale  * + "个"); } }  */ public void run() { while (box.production > box.sale) { try { sleep(1000); } catch (Exception e) { } sell(); } } }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 755 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信