题目是:  采用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();
}
}
}
}
}
}疑问:压入和弹出都是不间断的,怎么实现随机两个动作。有没有更好的思路去实现?我是在校的学生,希望像大家多多学习。

解决方案 »

  1.   

    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;
    }
    }
    }
    不知道这样可不可以?
      

  2.   

    给你一个相似的程序,看懂了就明白了
    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(); }}
      

  3.   

    去掉sleep呗,不要去限制线程执行时间,试试