生产者和消费者课程设计基于Java可视化界面

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《操作系统》课程设计生产者和消费者问题实践

系院:计算机科学系

学生姓名:***

学号:**********

专业:计算机科学与技术

年级:三年级

完成日期:2010年12月

指导教师:***

public JTextArea jt=new JTextArea();

//构造方法并对私有变量赋值

public Consumer(Share s){

shared=s;

}

//重写父类方法

public void run(){

int value=0;

//默认十次循环,消费者将从共享资源内取出相应资源

for(int i=1;i<11;i++){

value=shared.get();

jieguo[i]="消费者第"+i+"次消费"+" 消费者获得的生产数据:"+value+"\n";

jt.append(jieguo[i]);

try{

sleep(1000);

}catch(InterruptedException e){}

}

}

}

5、实验运行图;

同步运行结果

互斥运行结果

6、实验结果分析;

只有在生产者生产了产品并将产品存放到缓冲池中消费者才能去消费,当缓冲池为空时消费者不能消费

六、结论(应当准确、完整、明确精练;也可以在结论或讨论中提出建议、设想、

尚待解决问题等。)

七、参考文献

【1】邵丽萍,邵光亚,张后扬编著.Java语言程序设计,清华大学出版社。2008年8月第3版

【2】汤小丹,梁红兵,哲凤屏,汤子瀛编著.计算机操作系统,西安电子科技大学出版社。2007年5月第3版

附:课程设计源代码:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//窗口类

class window extends JFrame{

public JFrame jf;

public JPanel jp3;

public ScrollPane sp1,sp2,sp3;

public Container c;

window(){

jf=new JFrame();

jp3=new JPanel();

sp1=new ScrollPane();

sp2=new ScrollPane();

sp3=new ScrollPane();

c=getContentPane();

c.setLayout(new GridLayout(2,2,10,10));

jf.add(c);

c.add(sp1);

c.add(sp2);

c.add(jp3);

c.add(sp3);

jf.setSize(400,300);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

//Share 类

class Share{

private int u,jishu=0;

private boolean available=false;

public JTextArea jt=new JTextArea("==========生产消费状态==========\n\n"); //同步方法

public synchronized int get(){

jishu++;

while(available==false){

try{wait();}

catch(InterruptedException e){}

jt.append(jishu+" Share中没有资源,消费者等待……\n");

}

available=false;

notifyAll();

jt.append(jishu+" 正在唤醒生产者生产……\n");

return u;

}

public synchronized void put(int value){

jishu++;

while(available==true){

try{wait();}

catch(InterruptedException e){}

jt.append(jishu+" Share中已有资源,生产者等待……\n");

}

u=value;

available=true;

notifyAll();

jt.append(jishu+" 正在唤醒消费者消费……\n");

}

//互斥方法

public int hget(){

jishu++;

jt.append(jishu+" 消费者正在消费资源……\n");

return u;

}

public void hput(int value){

jishu++;

jt.append(jishu+" 生产者正在生产资源……\n");

u=value;

}

}

//生产者类

class Producer extends Thread{

private Share shared;

public String jieguo[]=new String[11];

public JTextArea jt=new JTextArea("==========生产者进程==========\n\n");

public Producer(Share s){

shared=s;

}

public void run(){

for(int i=1;i<11;i++){

shared.put(i);

jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";

jt.append(jieguo[i]);

try{

//sleep((int)(Math.random()*100));

sleep(1000);

}catch(InterruptedException e){}

}

}

}

class hProducer extends Thread{

private Share shared;

public String jieguo[]=new String[11];

public JTextArea jt=new JTextArea("==========生产者线程==========\n\n");

public hProducer(Share s){

shared=s;

}

public void run(){

for(int i=1;i<11;i++){

shared.hput(i);

jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";

jt.append(jieguo[i]);

try{

//sleep((int)(Math.random()*100));

sleep(1000);

相关文档
最新文档