多生产者--多消费者 显示锁实现同步 java实现
2015-09-16 10:12
453 查看
看了毕老师关于多生产者多消费者的视频,自己就试着写一个多生产者多消费者的程序,跟大家分享,希望对需要的人有所帮助。
package interview;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author jing
* 在本程序中假设生产者生产每次只生产一只烤鸭,等消费者消费完,再接着生产第二只.....
*
*/
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
// 创建一个锁对象。
Lock lock = new ReentrantLock();
// 通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。
Condition producer_con = lock.newCondition();
Condition consumer_con = lock.newCondition();
public void set(String name)
{
lock.lock(); //获取锁对象,当有一个生产者在生产,其他生产者就无法进入
try
{
/*使用while是为了使线程每次被唤醒之后都要判断标记flag*/
while(flag)
{
try {
producer_con.await(); //将生产者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true; //生产者生产完后,将标记置为true,这样其他生产者线程进来之后,判断标记就进入等待
consumer_con.signal(); //唤醒一个消费者线程
}
finally
{
lock.unlock(); //释放锁,让其他生产者进入,这个必须执行,所以放在finally代码块中
}
}
public void out()
{
lock.lock(); //获取锁对象,当有一个消费者在消费时,其他消费者就无法进入
try
{
while(!flag)
{
try {
consumer_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
flag = false;
producer_con.signal();
//唤醒一个生产者
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource r;
public Producer(Resource r) {
this.r = r;
}
@Override
public void run() {
while(true)
{
r.set("烤鸭");
}
}
}
class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r) {
this.r = r;
}
@Override
public void run() {
while(true)
{
r.out();
}
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource1 r = new Resource1();
Producer1 pro = new Producer1(r);
Consumer1 con = new Consumer1(r);
Thread t0 = new Thread(pro); //创建五个线程
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t0.start(); //开启五个线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package interview;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author jing
* 在本程序中假设生产者生产每次只生产一只烤鸭,等消费者消费完,再接着生产第二只.....
*
*/
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
// 创建一个锁对象。
Lock lock = new ReentrantLock();
// 通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。
Condition producer_con = lock.newCondition();
Condition consumer_con = lock.newCondition();
public void set(String name)
{
lock.lock(); //获取锁对象,当有一个生产者在生产,其他生产者就无法进入
try
{
/*使用while是为了使线程每次被唤醒之后都要判断标记flag*/
while(flag)
{
try {
producer_con.await(); //将生产者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true; //生产者生产完后,将标记置为true,这样其他生产者线程进来之后,判断标记就进入等待
consumer_con.signal(); //唤醒一个消费者线程
}
finally
{
lock.unlock(); //释放锁,让其他生产者进入,这个必须执行,所以放在finally代码块中
}
}
public void out()
{
lock.lock(); //获取锁对象,当有一个消费者在消费时,其他消费者就无法进入
try
{
while(!flag)
{
try {
consumer_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
flag = false;
producer_con.signal();
//唤醒一个生产者
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource r;
public Producer(Resource r) {
this.r = r;
}
@Override
public void run() {
while(true)
{
r.set("烤鸭");
}
}
}
class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r) {
this.r = r;
}
@Override
public void run() {
while(true)
{
r.out();
}
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource1 r = new Resource1();
Producer1 pro = new Producer1(r);
Consumer1 con = new Consumer1(r);
Thread t0 = new Thread(pro); //创建五个线程
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t0.start(); //开启五个线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
相关文章推荐
- java过滤utf8mb4表情符号
- Java随机生成18位身份证号
- 使用fat jar打包java项目
- Spring MVC ContentNegotiatingViewResolver 样例
- test2.2
- 在eclipse导入Java 的jar包的方法 JDBC【图文说明】
- Eclipse Java代码补全
- java接口
- test2.1
- Java TreeMap 源码解析
- 简单介绍 java enumeration
- Java String的hashCode实现
- java得到各种路径的方法
- MonkeyRunner于Windows在下面Eclipse开发环境的搭建步骤(并解决在线Jython配置错误的问题)
- java-encodeURI decodeURI 解决地址传参乱码问题
- Java泛型的类型擦除
- Hadoop 笔记 Eclipse导入Hadoop Jar包
- SpringMVC+JPA+SpringData配置
- java File的简单用法
- Struts2标签取值方式