找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 129|回复: 2

多线程面向demo编程前后对比

[复制链接]

373

主题

55

回帖

1944

积分

管理员

积分
1944
发表于 2020-11-3 09:46:01 | 显示全部楼层 |阅读模式
public class ProdConsumerDemo {
    public static int shareNumber = 1000;

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        // 15个生产者
        for (int i = 0; i < 15; i++) {
            new Thread(()->{
                while(true){
                    try {
                        lock.lock();
                        shareNumber ++;
                        System.out.println("生产者工作,当前资源:" + shareNumber);
                        condition.signalAll();
                    } finally {
                        lock.unlock();
                    }
                }
            }).start();
        }

        //30个消费者
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                while (true){
                    try{
                        lock.lock();

                        while(shareNumber == 0){
                            condition.await();
                        }
                        shareNumber--;
                        System.out.println("消费者工作,当前资源:" + shareNumber);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally{
                        lock.unlock();
                    }
                }

            }).start();
        }

    }
}
回复

使用道具 举报

373

主题

55

回帖

1944

积分

管理员

积分
1944
 楼主| 发表于 2020-11-3 09:55:52 | 显示全部楼层
package com.example.demo.thread;

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

public class ProdConsumerDemo02 {
    public static void main(String[] args) {
        ShareEntry shareEntry = new ShareEntry(3000);

        // 15 个生产者
        for (int i = 0; i < 15; i++) {
            new Thread(() -> {
                while (true) {
                    shareEntry.increment();
                }

            }).start();
        }

        // 30个消费者
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                while (true) {
                    shareEntry.deIncrement();
                }
            }).start();
        }
    }

    private static class ShareEntry {
        private int shareNumber;
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();

        private ShareEntry(int shareNumber) {
            this.shareNumber = shareNumber;
        }

        private void increment() {
            try {
                lock.lock();

                shareNumber++;
                System.out.println("生产者工作, 当前剩余资源:" + shareNumber);
                // 资源增加后可以唤醒消费者
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }

        private void deIncrement() {
            try {
                lock.lock();

                while (shareNumber == 0) {
                    condition.await();
                }

                shareNumber--;
                System.out.println("消费者工作, 当前剩余资源:" + shareNumber);
                // 因为生产者不会阻塞, 因此不需要唤醒生产者
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}

回复

使用道具 举报

373

主题

55

回帖

1944

积分

管理员

积分
1944
 楼主| 发表于 2020-11-3 09:56:49 | 显示全部楼层
对共享资源的操作往往是调用对象方法进行的,就是对资源进行操作,只需要在资源的层级上加锁以及同步线程即可
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, 2024-9-20 10:26 , Processed in 0.032914 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表