黑马程序员技术交流社区
标题:
生产者与消费者运用同步和Lock锁
[打印本页]
作者:
追逐
时间:
2014-3-19 00:57
标题:
生产者与消费者运用同步和Lock锁
本帖最后由 追逐 于 2014-3-21 19:35 编辑
1,运用同步
/*
生产者消费者问题
对于生产多个生产者和消费者
为什么要定义while判断标记。
原因:让被唤醒的线程再一次判断标记
为什么要定义notifyAll
以为需要唤醒对方线程
因为只用notify,容易出现只唤醒本方线程的情况,导致程序中的所有程序都等待
*/
//资源类
class Resource {
private String name;
private int a = 0;
private boolean b = false; //定义标签
public synchronized void set(String name) { //往里面输入
while(b)
try{
this.wait();
} catch(InterruptedException e) {}
this.name = name + "..." + a++;
System.out.println(Thread.currentThread().getName() + "生产者..." + this.name);
this.b = true;
this.notifyAll();
}
public synchronized void out() {
while(!b)
try {
this.wait();
} catch(InterruptedException e) {}
System.out.println(Thread.currentThread().getName() + "............消费者..." + this.name);
this.b = false;
this.notifyAll();
}
}
//生产者
class Produser implements Runnable {
private Resource r;
Produser(Resource r) {
this.r = r;
}
public void run() {
while(true) {
r.set("理学院");
}
}
}
//消费者
class Consumer implements Runnable {
private Resource r;
Consumer(Resource r) {
this.r = r;
}
public void run() {
while(true) {
r.out();
}
}
}
//运行类
class ProduserConsumerDemo {
public static void main(String[] args) {
Resource r = new Resource();
Produser p = new Produser(r);
Consumer c = new Consumer(r);
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
Thread t3 = new Thread(c);
Thread t4 = new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
1,升级版的JDK新做法
import java.util.concurrent.locks.*;
/*
JDK1.5中提供了多线程升级解决方案。
将同步synchronized替换成显示的Lock操作。
将Object中的wait,notify,notifyAll,替换成了Condition对象
该对象可以Lock锁,进行获取。
该示例中,实现了本方只唤醒对方的操作。
消费者和生产者问题
*/
//定义资源类
class Resource1 {
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition(); //创建生产者锁对象
private Condition condition_con = lock.newCondition(); //创建消费者锁对象
private boolean b = false; //创建一个标记
private String name;
private int a = 1;
public void set(String name) throws InterruptedException {
lock.lock(); //创建锁
try {
while(b)
condition_pro.await(); //生产者沉睡
this.name = name + "......" + a++;
System.out.println(Thread.currentThread().getName() + "生产者..." + name);
this.b = true;
condition_con.signal(); //唤醒消费者
} finally {
lock.unlock(); //释放锁
}
}
public void out() throws InterruptedException {
lock.lock(); //创建锁
try {
while(!b)
condition_con.await(); //消费者沉睡
System.out.println(Thread.currentThread().getName() + ".......消费者........" + name);
this.b = false;
condition_pro.signal(); //唤醒生产者
} finally {
lock.unlock(); //释放锁
}
}
}
//定义生产者
class Produser1 implements Runnable {
private Resource1 r;
Produser1(Resource1 r) {
this.r = r;
}
public void run() {
while(true) {
try {
r.set("张三");
} catch(InterruptedException e) {}
}
}
}
//定义消费者
class Consumer1 implements Runnable {
private Resource1 r;
Consumer1(Resource1 r) {
this.r = r;
}
public void run() {
while(true) {
try {
r.out();
} catch(InterruptedException e) {}
}
}
}
class ProduserConsumerDemo1 {
public static void main(String[] args) {
Resource1 r = new Resource1();
Produser1 pro = new Produser1(r);
Consumer1 con = new Consumer1(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();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2