您的位置:首页 > 编程语言 > Java开发

线程通信之生产者与消费者--小码哥java

2017-10-02 22:32 274 查看
定义三个类,分别是Producer(生产者),Consumer(消费者),ShareReasource(公共区域)。
定义公共区域类的目的:
解除耦合关系,如果没有公共区域,那么生产者类中将要引用消费者,消费者类中也需要引用生产者,这样会使生产者和消费者彼此都耦合太深,如果修改了消费者,需要修改生产者,如果修改了生产者,也需要修改消费者。
生产者类如下:


public class Producer implements Runnable{
private ShareReasource share=null;
public Producer(ShareResource share)
{
this.share=share;
}
public void run(){
for(int i=0;i<500;i++)
{
if(i%2==0)
share.push("小红","女");
else
share.push("小明","男");
}
}
}


消费者类如下:


public class Consumer implements Runnable{
private ShareResource share=null;
public Consumer (ShareResource share)
{
this.share=share;
}
public void run()
{
for(int i=0;i<500;i++)
{
share.popup();
}
}
}


公共区域类如下:


public class ShareResource{
private String name;
private String gender;
public void push(String name,String gender)
{
this.name=name;
this.gender=gender;
}
public void popup()
{
System.out.println(this.name+"---"+this.gender);
}
}


测试类如下:


public class Test{
public static void main(String []args)
{
ShareResource share=new ShareResource();
new Thread(new Producer(share)).start();
new Thread(new Consumer(share)).start();
}
}


上述基本能实现生产者与消费者的关系,但是也是可能造成线程不安全问题。

解决方法:

1.使用synchronized代码块

2.使用synchronized方法

3.使用Lock机制

在Object类中,操作线程的方法:

都得使用当前同步监听对象来调用,否则报错IllegalMonitorStateException

以下为Object类中与线程相关的几个方法:

void wait(long timeout):让当前线程失去同步监听对象,进入等待,若等待时间一到,或者其他线程来唤醒它,才会再次拥有同步监听对象。

void wait():让当前线程失去同步监听对象,进入无限制的等待,除非其他线程唤醒它,不然永远醒不过来

void notify():唤醒在此同步监听对象上等待的一个线程

void notifyAll():唤醒在此同步监听对象上等待的所有进程

方式1:使用synchronized方法

public class ShareResource{
private String name;
private String gender;
private boolean isempty=true;
syschronized public void push(String name,String gender)
{
while(!isEmpty)
{
try{
this.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.name=name;
this.gender=gender;
isEmpty=false;
this.notifyAll();
}
public void popup()
{
while(isEmpty){
try{
this.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.name+"---"+this.gender);
isEmpty=true;
this.notifyAll();
}
}


方式2:使用Lock机制

public class ShareResource{
private String name;
private String gender;
private boolean isEmpty = true;
private Lock lock=new ReentrantLock();
private Condition condition=lock.newCondition();
public void push(String name,String gender)
{
lock.lock();
try{
while(!isEmpty)
{
condition.await();
}
this.name=name;
this.gender=gender;
isEmpty=false;
condition.signal();//等价于同步监听对象.notiy();
}catch(Exception e)
{
e.printStackTrace();
}finally{
lock.unlock();
}
}

9454
public void popup()
{
lock.lock();
try{
while(isEmpty)
{
condition.await();
}
System.out.println(this.name+"---"+this.gender);
isEmpty=true;
condition.signal();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐