消费者模式java多线程之哲学家进餐问题(5人5筷)

消费者模式之哲学家进餐问题

/**
* Description: 哲学家吃饭

* Copyright (c) , 2016, Jansonxu

* This program is protected by copyright laws.

* Program Name:PhilosopheEat.java

* Date: 2016年1月26日
*
* @author 李阳
* @version : 1.0
*/
package PhysicsEatFish;

public class PhilosopheEat {
class Chopstic{
private int id;//编号
private boolean state;//true 被占用 false 未被占用
/**
*
*/
public Chopstic() {
// TODO Auto-generated constructor stub
}
public Chopstic(int id, boolean state) {
super();
this.id = id;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
@Override
public String toString() {
return “Chopstic [id=” + id + “, state=” + state + “]”;
}

}
//哲学家线程类
class Philosophe extends Thread{
    private String pname;
    private Chopstic left;
    private Chopstic right;

    /* (non-Javadoc)
     * @see java.lang.Thread#toString()
     */
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

    /**
     * 
     */
    public Philosophe() {
        // TODO Auto-generated constructor stub
    }



    public Philosophe(String pname, Chopstic left, Chopstic right) {
        super();
        this.pname = pname;
        this.left = left;
        this.right = right;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Chopstic getLeft() {
        return left;
    }

    public void setLeft(Chopstic left) {
        this.left = left;
    }

    public Chopstic getRight() {
        return right;
    }

    public void setRight(Chopstic right) {
        this.right = right;
    }

    @Override
    public void run() {
        while(true){
            synchronized ("") {
                //判断左筷子是否被占用  
                if(left.isState()){
                    try {
                        "".wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 走到下边说明未被占用设置状态为 true
                left.setState(true);

            //a 被占用 就等待
            //b 否则就拿起

            //2 判断右筷子是否被占用
            //a 占用 放下左筷子
            //b 占用 放下左筷子
            if(right.isState()){
                left.setState(false);
                //放下左筷子之后 也要等待
                try {
                    "".wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //走到这 说明 右筷子也未被使用 设置状态为 true
            right.setState(true);
            System.out.println("哲学家【"+pname+"】拿起了 左筷子 【"+left+"】 右筷子【"+right+"】 开始进餐");
            //进餐后 放下左右的筷子 通知别的哲学家 吃东西
            left.setState(false);
            right.setState(false);
            //通知别的哲学家 进餐
            "".notifyAll();

        }   
            //通知之后 睡会 
            // 写在外边 让其他人 获得筷子 
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
// 最完美的时刻  应该是  两个一起吃 同一时刻 两个吃  
public static void main(String[] args) {
    //步骤
    //构建外部类对象
    PhilosopheEat outer=new PhilosopheEat();

    //构建5个筷子
    Chopstic one=outer.new Chopstic(1,false);
    Chopstic two=outer.new Chopstic(2,false);
    Chopstic three=outer.new Chopstic(3,false);
    Chopstic four=outer.new Chopstic(4,false);
    Chopstic five=outer.new Chopstic(5,false);

    //构建五个哲学家
    Philosophe  kz=outer.new Philosophe("孔子",one,five);
    Philosophe  mz=outer.new Philosophe("孟子",two,one);
    Philosophe  lz=outer.new Philosophe("老子",three,two);
    Philosophe  hfz=outer.new Philosophe("韩非子",four,three);
    Philosophe  sz=outer.new Philosophe("孙子",five,four);

    //开启线程
     kz.start();
     mz.start();
     lz.start();
     hfz.start();
     sz.start();


}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的BlockingQueue来实现消费者生产者问题。BlockingQueue是一个线程安全的队列,它提供了put()和take()方法,分别用于生产和消费。当队列为空时,take()方法会阻塞线程,直到队列中有元素;当队列满时,put()方法会阻塞线程,直到队列中有空闲位置。 以下是一个简单的示例代码: import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class ProducerConsumerExample { private static final int QUEUE_CAPACITY = 10; private static final int NUM_PRODUCERS = 2; private static final int NUM_CONSUMERS = 3; public static void main(String[] args) { BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(QUEUE_CAPACITY); for (int i = 0; i < NUM_PRODUCERS; i++) { new Thread(new Producer(queue)).start(); } for (int i = 0; i < NUM_CONSUMERS; i++) { new Thread(new Consumer(queue)).start(); } } static class Producer implements Runnable { private final BlockingQueue<Integer> queue; public Producer(BlockingQueue<Integer> queue) { this.queue = queue; } @Override public void run() { try { for (int i = 0; i < 10; i++) { queue.put(i); System.out.println("Produced: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } } static class Consumer implements Runnable { private final BlockingQueue<Integer> queue; public Consumer(BlockingQueue<Integer> queue) { this.queue = queue; } @Override public void run() { try { while (true) { Integer value = queue.take(); System.out.println("Consumed: " + value); Thread.sleep(2000); } } catch (InterruptedException e) { e.printStackTrace(); } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值