消费者和生产者问题:
生产者:生产数据。将数据传递到共享资源对象。
共享资源对象:获取到生产者生产的数据,将数据提供给消费者。
消费者:拿到共享资源对象中的数据。
synchronized 方式:
package theadDemo;
/**
* 线程的通信(生产者和消费者)共享资源,使用实现接口的方式,生产和消费同时进行;使用一个或者多个线程表示生产者P,使用一个或者多个线程表示消费者C
* @author Administrator
*
*/
public class demo1 {
public static void main(String[] args) {
ShareResoure s = new ShareResoure();
new Thread(new P(s)).start();
new Thread(new C(s)).start();
}
}
//共享资源对象--中间数据交换
class ShareResoure{
private String name;
private String sex;
private Boolean isEmpty = true;//表示共享资源是否为空
//用于向生产者资源对象存数据
synchronized public void push(String name,String sex) {
try {
while(!isEmpty) {
this.wait();//同步锁对象调用,当前线程进入等待池中,只能被其他线程唤醒
}
this.name=name;
Thread.sleep(10);//线程休眠
this.sex=sex;
isEmpty = false;
this.notifyAll();//唤醒生产者
}catch (Exception e) {
e.printStackTrace();
}
}
//消费者从共享资源对象中取出数据
synchronized public void get() {
try {
while(isEmpty) {
//true 等待生产者生产数据
this.wait();
}
Thread.sleep(10);
System.out.println(this.name+"----"+this.sex);
isEmpty=true;
this.notifyAll();//唤醒消费者
}catch (Exception e) {
e.printStackTrace();
}
}
}
//生产者
class P implements Runnable{
private ShareResoure shareResoure = null;
public P(ShareResoure shareResoure) {
super();
this.shareResoure = shareResoure;
}
@Override
public void run() {
for(int i=0;i<50;i++) {
if(i%2==0) {
shareResoure.push((i+1)+"A", "女");
}else {
shareResoure.push((i+1)+"B", "男");
}
}
}
}
//消费者
class C implements Runnable{
private ShareResoure shareResoure = null;
public C(ShareResoure shareResoure) {
super();
this.shareResoure = shareResoure;
}
@Override
public void run() {
for(int i=0;i<50;i++) {
shareResoure.get();
}
}
}
lock方式:
package theadDemo;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* lock 中不能使用wait notifyAll..使用 newCondition() .从java5开始可以使用lock取代synchronized
* Condition()中signalAll()唤醒所有等待线程;await() 导致当前线程等到发信号或interrupted中断这个线程 。
* @author Administrator
*
*/
public class lookdemo1 {
public static void main(String[] args) {
ShareResoure2 s = new ShareResoure2();
new Thread(new P2(s)).start();
new Thread(new C2(s)).start();
}
}
//共享资源对象--中间数据交换
class ShareResoure2{
private String name;
private String sex;
private Boolean isEmpty = true;//表示共享资源是否为空
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();//获取Condition。为之后调用线程的等待和唤醒
//用于向生产者资源对象存数据
public void push(String name,String sex) {
lock.lock();//获取锁
try {
while(!isEmpty) {
//生产者---共享资源中存在数据,等待消费者取出数据,
condition.await();//让线程进入等待状态
}
this.name=name;
Thread.sleep(10);//线程休眠
this.sex=sex;
isEmpty = false;
condition.signalAll();//唤醒所有线程
}catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
//消费者从共享资源对象中取出数据
public void get() {
lock.lock();
try {
while(isEmpty) {
//消费者---共享资源中未存在数据,等待生产者将数据传递到共享资源中。
condition.await();//让线程进入等待状态
}
Thread.sleep(10);
System.out.println(this.name+"----"+this.sex);
isEmpty = true;
condition.signalAll();//唤醒所有线程
}catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();//释放lock资源
}
}
}
//生产者
class P2 implements Runnable{
private ShareResoure2 shareResoure = null;
public P2(ShareResoure2 shareResoure) {
this.shareResoure = shareResoure;
}
@Override
public void run() {
for(int i=0;i<50;i++) {
if(i%2==0) {
shareResoure.push((i+1)+"A", "女");
}else {
shareResoure.push((i+1)+"B", "男");
}
}
}
}
//消费者
class C2 implements Runnable{
private ShareResoure2 shareResoure = null;
public C2(ShareResoure2 shareResoure) {
this.shareResoure = shareResoure;
}
@Override
public void run() {
for(int i=0;i<50;i++) {
shareResoure.get();
}
}
}
运行结果: