- public class Cooking {
- public static void main(String[] args) {
- new Thread(new Mother()).start();
- }
- }
- class Salt {
- boolean flag = false;
- }
- class Mother implements Runnable {
- public void run() {
- System.out.println("开始做饭了");
- System.out.println("妈妈说:\"没盐了,儿子,买盐去!\"");
- Thread son = new Thread(new Son());
- son.start();
- try {
- son.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("盐买回来,妈妈继续做饭。");
- }
- }
- class Son implements Runnable {
- public void run() {
- System.out.println("儿子:\"好的,马上去买盐!\"");
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 没有太明白楼主的代码,private Salt s, s.notify();什么意思。买盐的问题可以参考一下以上代码。 |