Java生产者消费者五种方法

一、blocking+queue

package language.java.thread.messeging.blocking_queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Producer extends Thread {

    private final BlockingQueue<Object> queue;

    public Producer(BlockingQueue<Object> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                int item = 1;
                queue.put(item);
                System.out.println("produce item : " + item);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.blocking_queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Consumer extends Thread {

    private final BlockingQueue<Object> queue;

    public Consumer(BlockingQueue<Object> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                final Object item = queue.take();
                System.out.println("consume item : " + item);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.blocking_queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Main {
    public static void main(String[] args) {
        int size = 100;
        BlockingQueue<Object> queue = new LinkedBlockingQueue<>(size);
        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);
        producer.start();
        consumer.start();
    }
}


二、lock+双condition

package language.java.thread.messeging.lock_condition;

import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Producer extends Thread {

    private final int size;
    private final Queue<Object> queue;
    private final Lock lock;
    private final Condition notFull;
    private final Condition notEmpty;

    public Producer(int size, Queue<Object> queue, Lock lock, Condition notFull, Condition notEmpty) {
        this.size = size;
        this.queue = queue;
        this.lock = lock;
        this.notFull = notFull;
        this.notEmpty = notEmpty;
    }

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (queue.size() == size) {
                    try {
                        notFull.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int item = 1;
                queue.add(item);
                System.out.println("produce item : " + item);
                notEmpty.signal();
            } finally {
                lock.unlock();
            }
        }
    }
}
package language.java.thread.messeging.lock_condition;

import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 5:04 下午
 */
public class Consumer extends Thread {

    private final Queue<Object> queue;
    private final Lock lock;
    private final Condition notFull;
    private final Condition notEmpty;

    public Consumer(Queue<Object> queue, Lock lock, Condition notFull, Condition notEmpty) {
        this.queue = queue;
        this.lock = lock;
        this.notFull = notFull;
        this.notEmpty = notEmpty;
    }

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (queue.isEmpty()) {
                    try {
                        notFull.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                final Object item = queue.poll();
                System.out.println("consume item : " + item);
                notEmpty.signal();
            } finally {
                lock.unlock();
            }
        }
    }
}
package language.java.thread.messeging.lock_condition;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Main {
    public static void main(String[] args) {
        Queue<Object> queue = new LinkedList<>();
        int size = 100;
        Lock lock = new ReentrantLock();
        Condition notFull = lock.newCondition();
        Condition notEmpty = lock.newCondition();
        Producer producer = new Producer(size, queue, lock, notFull, notEmpty);
        Consumer consumer = new Consumer(queue, lock, notFull, notEmpty);
        producer.start();
        consumer.start();
    }
}


三、pipedstream

package language.java.thread.messeging.piped_stream;

import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Producer extends Thread {

    private final PipedOutputStream pos;

    public Producer(PipedOutputStream pos) {
        this.pos = pos;
    }

    @Override
    public void run() {
        while (true) {
            try {
                int item = 1;
                pos.write(item);
                pos.flush();
                System.out.println("produce item : " + item);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.piped_stream;

import java.io.IOException;
import java.io.PipedInputStream;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Consumer extends Thread {

    private final PipedInputStream pis;

    public Consumer(PipedInputStream pis) {
        this.pis = pis;
    }

    @Override
    public void run() {
        while (true) {
            try {
                final int item = pis.read();
                System.out.println("produce item : " + item);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.piped_stream;

import language.java.thread.messeging.synchronized_object.Consumer;
import language.java.thread.messeging.synchronized_object.Producer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Main {
    public static void main(String[] args) {
        Queue<Object> queue = new LinkedList<>();
        int size = 100;
        Producer producer = new Producer(queue, size);
        Consumer consumer = new Consumer(queue);
        producer.start();
        consumer.start();
    }
}


四、三个semaphore

package language.java.thread.messeging.semaphore;

import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Producer extends Thread {

    private Queue<Object> queue;
    private Semaphore notFull;
    private Semaphore notEmpty;
    private Semaphore mutex;

    public Producer(Queue<Object> queue,
                    Semaphore notFull,
                    Semaphore notEmpty,
                    Semaphore mutex) {
        this.queue = queue;
        this.notFull = notFull;
        this.notEmpty = notEmpty;
        this.mutex = mutex;
    }

    @Override
    public void run() {
        while (true) {
            try {
                notFull.acquire();
                mutex.acquire();
                int item = 1;
                queue.add(item);
                System.out.println("produce item : " + item);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                mutex.release();
                notEmpty.release();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.semaphore;

import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Consumer extends Thread {

    private Queue<Object> queue;
    private Semaphore notFull;
    private Semaphore notEmpty;
    private Semaphore mutex;


    public Consumer(Queue<Object> queue,
                    Semaphore notFull,
                    Semaphore notEmpty,
                    Semaphore mutex) {
        this.queue = queue;
        this.notFull = notFull;
        this.notEmpty = notEmpty;
        this.mutex = mutex;
    }

    @Override
    public void run() {
        while (true) {
            try {
                notEmpty.acquire();
                mutex.acquire();
                final Object item = queue.poll();
                System.out.println("produce item : " + item);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                mutex.release();
                notFull.release();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.semaphore;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Semaphore;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Main {
    public static void main(String[] args) {
        Queue<Object> queue = new LinkedList<>();
        int size = 100;

        Semaphore notFull = new Semaphore(size);
        Semaphore notEmpty = new Semaphore(0);
        Semaphore mutex = new Semaphore(1);

        Producer producer = new Producer(queue, notFull, notEmpty, mutex);
        Consumer consumer = new Consumer(queue, notFull, notEmpty, mutex);

        producer.start();
        consumer.start();
    }
}


五、synchronized+object

package language.java.thread.messeging.synchronized_object;

import java.util.Queue;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Producer extends Thread {

    private final int size;
    private final Queue<Object> queue;

    public Producer(Queue<Object> queue, int size) {
        this.queue = queue;
        this.size = size;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                if (queue.size() == size) {
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int item = 1;
                queue.add(item);
                System.out.println("produce item : " + item);
                queue.notify();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.synchronized_object;

import java.util.Queue;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Consumer extends Thread {

    private final Queue<Object> queue;

    public Consumer(Queue<Object> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                if (queue.isEmpty()) {
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                final Object item = queue.poll();
                System.out.println("consume item : " + item);
                queue.notify();
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package language.java.thread.messeging.synchronized_object;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author <a href="mailto:zhanghuirui163@163.com">zhanghuirui</a>
 * @date 2020/4/30 4:47 下午
 */
public class Main {
    public static void main(String[] args) {
        Queue<Object> queue = new LinkedList<>();
        int size = 100;
        Producer producer = new Producer(queue, size);
        Consumer consumer = new Consumer(queue);
        producer.start();
        consumer.start();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值