黑马程序员技术交流社区
标题:
黑马基础题妈妈做饭的
[打印本页]
作者:
tanzhiqiu
时间:
2013-8-27 18:49
标题:
黑马基础题妈妈做饭的
package com.itheima;
public class Test11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Solt s=new Solt();
new Thread(new Tucker(s)).start();
new Thread(new Sonera(s)).start();
}
}
class Solt{
String name;
boolean f=false;
}
class Tucker implements Runnable{
private Solt s;
public Tucker(Solt s) {
super();
this.s = s;
}
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(s){
if(s.f){
try {
s.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println("妈妈做饭,发现没有盐");
s.f=true;
s.notify();
try {
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("妈妈继续做饭");
}
}
}
class Sonera implements Runnable{
private Solt s;
public Sonera(Solt s) {
super();
this.s = s;
}
public void run() {
// TODO Auto-generated method stub
synchronized(s){
if(!(s.f)){
try {
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("儿子去买盐");
s.f=true;
s.notify();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("买盐回来");
s.notify();
}
}
}
自己写的,不知道哪里还有改进的。大家看看
作者:
似鱼非鱼
时间:
2013-8-27 19:34
没注释....
作者:
肥猫
时间:
2013-8-27 20:14
哪要这么才长啊,至少可以减一半多的。
作者:
328078121
时间:
2013-8-29 07:51
入门测试,其实不需要整这复杂,关键考察你有门有线程的概念。
if()//如果没盐
{
thread t=new thread();//开启一个线程
t.join();//让这个线程运行完毕
t.start();
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2