码迷,mamicode.com
首页 > 编程语言 > 详细

java 生产者和消费者

时间:2017-07-09 19:37:34      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:代码   同步   ant   rac   取出   锁对象   安全   ==   product   

生产者和消费者的例子

一、wait() / notify()方法

wait() / nofity()方法是基类Object的两个方法,也就意味着所有Java类都会拥有这两个方法,这样,我们就可以为任何对象实现同步机制。

wait()方法:当缓冲区已满/空时,生产者/消费者线程停止自己的执行,放弃锁,使自己处于等等状态,让其他线程执行。

notify()方法:当生产者/消费者向缓冲区放入/取出一个产品时,向其他等待的线程发出可执行的通知,同时放弃锁,使自己处于等待状态。

直接贴上代码 用as

class Person{
private String name;
private String sex;

private Boolean isEmpty = Boolean.TRUE;

public void set(String name,String sex){
synchronized(this){
while(!isEmpty.equals(Boolean.TRUE)){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name=name;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.sex=sex;
isEmpty=Boolean.FALSE;
this.notifyAll();
}
}

public void get(){
synchronized(this){
while(!isEmpty.equals(Boolean.FALSE)){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String name=getName();
String sex=getSex();
System.out.println(name + " --> " + sex);
isEmpty=Boolean.TRUE;
this.notifyAll();
}
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

}

class Producer implements Runnable{
private Person p;
public Producer(Person p){
this.p=p;

}

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=2;i<100;i++){
if(i%2==0){
p.set("李果", "女");
}else{
p.set("李凡", "男");
}
}
}

}
class Consumer implements Runnable{
private Person p;
public Consumer(Person p){
this.p=p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=2;i<100;i++){
p.get();
}
}

}
public class Productor_ConsumerDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();

new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}

}

用Synchronized 同步方法

await() / signal()方法

在JDK5.0之后,Java提供了更加健壮的线程处理机制,包括同步、锁定、线程池等,它们可以实现更细粒度的线程控制。await()和signal()就是其中用来做同步的两种方法,它们的功能基本上和wait() / nofity()相同,完全可以取代它们,但是它们和新引入的锁定机制Lock直接挂钩,具有更大的灵活性。通过在Lock对象上调用newCondition()方法,将条件变量和一个锁对象进行绑定,进而控制并发程序访问竞争资源的安全。下面来看代码:

class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

private String sex;

private final ReentrantLock lock = new ReentrantLock();
private final Condition con = lock.newCondition();
private Boolean isEmpty = Boolean.TRUE;

public void set(String name, String sex) {
lock.lock();
while (!isEmpty.equals(Boolean.TRUE)) {
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
this.name = name;
Thread.sleep(1);
this.sex = sex;
isEmpty = Boolean.FALSE;
con.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}

}

public void get() {
lock.lock();
while (!isEmpty.equals(Boolean.FALSE)) {
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
String name = getName();

String sex = getSex();
System.out.println(name + " --> " + sex);
isEmpty = Boolean.TRUE;
con.signal();

} finally {
lock.unlock();
}
}
}

class Productor implements Runnable {
private Person p;

public Productor(Person p) {
this.p = p;
}

@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2; i < 100; i++) {
if (i % 2 == 0) {
p.set("李果", "女");
} else {
p.set("李雨軒", "男");
}
}
}

}

class Cosumer implements Runnable {
private Person p;

public Cosumer(Person p) {
this.p = p;
}

@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2; i < 100; i++) {
p.get();
}

}

}

public class Producer_Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();

new Thread(new Productor(p)).start();
new Thread(new Cosumer(p)).start();
}

}

java 生产者和消费者

标签:代码   同步   ant   rac   取出   锁对象   安全   ==   product   

原文地址:http://www.cnblogs.com/lixiaowei395659729/p/7142389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!