java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
2016-05-06 14:30
736 查看
Lock 实现提供了比使用 synchronized 方法和语句可获得的更广泛的锁定操作。
此实现允许更灵活的结构,可以具有差别很大的属性,可以支持多个相关的 Condition 对象。
使用生产者和消费者为例!!!import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 使用Lock和Condition代替
* synchronized 和 wait, notify notifyAll();
*/
class Resource2{
private String product;
private int count = 1;
private boolean flag= false;
private Lock lock = new ReentrantLock();
Condition condition_pro = lock.newCondition(); //鎖創建的生產者condition對象!!
Condition condition_con = lock.newCondition(); //鎖創建的消費者condition對象!!
public void produce(String product) throws InterruptedException{
lock.lock();
try {
while(flag){
condition_pro.await();
}
this.product = product+"----"+ count++;
System.out.println(Thread.currentThread().getName()+"————生產了————"+this.product);
flag= true;
condition_con.signal(); //喚醒消費者對象!!!
}finally{
lock.unlock();
}
}
public void consume() throws InterruptedException{
lock.lock();
try {
while(!flag){
condition_con.await();
}
System.out.println(Thread.currentThread().getName()+"————消費了————"+this.product);
flag= false;
condition_pro.signal(); //喚起生產者對象!!!
}finally{
lock.unlock();
}
}
}
class Producer2 implements Runnable{
private Resource2 res;
public Producer2(Resource2 res){
this.res= res;
}
@Override
public void run() {
while(true){
try {
res.produce("Iphone 7");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer2 implements Runnable{
private Resource2 res;
public Consumer2(Resource2 res){
this.res= res;
}
@Override
public void run() {
while(true){
try {
res.consume();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ProducerComsumer2 {
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,"生產者1");
Thread t2 = new Thread(pro,"生產者2");
Thread t3 = new Thread(con,"消費者1");
Thread t4 = new Thread(con,"消費者2");
t1.start();
t2.start();
t3.start();
t4.start();
//new Thread(new Producer(res)).start();
//new Thread(new Consumer(res)).start();
}
}
此实现允许更灵活的结构,可以具有差别很大的属性,可以支持多个相关的 Condition 对象。
使用生产者和消费者为例!!!import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 使用Lock和Condition代替
* synchronized 和 wait, notify notifyAll();
*/
class Resource2{
private String product;
private int count = 1;
private boolean flag= false;
private Lock lock = new ReentrantLock();
Condition condition_pro = lock.newCondition(); //鎖創建的生產者condition對象!!
Condition condition_con = lock.newCondition(); //鎖創建的消費者condition對象!!
public void produce(String product) throws InterruptedException{
lock.lock();
try {
while(flag){
condition_pro.await();
}
this.product = product+"----"+ count++;
System.out.println(Thread.currentThread().getName()+"————生產了————"+this.product);
flag= true;
condition_con.signal(); //喚醒消費者對象!!!
}finally{
lock.unlock();
}
}
public void consume() throws InterruptedException{
lock.lock();
try {
while(!flag){
condition_con.await();
}
System.out.println(Thread.currentThread().getName()+"————消費了————"+this.product);
flag= false;
condition_pro.signal(); //喚起生產者對象!!!
}finally{
lock.unlock();
}
}
}
class Producer2 implements Runnable{
private Resource2 res;
public Producer2(Resource2 res){
this.res= res;
}
@Override
public void run() {
while(true){
try {
res.produce("Iphone 7");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer2 implements Runnable{
private Resource2 res;
public Consumer2(Resource2 res){
this.res= res;
}
@Override
public void run() {
while(true){
try {
res.consume();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ProducerComsumer2 {
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,"生產者1");
Thread t2 = new Thread(pro,"生產者2");
Thread t3 = new Thread(con,"消費者1");
Thread t4 = new Thread(con,"消費者2");
t1.start();
t2.start();
t3.start();
t4.start();
//new Thread(new Producer(res)).start();
//new Thread(new Consumer(res)).start();
}
}
相关文章推荐
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- Spring MVC @ResponseBody注解返回响应流时中文乱码问题的解决
- Eclipse的代码追踪功能
- MyEclipse8.5可用注册码(到2018年)
- eclipse 常用的10大快捷键
- Eclipse Android NDK环境搭建
- javaIO流 FileReader 的readLIne 和 read的区别
- eclipse 打不开,一闪而过
- Java类MemoryUsage查看虚拟机的使用情况
- trie树(字典树)java实现
- java线程——多个线程之间切换执行
- 使用CMD编译JAVA文件时,不要再系统目录下面.
- java foreach实现原理
- java实现文件的分割与合并
- Java高效读取大文件
- 当接口被调用时使用Spring拦截器注入运行时数据
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
- Java中ResourceBundle类的使用
- spring task小结
- 集成p6spy到spring中