//设置生产者生产完成回到起点 if(n>=599&&i==0) {
x[0]=20;//生产者回到起点
Id.push(pictureProduce);//添加到栈中添加一个产品,此时缓冲区并没有产品
image=produce();
pictureProduce=new JLabel(image); add(pictureProduce); }
else if(n==20&&i==1||n>=599&&i==1) {
if(n==20&&i==1)//如果是可以消费 {
if(!Id.isEmpty())//当缓冲区不为空时,可以消费 pictureConsume=consume(); else
pictureConsume=null; }
//System.out.println(n); if(x[1]>=599&&i==1) {
pictureConsume.setVisible(true); x[1]=20; } }
return true; }
else return false; }
public ImageIcon produce() {
return new ImageIcon(\ }
public JLabel consume() {
return Id.pop(); }
public boolean isfull()//判断缓冲区是否已经满了 {
if(Id.size()==max) return true;
else return false; }
public DrawPanel() {
try {
b[0]=false;//初始状态既不可生产也不可消费 b[1]=false;
x[0]=x[1]=20;//设置三个起始横坐标 int y[]={20,230,440}; setLayout(null);
image=produce();
pictureProduce=new JLabel(image);//为生产者图片赋值
pictureProduce.setBounds(x[0], y[0], 120, 120);//初始状态下的生产者图片位置
//实例化生产与消费者对象
threadProduce=new mythread(0, this); threadComsume=new mythread(1, this);
//启动线程
threadProduce.start(); threadComsume.start();
add(pictureProduce);
btStartProduce=new JButton(\开始生产\
btStartProduce.addActionListener(this);//添加本类中的的 actionPerformed(ActionEvent arg0)方法
btnStartCosume=new JButton(\开始消费\
btnStartCosume.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub b[1]=true; } });
btnStopConsume=new JButton(\停止消费\
btnStopConsume.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[1]=false;
} });
btnStopProduce=new JButton(\停止生产\
btnStopProduce.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[0]=false; } });
add(btStartProduce); add(btnStartCosume); add(btnStopConsume); add(btnStopProduce); repaint();
} catch (Exception e) {
// TODO: handle exception } }
public void paint(Graphics g) {
super.paint(g);
int y[]={20,230,440};//三个矩形的纵坐标 btStartProduce.setBounds(100,170,150,30); btnStartCosume.setBounds(100,600,150,30); btnStopConsume.setBounds(300,600,150,30); btnStopProduce.setBounds(300,170,150,30);
g.setColor(Color.gray);
g.drawRect(20, y[0], 575, 120);//绘制生产矩形 g.drawRect(20, y[1], 870, 120);//绘制缓冲区矩形 g.drawRect(20, y[2], 575, 120);//绘制消费矩形
g.setColor(Color.black);
Font font=new Font(\楷体\ g.setFont(font);
g.drawString(\刘克 软件二班\ }
//更新界面显示内容 public void Update() {
int y[]={20,230,440}; if(pictureProduce!=null) {
if(!Id.isEmpty()) {
Id.lastElement().setBounds(20+(Id.size()-1)*125,230,120,120); }
pictureProduce.setBounds(x[0], y[0], 120, 120);//再次设置生产图片位置
}
else {
x[0]=20;
b[0]=false;//为空则不能生产 }
//只要消费者图片不为空就可以显示 if(pictureConsume!=null) {
pictureConsume.setBounds(x[1], y[2], 120, 120); }
else {
x[1]=20; b[1]=false; }
//如果缓冲区满了,则不可生产 if(isfull()) {
b[0]=false; }
repaint(); }
@Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try {
if(!threadProduce.isAlive())//唤醒生产线程 threadProduce.notify(); b[0]=true;
} catch (Exception e2) {
// TODO: handle exception }
repaint();
} }
2.mythread类代码
public class mythread extends Thread {
private int id;//序号 private int n;//坐标位置 private DrawPanel panel;
public mythread(int i,DrawPanel d) {
id=i; panel=d; }
public int get_n() {
return n; }
public void run() {
try {
while (true) {
n=panel.get_x(id);//得到初始位置
if(panel.set_x(id,n))//如果可以生产或者可以消费 {
panel.Update(); ++n;
n=n`0;//不断改变X[i],即生产或消费者的横坐标位置 Thread.sleep(4); panel.set_x(id,n); } }
} catch (Exception e) {
// TODO: handle exception } } }
3.DrawFrame类代码
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
public class DrawFrame extends JFrame {
public DrawPanel panel; public DrawFrame() {
setTitle(\生产者与消费者问题\ setSize(950, 700); panel=new DrawPanel();
//panel.setBackground(Color.green); Container container=getContentPane(); container.add(panel);//把面板添加到容器里 } }
4.ProcessMain类代码
import javax.swing.JFrame;
public class ProconMain {
public static void main(String[] args) { // TODO Auto-generated method stub DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库先来先服务问题java程序设计报告(6)在线全文阅读。
相关推荐: