import java.util.Locale;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*jdk,1.4版本及其以前的处理方法。
* 对于多个生产者和消费者。
* 为什么定义while判断标记。
* 原因:让被唤醒的线程再一次判断标记
*
* 为什么定义notifyAll
* 因为需要唤醒对方线程
* 因为只用notify,容易出现只唤醒本方线程的情况。
* 导致程序中所有的线程都处于等待状态。
*
class Resource
{
//生产者与消费者姓名
private String name;
//生产者与消费者编号
private int count=1;
//判断值
private boolean flag=false;
//设置数据的方法,用synchronized上锁
public synchronized void set(String name)
{
//判断锁
while(flag)
try{
//判断值为真,生产者等待
this.wait();
}
catch (Exception e) {
}
//赋值
this.name=name+"--"+count++;
//打印
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
//改变判断值
flag=true;
//唤醒所有进入等待的线程
this.notifyAll();
}
//输出数据
public synchronized void out()
{
//判断锁
while(!flag)
try{
this.wait();
}
catch(Exception e){}
//打印
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
//改变判断值
flag=false;
//唤醒所有等待线程
this.notifyAll();
}
}
class Producer implements Runnable
{
//定义资源对象
private Resource res;
//构造函数,以资源对象作为参数
Producer(Resource res) {
this.res=res;
}
//复写run方法
public void run()
{
while (true)
{
//设置数据
res.set("+商品+");
}
}
}
//定义消费者实现Runnable 接口
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res) {
this.res=res;
}
public void run()
{
while (true)
{
//输出数据
res.out();
}
}
}
public class PcDemo {
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 t3=new Thread(con);
Thread t4=new Thread(con);
//开启线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
*/
/*
* jdk.1.5版本以后的的处理方式(升级解决方案):
* 将同步Synchronized替换成显示的Lock操作。
* 将Object 中的wiat , notify 等方法,替换成condition对象。
* 该对象可以通过lock锁获取。
* 该实例中,实现了本方只唤醒对方的一个线程的操作。
*/
class Resource
{
private String name;
private int count=1;
private boolean flag=false;
//定义一个锁对象
private Lock lock=new ReentrantLock();
//用lock产生一个condition对象
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();
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
flag=true;
//唤醒消费者中的一个线程
condition_con.signal();
}
//释放锁
finally
{
lock.unlock();
}
}
public synchronized void out() throws InterruptedException
{
try{
//定义锁
lock.lock();
while(!flag)
//消费者进入等待
condition_con.await();
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
flag=false;
//唤醒一个生产者
condition_pro.signal();
}
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) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res) {
this.res=res;
}
public void run()
{
while (true)
{
try {
res.out();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
public class PcDemo {
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 t3=new Thread(con);
Thread t4=new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*jdk,1.4版本及其以前的处理方法。
* 对于多个生产者和消费者。
* 为什么定义while判断标记。
* 原因:让被唤醒的线程再一次判断标记
*
* 为什么定义notifyAll
* 因为需要唤醒对方线程
* 因为只用notify,容易出现只唤醒本方线程的情况。
* 导致程序中所有的线程都处于等待状态。
*
class Resource
{
//生产者与消费者姓名
private String name;
//生产者与消费者编号
private int count=1;
//判断值
private boolean flag=false;
//设置数据的方法,用synchronized上锁
public synchronized void set(String name)
{
//判断锁
while(flag)
try{
//判断值为真,生产者等待
this.wait();
}
catch (Exception e) {
}
//赋值
this.name=name+"--"+count++;
//打印
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
//改变判断值
flag=true;
//唤醒所有进入等待的线程
this.notifyAll();
}
//输出数据
public synchronized void out()
{
//判断锁
while(!flag)
try{
this.wait();
}
catch(Exception e){}
//打印
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
//改变判断值
flag=false;
//唤醒所有等待线程
this.notifyAll();
}
}
class Producer implements Runnable
{
//定义资源对象
private Resource res;
//构造函数,以资源对象作为参数
Producer(Resource res) {
this.res=res;
}
//复写run方法
public void run()
{
while (true)
{
//设置数据
res.set("+商品+");
}
}
}
//定义消费者实现Runnable 接口
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res) {
this.res=res;
}
public void run()
{
while (true)
{
//输出数据
res.out();
}
}
}
public class PcDemo {
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 t3=new Thread(con);
Thread t4=new Thread(con);
//开启线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
*/
/*
* jdk.1.5版本以后的的处理方式(升级解决方案):
* 将同步Synchronized替换成显示的Lock操作。
* 将Object 中的wiat , notify 等方法,替换成condition对象。
* 该对象可以通过lock锁获取。
* 该实例中,实现了本方只唤醒对方的一个线程的操作。
*/
class Resource
{
private String name;
private int count=1;
private boolean flag=false;
//定义一个锁对象
private Lock lock=new ReentrantLock();
//用lock产生一个condition对象
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();
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
flag=true;
//唤醒消费者中的一个线程
condition_con.signal();
}
//释放锁
finally
{
lock.unlock();
}
}
public synchronized void out() throws InterruptedException
{
try{
//定义锁
lock.lock();
while(!flag)
//消费者进入等待
condition_con.await();
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
flag=false;
//唤醒一个生产者
condition_pro.signal();
}
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) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res) {
this.res=res;
}
public void run()
{
while (true)
{
try {
res.out();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
public class PcDemo {
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 t3=new Thread(con);
Thread t4=new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}