多线程生产消费模型的三种实现

1. synchornized实现

public class MyTest {
    private static String str = "";
    private static final Object lock = new Object();
    private static AtomicInteger num = new AtomicInteger(0);
    static class Provider extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    while (true) {
                        Thread.sleep(200L);
                        if (!"".equals(str)) {
                            lock.wait();
                        }
                        str = "provide";
                        int incrementAndGet = num.incrementAndGet();
                        System.out.println("生产者"+incrementAndGet);
                        lock.notifyAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    static class Consumer extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    while (true) {
                        Thread.sleep(200L);
                        if ("".equals(str)) {
                            lock.wait();
                        }
                        str = "";
                        int decrementAndGet = num.getAndDecrement();
                        System.out.println("消费"+decrementAndGet);
                        lock.notifyAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        new Provider().start();
        new Consumer().start();
    }
}

2. Lock实现

public class MyTest2 {
    private static String str = "";
    private static Lock lock = new ReentrantLock();
    private static Condition providerCondition = lock.newCondition();
    private static Condition consumerCondition = lock.newCondition();

    private static AtomicInteger num = new AtomicInteger(0);

    static class Provider extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    lock.lock();
                    Thread.sleep(200L);
                    if (!"".equals(str)) {
                        providerCondition.await();
                    }
                    str = "provide";
                    int incrementAndGet = num.incrementAndGet();
                    System.out.println("生产者" + incrementAndGet);
                    consumerCondition.signalAll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    static class Consumer extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    lock.lock();
                    Thread.sleep(200L);
                    if ("".equals(str)) {
                        consumerCondition.await();
                    }
                    str = "";
                    int decrementAndGet = num.getAndDecrement();
                    System.out.println("消费" + decrementAndGet);
                    providerCondition.signalAll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        new Provider().start();
        new Consumer().start();
    }

}

3. BlockingQueue实现

public class MyTest3 {

    private static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();

    private static AtomicInteger num = new AtomicInteger(0);

    static class Provider extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100L);queue.add("");
                    queue.put("sss");
                    int incrementAndGet = num.incrementAndGet();
                    System.out.println("生产者" + incrementAndGet);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Consumer extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100L);
                    String take = queue.take();
                    int andDecrement = num.getAndDecrement();
                    System.out.println("消费者" + andDecrement);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        new Provider().start();
        new Consumer().start();
    }

}

运行结果:

生产者1
消费者1
生产者1
消费者1
生产者1
消费者1
发布了52 篇原创文章 · 获赞 7 · 访问量 3804

猜你喜欢

转载自blog.csdn.net/maomaoqiukqq/article/details/100597622