模拟妈妈做饭,做饭时发现没有盐了,让儿子去买盐(假设买盐需要3分钟),只有盐买回来之后,妈妈才能继续做饭的过程。
解决方案 »
- String类中的substring(String.length())怎么能取为空?
- 为什么【三元运算符】不能独立成句,有返回值的方法却可以独立成句?
- 大家看看这个为什么输出的是 null 啊?
- 在线游戏系统设计方面的疑问
- 刚才经理问我的eclipse中开发JSP的问题,我不懂
- 软件编程---数据库----操作系统----我该学什么 IT老鸟请指点迷经!!!!!!
- 准备考JP,可心里没底,那位大哥愿意给小弟提供点资料(特别是讲义和题库)?
- 请问可以用那个函数把ip地址转化成一个数字么?
- 两种系统时间输出的区别?
- cannot convert from Map<String,String[]> to Map<String,Object>
- Java线程问题
- 帮忙看看,我的服务器为什么不能回车发消息
public static void main(String[] args) {
Thread mother=new Thread(new Mother());
mother.start();
}}
class Home{
int salt=0;
}
class Mother implements Runnable{
private Home home=new Home();
public void run() {
try {
if(home.salt==0){
Thread th=new Thread(new Son(home));
th.start();
System.out.println("wait..... ");
Thread.sleep(10000);
}
System.out.println("salt is "+home.salt);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Son implements Runnable{
private Home home;
public Son(Home h){
this.home=h;
}
public void run() {
home.salt=1000;
}
}
只能出现等待的效果,但感觉很不爽。
那位大侠指教一下
/** 盐的用量 */
final int SaltUseage = 40;
volatile boolean cooking = true;
Home home;
public void run(){
try {
while(cooking){
Salt salt = home.salt;
if(salt.value<SaltUseage){
home.son.buySalt();
waiting();
}
System.out.println("[妈妈]盐 ==>> 当前量:"+salt.value+" 用去量:"+SaltUseage+" 剩余量:"+(salt.value-SaltUseage));
salt.value -= SaltUseage;
otherThings();
}
} catch (Exception e) {
cooking = false;
}
}
private void otherThings() {
try {
sleep(1000);
} catch (Exception e) {
cooking = false;
}
}
private synchronized void waiting() throws InterruptedException {
System.out.println("[妈妈]盐 ==>> 当前量:"+home.salt.value+" 等待儿子去买盐。");
home.mother.wait();
}
private synchronized void notice(){
home.mother.notify();
}
}
static class Son extends Thread{
/** 盐的购买量 */
final int SaltPurchases = 60;
volatile boolean free = true;
Home home;
public void run(){
try {
while(free){
waiting();
sleep(2000);
System.out.println("[儿子]盐 ==>> 当前量:"+home.salt.value+" 购买量:"+SaltPurchases+" 剩余量:"+(home.salt.value+SaltPurchases));
home.salt.value += SaltPurchases;
notice();
}
} catch (Exception e) {
free = false;
}
}
public synchronized void buySalt(){
notify();
}
private void notice() {
System.out.println("[儿子]盐 ==>> 当前量:"+home.salt.value+" 告知妈妈盐已买到。");
home.mother.notice();
}
private synchronized void waiting() throws InterruptedException {
System.out.println("[儿子]盐 ==>> 当前量:"+home.salt.value+" 等待下次购买。");
wait();
}
}
static class Salt{
int value = 90;
}
static class Home{
Mother mother = new Mother();
Son son = new Son();
Salt salt = new Salt();
public Home(){
mother.home = this;
son.home = this;
}
public void startCooking(){
mother.start();
son.start();
}
public void stopCooking(){
mother.cooking=false;
son.free=false;
mother.interrupt();
son.interrupt();
}
}
/**
* 测试用例
*/
public static void main(String[] args) throws Exception {
Home home = new Home();
home.startCooking();
Thread.sleep(15000);
home.stopCooking();
}}
做饭时发现没有盐了: if (没盐)
让儿子去买盐:Thread son = new 儿子(); son.start();
只有盐买回来之后:son.join(); // 嗯,其实就这样就够了,反正题目也没其它复杂要求
妈妈才能继续做饭的过程:做饭()
嗯,其实核心就是:因为没有并发要求,所以 Thread.join() 就够了。
楼主其实就是要生产者、消费者的例子,只不过他表述的太简单了。
如果按你的join方法,完全可以不用线程,直接if再调用买盐方法岂不更简单?
多线程跟join并不冲突,否则join这个函数就被取消了,join函数之所以存在,就是因为它是有价值的。所以它仍然体现了多线程的意义,它只不过没有体现并发的意义。如果楼主真的要生产者消费者的例子,只能说他这个例子举的不好,应该举个厨房里厨师和采购人的例子应该会更合适。
public class Mouther extends Thread{
private Time t;
public Mouther(Time t){
this.t=t;
}
public void run(){
synchronized (t) {
System.out.println("做饭..");
if(t.isHaveSalt()==false){
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("有盐做饭了");
System.exit(1);
}
}
}
}public class Son extends Thread{
private Time t;
public Son(Time t){
this.t=t;
}
public void run(){
synchronized (t) {
System.out.println("sonrun");
if(t.isHaveSalt()==false){
System.out.println("买盐");
for(int i=this.t.getTime();i>=0;i--){
System.out.println("时间:"+i);
}
System.out.println(t.getTime());
System.out.println("买完.....");
t.notifyAll();
}
}
}
public static void main(String[] args) {
Time t= new Time();
t.setTime(10000);
t.setHaveSalt(false);
Son s = new Son(t);
Mouther m = new Mouther(t);
m.start();
s.start();
}
}
public class Time {
private int time;
private boolean haveSalt;
public boolean isHaveSalt() {
return haveSalt;
} public void setHaveSalt(boolean haveSalt) {
this.haveSalt = haveSalt;
} public int getTime() {
return time;
} public void setTime(int time) {
this.time = time;
}
}