黑马程序员-多线程经典之消费者与生产者
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();
}
}
此处是详细的写了生产者与消费者的例子,以及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();
}
}
相关文章推荐
- 黑马程序员——多线程操作经典实例:生产者消费者问题
- java多线程经典的生产者消费者问题
- java 多线程之 wait(),notify(),notifyAll()以及经典的生产者消费者模型
- java多线程之生产者消费者经典问题
- JAVA多线程之——经典面试消费者与生产者
- C# 多线程编程 经典模型 生产者和消费者问题
- 多线程经典例子--生产者、消费者(1)
- C# Thread 多线程 Monitor 锁 Producer And Consumer 生产者和消费者 经典模型
- 多线程经典案例——生产者/消费者问题的Java实现与详解
- java多线程之生产者消费者经典问题
- java多线程之生产者消费者经典问题
- 经典多线程Java实例 生产者与消费者
- 黑马程序员_日记18_Java多线程(八)--生产者消费者问题JDK1.5特性
- 黑马程序员-----多线程之间的通信(生产者消费者)
- 黑马程序员自学笔记————多线程 线程间通信之生产者消费者问题;
- java多线程实现生产者与消费者---经典问题
- java多线程之生产者和消费者经典问题案例
- java多线程总结六:经典生产者消费者问题实现
- 黑马程序员------多线程(No.2)(死锁、线程间通信、生产者消费者问题)
- java多线程之生产者消费者经典问题