您的位置:首页 > 职场人生

黑马程序员-多线程经典之消费者与生产者

2013-03-27 09:44 288 查看
------- android培训java培训、期待与您交流! ----------

此处是详细的写了生产者与消费者的例子,以及1.5新特性Lock的操作

package com.itheima;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

* 需求:做一个生产者消费者的例子并用两种不同的同步方式

* 同步代码块synchronized 与jdk1.5新特性Lock

*

* 步骤:

* 1.创建产品类 Resource 包括两个属性name 与编号 count;

* 2.Resource类中有两个方法get()与set()分别表示代表生产和消费

* 3.创建生产者类Producer 和 消费者类Consumer 这两个类继承Runnable

* 4.MainClass 创建多个线程对象 测试

*

*/

//创建产品类

class Resource1{

private String name;

private int count=1;

private boolean flag=false;//这里为什么初始化为false,定义为false 如果消费者线程先执行

//则这消费者会等待,这样就释放了锁 。如果生产者先得到了锁,则先生产,然后改变标记

public synchronized void set(String name){

//Syystem.out.println("=============");

if(flag){

try {

this.wait();

Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

this.name=name+"_"+(this.count++);

System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);

flag=true;

this.notify();

}

public synchronized void get(){

//System.out.println("--------------");

if(!flag){

try {

this.wait();

Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);

flag=false;

this.notify();

}

}

class Producer1 implements Runnable{

private Resource1 res;

public Producer1(Resource1 res){

this.res=res;

}

public void run(){

while(true){

res.set("黄瓜");

}

}

}

class Consumer1 implements Runnable{

private Resource1 res;

public Consumer1(Resource1 res){

this.res=res;

}

public void run(){

while(true){

res.get();

}

}

}

class ThreadConsumer1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Resource1 res = new Resource1();

Producer1 prop = new Producer1(res);

Consumer1 con = new Consumer1(res);

Thread t1 = new Thread(prop);

Thread t2 = new Thread(con);

t1.start();

t2.start();

}

}

//以上代码存在着问题,如果多个线程一起并发的时候他不知道那个先运行,所以要将if(flag)改成while(flag)notify改成notifyAll();

//下面使用Lock 再写一遍,这个只是代码

class Resource2{

private String name;

private int count=1;

private boolean flag = false;

//创建明锁

Lock lock = new ReentrantLock();

//通过已有的锁,创建锁的监听器 通过lock对象创建

Condition consumer_con = lock.newCondition();

Condition producer_con = lock.newCondition();

public void set(String name){

lock.lock();//获取锁

try{

while(flag){

try {

producer_con.await();

Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

this.name=name+count;

count++;

System.out.println(Thread.currentThread().getName()+"生产者---"+this.name);

flag = true;

consumer_con.signal();//这里只是唤醒consumer_con 的线程

}

finally{

lock.unlock();

}

}

public void get(){

lock.lock();

try{

while(!flag){

try {

consumer_con.await();

Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

System.out.println(Thread.currentThread().getName()+"-------消费者"+this.name);

flag = false;

producer_con.signal();

}

finally{

lock.unlock();

}

}

}

class Producer2 implements Runnable{

private Resource2 res;

public Producer2(Resource2 res){

this.res = res;

}

public void run(){

while(true){

res.set("香蕉");

}

}

}

class Consumer2 implements Runnable{

private Resource2 res;

public Consumer2(Resource2 res){

this.res = res;

}

public void run(){

while(true)

res.get();

}

}

class ThreadConsumer2{

public static void main(String[] args){

Resource2 res = new Resource2();

Producer2 pro = new Producer2(res);

Consumer2 con = new Consumer2(res);

Thread t1=new Thread(pro);

Thread t2=new Thread(con);

Thread t3=new Thread(pro);

Thread t4=new Thread(con);

t1.start();

t2.start();

t3.start();

t4.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: