zoukankan      html  css  js  c++  java
  • java 生产者和消费者

    生产者和消费者的例子

    一、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();
    }

    }

  • 相关阅读:
    tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案
    Myeclispe 安装 SVN :
    Oracle数据库中SYS、SYSTEM、DBSNMP、SYSMAN四用户的区别
    转: Maven 仓库中添加Oracle JDBC驱动(11g)
    Ubuntu14.04的常用快捷键
    Ubuntu下常用的命令
    Ubuntu14.04 java环境配置
    主谓宾定状补
    Git的常用命令
    转:Android面试
  • 原文地址:https://www.cnblogs.com/lixiaowei395659729/p/7142389.html
Copyright © 2011-2022 走看看