多线程生产消费问题
2018-01-21 22:41
309 查看
运用Lock和Condition解决多线程容易出现的问题(基础方法)
Lock:lock(); unlock(); newCondition();
Condition: await(); single(); singleAll();
import java.util.concurrent.locks.*;
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread
4000
t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count = 1;
private Boolean flag = false;
public Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();//上锁
try
{
while(flag)
condition_pro.await();//pro进入等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
condition_con.signal();//唤醒con一个等待线程
}
finally
{
lock.unlock();//锁必须释放
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();//con进入等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
flag = false;
condition_pro.signal();//唤醒一个pro线程
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch (InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}
}
Lock:lock(); unlock(); newCondition();
Condition: await(); single(); singleAll();
import java.util.concurrent.locks.*;
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread
4000
t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count = 1;
private Boolean flag = false;
public Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();//上锁
try
{
while(flag)
condition_pro.await();//pro进入等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
condition_con.signal();//唤醒con一个等待线程
}
finally
{
lock.unlock();//锁必须释放
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();//con进入等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
flag = false;
condition_pro.signal();//唤醒一个pro线程
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch (InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}
}
相关文章推荐
- 多线程之生产消费问题
- 多线程中多生产多消费问题
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- Java 多线程间通信 多生产 多消费问题
- --多线程中,单生产单消费,多生产多消费的问题
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 多线程中的多生产多消费问题
- 多线程的经典案例(生产消费问题)
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 【代码练习6】利用多线程生产消费问题实现熊吃蜂蜜问题
- 多线程生产消费问题
- java多线程之生产消费问题
- 多线程之偷了个懒之经典生产消费问题
- Java多线程的单生产单消费和多生产多消费问题的解决
- Java中多线程的多生产多消费问题的解决方案
- 多线程多生产多消费问题以及解决
- Java模拟生产消费问题
- java多线程(生产消费)
- 生产者与消费者模型(基于单链表、环形队列、多线程、多消费多生产)