多线程同步问题-生产者和消费者

package Test;


import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

public class TEst {
    public static void main(String[] args) {
        new Thread(new Producer(Resource.getInstance()) , "正常人").start();
        new Thread(new Producer(Resource.getInstance()) , "机器人").start();
        new Thread(new Customer(Resource.getInstance())).start();
    }
}

class Resource {
    private static final int CAPACITY = 1;
    private List<String> nameList = new ArrayList<>(CAPACITY);
    private List<Integer> genderList = new ArrayList<>(CAPACITY);       //0:woman   1:man
    private final ReentrantLock lock = new ReentrantLock();

    private Resource (){}
    static private final Resource resource = new Resource();

    static Resource getInstance() {
        return resource;
    }

    public void put(String name , Integer gender)  {
        synchronized (resource){
            try {
                if ( nameList.size() == CAPACITY ) {
                    resource.wait();
                } if (nameList.size() < CAPACITY ) {
                    resource.notify();
                }
                Thread.sleep(5);
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            nameList.add(Thread.currentThread().getName()+"  "+name);
            genderList.add(gender);
        }
    }

    public void get() {
        synchronized (resource) {
            try {
                if ( nameList.isEmpty() ) {
                    resource.wait();
                }if (nameList.size() > 0) {
                    resource.notify();
                }
                Thread.sleep(5);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            System.out.println("姓名: " + nameList.get(nameList.size() - 1) + "  |  性别: " + genderList.get(genderList.size() - 1));
            nameList.remove(nameList.size() - 1);
            genderList.remove(genderList.size() - 1);

        }
    }
}


 

class Producer implements Runnable {
    private Resource resource;
    private String[] names = {"小红", "小明"};

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            resource.put(names[i%2],i%2);

        }
    }
}

class Customer implements Runnable {
    private Resource resource;

    public Customer(Resource resource) {
        this.resource = resource;
    }
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            resource.get();
        }
    }
}


  • 将put()/get()中的存储/打印取走姓名和性别的操作同步,保证原子化
  • 通过调用Object的wait(),让同步锁监听对象resource释放同步锁,将当前线程A放入等待池中等待,然后锁池中的线程B获取同步锁,在满足一定要求后线程B调用notify(),唤醒等待池中的某一对象A(随机唤醒某一线程,如果使用notifyAll()则唤醒所有线程),将其从等待池中放入锁池,此时线程A有了获取同步锁的机会,等待线程调度.
  • 使用wait()会抛弃异常InterruptException,需要try,而不能throws,因为调用共享资源的类实现Runnable或者继承Thread,而他们都没有抛出过InterruptException(子类不能抛出父类没有抛出的异常这个).
  • 公共资源Resource采用了单例设计方法(懒汉式),将Resource的构造方法私有化,然后在类中创建一个resource对象,然后用一个静态方法return对象resource.
  • 采用实现的方法来创建线程new Thread(new Runnable() {} );(这个是匿名内部类,代码中没用,只是用在这里表示一下线程的创建).

采用Lock类来取代cynchronized,用Condition中的awati()代替wait().

ConditionCondition 将 Object 监视器方法(wait,notify 和 notifyAll)分解成截然不同的对象以便通过将这些对象与任意 Lock 实现组合使用为每个对象提供多个等待 set (wait-set).其中,Lock 替代了 synchronized 方法和语句的使用,Condition 替代了 Object 监视器方法的使用.

package Test;


import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class TEst {
    public static void main(String[] args) {
        new Thread(new Producer(Resource.getInstance()) , "正常人").start();
      //  new Thread(new Producer(Resource.getInstance()) , "机器人").start();
        new Thread(new Customer(Resource.getInstance())).start();
    }
}

class Resource {
    private static final int CAPACITY = 50;
    private List<String> nameList = new ArrayList<>(CAPACITY);
    private List<Integer> genderList = new ArrayList<>(CAPACITY);       //0:woman   1:man
    private final ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    private Resource (){}
    static private final Resource resource = new Resource();

    static Resource getInstance() {
        return resource;
    }

    public void put(String name , Integer gender)  {
        lock.lock();
        try {
            if ( nameList.size() == CAPACITY ) {
                condition.await();
            } if (!nameList.isEmpty()) {
                condition.signal();
            }
            Thread.sleep(5);
            nameList.add(Thread.currentThread().getName()+"  "+name);
            genderList.add(gender);
        } catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get() {
        lock.lock();
        try {
            if (nameList.isEmpty()) {
                condition.await();
            }
            if (nameList.size() < CAPACITY) {
                condition.signal();
            }
            System.out.println("姓名: " + nameList.get(nameList.size() - 1) + "  |  性别: " + genderList.get(genderList.size() - 1));
            genderList.remove(genderList.size() - 1);
            nameList.remove(nameList.size() - 1);
            Thread.sleep(5);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}




class Producer implements Runnable {
    private Resource resource;
    private String[] names = {"小红", "小明"};

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            resource.put(names[i%2],i%2);

        }
    }
}

class Customer implements Runnable {
    private Resource resource;

    public Customer(Resource resource) {
        this.resource = resource;
    }
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            resource.get();
        }
    }
}


猜你喜欢

转载自blog.csdn.net/reveire7/article/details/82667383