Demo entry 6762282

java

   

Submitted by anonymous on Oct 11, 2018 at 12:18
Language: Java. Code size: 2.9 kB.

package test1;

import java.util.Random;

public class Test1 {
    private static Integer count = 0;//全局变量count记录产品的数量,初始值为1
    private static final Integer FULL = 10;//全局常量,生产的最大容量,初始值为10
    private static String LOCK = "lock";//定义锁,通过对这个对象的访问实现互斥

    /**
     * Main函数
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        Test1 test1 = new Test1();
        for (int i = 0; i <= 5; i++){//创建多个生产者和消费者
            new Thread(test1.new Producer()).start();//将生产者加入就绪队列
            Thread.sleep((int)Math.random()*10000);//暂停0~10秒的任意时间
            new Thread(test1.new Producer()).start();//再创建一个生产者并加入就绪队列,使生产者的数量大于消费者
            Thread.sleep((int)Math.random()*10000);
            new Thread(test1.new Consumer()).start();//将消费者加入就绪队列
        }

    }

    /**
     * 生产者类,通过实现Runnable接口创建线程
     */
    class Producer implements Runnable {
        @Override
        public void run() { //重写run()方法,这个是Producer线程所要做的事情
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(3000);//暂停3秒
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //使用java的synchronized实现对LOCK对象的互斥访问
                synchronized (LOCK) {
                    //判断当前count是否已经满了,如果满了,令当前线程等待,否则生产一个产品(count+1)
                    while (count == FULL) {
                        try {
                            LOCK.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    count++;
                    System.out.println(Thread.currentThread().getName() + "生产者生产,目前总共有" + count);
                    //生产完毕,唤醒所有等待的线程
                    LOCK.notifyAll();
                }
            }
        }
    }
    class Consumer implements Runnable {
        @Override
        public void run() {//重写run()方法,这个是Consumer线程所要做的事情
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //使用java的synchronized实现对LOCK对象的互斥访问
                synchronized (LOCK) {
                    //判断当前count是否为0,如果为0了,令当前线程等待,否则消费一个产品(count-1)
                    while (count == 0) {
                        try {
                            LOCK.wait();
                        } catch (Exception e) {
                        }
                    }
                    count--;
                    System.out.println(Thread.currentThread().getName() + "消费者消费,目前总共有" + count);
                    //消费完毕,唤醒所有等待的线程
                    LOCK.notifyAll();
                }
            }
        }
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).