生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,生产者往存储空间中添加产品,消费者从存储空间中取走产品,当存储空间为空时,消费者阻塞,当存储空间满时,生产者阻塞。
现在用四种方式来实现生产者消费者模型
wait()和notify()方法的实现
这也是最简单最基础的实现,缓冲区满和为空时都调用wait()方法等待,当生产者生产了一个产品或者消费者消费了一个产品之后会唤醒所有线程。
/*** 生产者和消费者,wait()和notify()的实现* @author ZGJ* @date 2017年6月22日*/publicclassTest1 { privatestaticIntegercount=0; privatestaticfinalIntegerFULL=10; privatestaticStringLOCK="lock"; publicstaticvoidmain(String[] args) { Test1test1=newTest1(); newThread(test1.newProducer()).start(); newThread(test1.newConsumer()).start(); newThread(test1.newProducer()).start(); newThread(test1.newConsumer()).start(); newThread(test1.newProducer()).start(); newThread(test1.newConsumer()).start(); newThread(test1.newProducer()).start(); newThread(test1.newConsumer()).start(); } classProducerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (Exceptione) { e.printStackTrace(); } synchronized (LOCK) { while (count==FULL) { try { LOCK.wait(); } catch (Exceptione) { e.printStackTrace(); } } count++; System.out.println(Thread.currentThread().getName() +"生产者生产,目前总共有"+count); LOCK.notifyAll(); } } } } classConsumerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (InterruptedExceptione) { e.printStackTrace(); } synchronized (LOCK) { while (count==0) { try { LOCK.wait(); } catch (Exceptione) { } } count--; System.out.println(Thread.currentThread().getName() +"消费者消费,目前总共有"+count); LOCK.notifyAll(); } } } } }
结果:
Thread-0生产者生产,目前总共有1Thread-4生产者生产,目前总共有2Thread-3消费者消费,目前总共有1Thread-1消费者消费,目前总共有0Thread-2生产者生产,目前总共有1Thread-6生产者生产,目前总共有2Thread-7消费者消费,目前总共有1Thread-5消费者消费,目前总共有0Thread-0生产者生产,目前总共有1Thread-4生产者生产,目前总共有2Thread-3消费者消费,目前总共有1Thread-6生产者生产,目前总共有2Thread-1消费者消费,目前总共有1Thread-7消费者消费,目前总共有0Thread-2生产者生产,目前总共有1Thread-5消费者消费,目前总共有0Thread-0生产者生产,目前总共有1Thread-4生产者生产,目前总共有2Thread-3消费者消费,目前总共有1Thread-7消费者消费,目前总共有0Thread-6生产者生产,目前总共有1Thread-2生产者生产,目前总共有2Thread-1消费者消费,目前总共有1Thread-5消费者消费,目前总共有0Thread-0生产者生产,目前总共有1Thread-4生产者生产,目前总共有2Thread-3消费者消费,目前总共有1Thread-1消费者消费,目前总共有0Thread-6生产者生产,目前总共有1Thread-7消费者消费,目前总共有0Thread-2生产者生产,目前总共有1
可重入锁ReentrantLock的实现
java.util.concurrent.lock 中的 Lock 框架是锁定的一个抽象,通过对lock的lock()方法和unlock()方法实现了对锁的显示控制,而synchronize()则是对锁的隐性控制。
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响,简单来说,该锁维护这一个与获取锁相关的计数器,如果拥有锁的某个线程再次得到锁,那么获取计数器就加1,函数调用结束计数器就减1,然后锁需要被释放两次才能获得真正释放。已经获取锁的线程进入其他需要相同锁的同步代码块不会被阻塞。
importjava.util.concurrent.locks.Condition; importjava.util.concurrent.locks.Lock; importjava.util.concurrent.locks.ReentrantLock; /*** 生产者和消费者,ReentrantLock的实现* * @author ZGJ* @date 2017年6月22日*/publicclassTest2 { privatestaticIntegercount=0; privatestaticfinalIntegerFULL=10; //创建一个锁对象privateLocklock=newReentrantLock(); //创建两个条件变量,一个为缓冲区非满,一个为缓冲区非空privatefinalConditionnotFull=lock.newCondition(); privatefinalConditionnotEmpty=lock.newCondition(); publicstaticvoidmain(String[] args) { Test2test2=newTest2(); newThread(test2.newProducer()).start(); newThread(test2.newConsumer()).start(); newThread(test2.newProducer()).start(); newThread(test2.newConsumer()).start(); newThread(test2.newProducer()).start(); newThread(test2.newConsumer()).start(); newThread(test2.newProducer()).start(); newThread(test2.newConsumer()).start(); } classProducerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (Exceptione) { e.printStackTrace(); } //获取锁lock.lock(); try { while (count==FULL) { try { notFull.await(); } catch (InterruptedExceptione) { e.printStackTrace(); } } count++; System.out.println(Thread.currentThread().getName() +"生产者生产,目前总共有"+count); //唤醒消费者notEmpty.signal(); } finally { //释放锁lock.unlock(); } } } } classConsumerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (InterruptedExceptione1) { e1.printStackTrace(); } lock.lock(); try { while (count==0) { try { notEmpty.await(); } catch (Exceptione) { e.printStackTrace(); } } count--; System.out.println(Thread.currentThread().getName() +"消费者消费,目前总共有"+count); notFull.signal(); } finally { lock.unlock(); } } } } }
阻塞队列BlockingQueue的实现
BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞。
被阻塞的情况主要有如下两种:
- 当队列满了的时候进行入队列操作
- 当队列空了的时候进行出队列操作因此,当一个线程对已经满了的阻塞队列进行入队操作时会阻塞,除非有另外一个线 程进行了出队操作,当一个线程对一个空的阻塞队列进行出队操作时也会阻塞,除非有另外一个线程进行了入队操作。从上可知,
- 阻塞队列是线程安全的。下面是BlockingQueue接口的一些方法:
操作 | 抛异常 | 特定值 | 阻塞 | 超时 |
插入 | add(o) | offer(o) | put(o) | offer(o, timeout, timeunit) |
移除 | remove(o) | poll(o) | take(o) | poll(timeout, timeunit) |
检查 | element(o) | peek(o) |
这四类方法分别对应的是:
1 . ThrowsException:如果操作不能马上进行,则抛出异常
2 . SpecialValue:如果操作不能马上进行,将会返回一个特殊的值,一般是true或者false
3 . Blocks:如果操作不能马上进行,操作会被阻塞
4 . TimesOut:如果操作不能马上进行,操作会被阻塞指定的时间,如果指定时间没执行,则返回一个特殊值,一般是true或者false
下面来看由阻塞队列实现的生产者消费者模型,这里我们使用take()和put()方法,这里生产者和生产者,消费者和消费者之间不存在同步,所以会出现连续生成和连续消费的现象
importjava.util.concurrent.ArrayBlockingQueue; importjava.util.concurrent.BlockingQueue; /*** 使用BlockingQueue实现生产者消费者模型* @author ZGJ* @date 2017年6月29日*/publicclassTest3 { privatestaticIntegercount=0; //创建一个阻塞队列finalBlockingQueue<Integer>blockingQueue=newArrayBlockingQueue<>(10); publicstaticvoidmain(String[] args) { Test3test3=newTest3(); newThread(test3.newProducer()).start(); newThread(test3.newConsumer()).start(); newThread(test3.newProducer()).start(); newThread(test3.newConsumer()).start(); newThread(test3.newProducer()).start(); newThread(test3.newConsumer()).start(); newThread(test3.newProducer()).start(); newThread(test3.newConsumer()).start(); } classProducerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (Exceptione) { e.printStackTrace(); } try { blockingQueue.put(1); count++; System.out.println(Thread.currentThread().getName() +"生产者生产,目前总共有"+count); } catch (InterruptedExceptione) { e.printStackTrace(); } } } } classConsumerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (InterruptedExceptione1) { e1.printStackTrace(); } try { blockingQueue.take(); count--; System.out.println(Thread.currentThread().getName() +"消费者消费,目前总共有"+count); } catch (InterruptedExceptione) { e.printStackTrace(); } } } } }
信号量Semaphore的实现
Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源,在操作系统中是一个非常重要的问题,可以用来解决哲学家就餐问题。Java中的Semaphore维护了一个许可集,一开始先设定这个许可集的数量,可以使用acquire()方法获得一个许可,当许可不足时会被阻塞,release()添加一个许可。
在下列代码中,还加入了另外一个mutex信号量,维护生产者消费者之间的同步关系,保证生产者和消费者之间的交替进行
importjava.util.concurrent.Semaphore; /*** 使用semaphore信号量实现* @author ZGJ* @date 2017年6月29日*/publicclassTest4 { privatestaticIntegercount=0; //创建三个信号量finalSemaphorenotFull=newSemaphore(10); finalSemaphorenotEmpty=newSemaphore(0); finalSemaphoremutex=newSemaphore(1); publicstaticvoidmain(String[] args) { Test4test4=newTest4(); newThread(test4.newProducer()).start(); newThread(test4.newConsumer()).start(); newThread(test4.newProducer()).start(); newThread(test4.newConsumer()).start(); newThread(test4.newProducer()).start(); newThread(test4.newConsumer()).start(); newThread(test4.newProducer()).start(); newThread(test4.newConsumer()).start(); } classProducerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (InterruptedExceptione) { e.printStackTrace(); } try { notFull.acquire(); mutex.acquire(); count++; System.out.println(Thread.currentThread().getName() +"生产者生产,目前总共有"+count); } catch (InterruptedExceptione) { e.printStackTrace(); } finally { mutex.release(); notEmpty.release(); } } } } classConsumerimplementsRunnable { publicvoidrun() { for (inti=0; i<10; i++) { try { Thread.sleep(3000); } catch (InterruptedExceptione1) { e1.printStackTrace(); } try { notEmpty.acquire(); mutex.acquire(); count--; System.out.println(Thread.currentThread().getName() +"消费者消费,目前总共有"+count); } catch (InterruptedExceptione) { e.printStackTrace(); } finally { mutex.release(); notFull.release(); } } } } }
管道输入输出流PipedInputStream和PipedOutputStream实现
在java的io包下,PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流。
它们的作用是让多线程可以通过管道进行线程间的通讯。在使用管道通信时,必须将PipedOutputStream和PipedInputStream配套使用。
使用方法:先创建一个管道输入流和管道输出流,然后将输入流和输出流进行连接,用生产者线程往管道输出流中写入数据,消费者在管道输入流中读取数据,这样就可以实现了不同线程间的相互通讯,但是这种方式在生产者和生产者、消费者和消费者之间不能保证同步,也就是说在一个生产者和一个消费者的情况下是可以生产者和消费者之间交替运行的,多个生成者和多个消费者者之间则不行
/*** 使用管道实现生产者消费者模型* @author ZGJ* @date 2017年6月30日*/publicclassTest5 { finalPipedInputStreampis=newPipedInputStream(); finalPipedOutputStreampos=newPipedOutputStream(); { try { pis.connect(pos); } catch (IOExceptione) { e.printStackTrace(); } } classProducerimplementsRunnable { publicvoidrun() { try { while(true) { Thread.sleep(1000); intnum= (int) (Math.random() *255); System.out.println(Thread.currentThread().getName() +"生产者生产了一个数字,该数字为: "+num); pos.write(num); pos.flush(); } } catch (Exceptione) { e.printStackTrace(); } finally { try { pos.close(); pis.close(); } catch (IOExceptione) { e.printStackTrace(); } } } } classConsumerimplementsRunnable { publicvoidrun() { try { while(true) { Thread.sleep(1000); intnum=pis.read(); System.out.println("消费者消费了一个数字,该数字为:"+num); } } catch (Exceptione) { e.printStackTrace(); } finally { try { pos.close(); pis.close(); } catch (IOExceptione) { e.printStackTrace(); } } } } publicstaticvoidmain(String[] args) { Test5test5=newTest5(); newThread(test5.newProducer()).start(); newThread(test5.newConsumer()).start(); } }