Java多线程(有一个厨师负责生产馒头,放在一个容量为10的篮子里,有3个学生负责吃馒头。 当篮子已经装满的时候厨师要暂停生产,当篮子为空的时候学生暂停吃馒头, 要求:使用多线程模拟该过程)

多线程作业

有一个厨师负责生产馒头,放在一个容量为10的篮子里,有3个学生负责吃馒头。
当篮子已经装满的时候厨师要暂停生产,当篮子为空的时候学生暂停吃馒头,
要求:使用多线程模拟该过程。(需要线程同步)

1.Consumer类

class Consumer implements Runnable {
    
    

    SyncStack ss = null;

    Consumer(SyncStack ss) {
    
    

        this.ss = ss;

    }



    public void run() {
    
    

        for(int i=0; i<20; i++) {
    
    

            MaTou wt = ss.pop();

            //System.out.println(Thread.currentThread().getName()+"消费了: " + wt);

            try {
    
    

                //Thread.sleep((int)(Math.random() * 1000));

                Thread.sleep(1000);

            } catch (InterruptedException e) {
    
    

                e.printStackTrace();

            }

        }

    }

}

2.Producer类

class Producer implements Runnable {
    
    

    SyncStack ss = null;

    Producer(SyncStack ss) {
    
    

        this.ss = ss;

    }



    public void run() {
    
    

        for(int i=0; i<60; i++) {
    
    

            MaTou wt = new MaTou(i);

            ss.push(wt);

            //System.out.println("厨师生产了:" + wt);

            try {
    
    

                //Thread.sleep((int)(Math.random() * 200));

                Thread.sleep(500);

            } catch (InterruptedException e) {
    
    

                e.printStackTrace();

            }

        }

    }

}

3.MaTou类

public class MaTou {
    
    

    int id;

    MaTou(int id) {
    
    

        this.id = id;

    }

    public String toString() {
    
    

        return id+"号馒头: ";

    }

}

4.SyncStack类


class SyncStack {
    
    

    int index = 0;

    MaTou[] arrWT = new MaTou[10];



    public synchronized void push(MaTou wt) {
    
    

        String s = Thread.currentThread().getName();

        while(index == arrWT.length) {
    
    

            try {
    
    

                System.out.println("篮子已满,暂停生产");

                this.wait();

                System.out.println(s+"继续生产");

            } catch (InterruptedException e) {
    
    

                e.printStackTrace();

            }

        }



        arrWT[index] = wt;

        index ++;

        System.out.println("厨师生产了:" + wt);

        this.notifyAll();

    }



    public synchronized MaTou pop() {
    
    

        String s = Thread.currentThread().getName();

        while(index == 0) {
    
    

            try {
    
    

                System.out.println("篮子已空,暂停消费");

                this.wait();

                System.out.println(s+"继续消费");

            } catch (InterruptedException e) {
    
    

                e.printStackTrace();

            }

        }



        index--;

        System.out.println(s+"消费了: " +arrWT[index]);

        this.notifyAll();

        return arrWT[index];



    }

}

5.ProducerConsumer类


public class ProducerConsumer {
    
    

        public static void main(String[] args) {
    
    

        SyncStack ss = new SyncStack();

        Producer p = new Producer(ss);

        Consumer c = new Consumer(ss);

        Thread zhang=new Thread(c);

        zhang.setName("张飞");

        Thread wang=new Thread(c);

        wang.setName("王朔");

        Thread li=new Thread(c);

        li.setName("李刚");

        Thread chushi=new Thread(p);

        chushi.setName("李大厨师");

        chushi.start();

        while(!chushi.isAlive()){
    
    }

        zhang.start();



        wang.start();



        li.start();

        }

        }

写在最后
这个有点令人头疼的作业,写到吐了,特此粘出来“参考答案”,供各位研究和学习。

猜你喜欢

转载自blog.csdn.net/qq_45769949/article/details/111770344