请教一个生产者消费者问题

ahhongxin 2012-10-26 09:13:37
题目是:

采用Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作,
其最大容量是12颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,
它不断从枪膛中射出子弹。
要求:
(1)给出分析过程说明。
(2)程序输出,要模拟体现对枪膛的压入和射出操作;
(3)设计程序时应考虑到两个线程的同步问题。




public class BT4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Gun 枪膛=new Gun(5);
枪膛.pop.start();
枪膛.push.start();
}

}
class Gun implements Runnable{
private int cartridge;
private int Max=12;
private int Min=0;
Thread pop,push;
Gun(int cartridge){
this.cartridge=cartridge;
push=new Thread(this);
pop=new Thread(this);
}
public void run(){
if(Thread.currentThread()==push){
while(true){
synchronized(this){
System.out.println("正在压入");
cartridge=cartridge+1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("已压入");
System.out.println ("还有"+cartridge+"发子弹\n");
}
if(cartridge==Max){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
if(Thread.currentThread()==pop){
while(true){
synchronized(this){
System.out.println("正在 射出");
cartridge=cartridge-1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("已射出");
System.out.println ("还有"+cartridge+"发子弹\n");
}
if(cartridge==Min){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}



疑问:压入和弹出都是不间断的,怎么实现随机两个动作。有没有更好的思路去实现?

我是在校的学生,希望像大家多多学习。

...全文
171 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
changtianshuiyue 2012-11-02
  • 打赏
  • 举报
回复
去掉sleep呗,不要去限制线程执行时间,试试
leabean 2012-11-01
  • 打赏
  • 举报
回复
给你一个相似的程序,看懂了就明白了
public class TestTread {

/**
* @param args
*/
public class Q
{
private String name="陈琼";
private String sex="女";
boolean bFul=false;
public synchronized void put(String name,String sex)
{
if(bFul)
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.name=name;
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
this.sex=sex;
bFul=true;
notify();
}
public synchronized void get()
{
if(!bFul)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+"-->"+sex);
bFul=false;
notify();
}

}
public class Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
q.put("刘德华", "男");
else
q.put("陈琼", "女");
i=(i+1)%2;
}
}

}
public class Customer implements Runnable
{
Q q=null;
public Customer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
{
q.get();
}
}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Q q=new Q();
new Thread(new Producer(q)).start();
new Thread(new Customer(q)).start();

}

}
成长之路2020 2012-10-31
  • 打赏
  • 举报
回复
class Bt4
{
public static void main(String[] args)
{
Qun q = new Qun();
new Thread(new push(q)).start();

new Thread(new pop(q)).start();
}
}

// 生产者
class push implements Runnable
{
Qun q = null;
public push(Qun q)
{
this.q = q;
}
public void run()
{
while(true)
q.push();
}
}
// 消费者
class pop implements Runnable
{
Qun q = null;
public pop (Qun q)
{
this.q = q;
}
public void run()
{
while(true)
q.pop();

}
}
// 定义枪
class Qun
{
final int BORE_MAX = 12;
int BroeNums = 0;
boolean flag = true;

// 压弹
public synchronized void push()
{
if(flag)
{
for(int i = 0; i < BORE_MAX; i++)
{
BroeNums++;
System.out.println("正在压入"+ BroeNums);
}
flag = false;
}
}


// 弹去
public synchronized void pop()
{
if(flag == false)
{
for(int i = BORE_MAX; i >0 ; i--)
{
System.out.println("正在发生" + BroeNums);
BroeNums--;
}
flag = true;
}
}
}
不知道这样可不可以?
aimsgmiss 2012-10-26
  • 打赏
  • 举报
回复
关注中...顺便学习哈
ahhongxin 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
去掉所有sleep
[/Quote]

很明显,这样应该不行。
Kanepan 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

去掉所有sleep
[/Quote]

+10086
ldq67123 2012-10-26
  • 打赏
  • 举报
回复
去掉所有sleep

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧