并发编程与高并发解决方案学习(J.U.C之AQS-CountDownLatch、Semaphore)_c高并发解决方案-程序员宅基地

技术标签: Java并发编程  并发编程  



※使用Node实现FIFO队列,可以用于构建锁或者其他同步装置的基础框架
※利用int类型表示状态 state=0表示还没有线程获取锁 state=1表示有现成获取锁,大于1表示可重入锁数量
※使用方法是继承 AQS使用模板方法模式,继承模板方法,复写其中的方法
※子类通过继承并通过实现它的方法管理其状态{acquire和release}的方法操作状态
※可以同时实现排它锁和共享锁模式( 独占、共享)

AQS同步组件( CountDownLatch、Semaphore、CyclicBarrier、ReentrantLock、Condition、FutureTask

CountDownLatch(作用类似于join()) 同步辅助类,可以实现阻塞的作用,同一时间只能有一个线程操作该计数器,调用该类的await类会一直处于阻塞状态,直到其他线程调用countDown方法,使当前计数器的值为0(计数器不可以重置)
使用场景:程序执行需要等待某个条件完成之后才能继续执行后续的操作,典型应用例如:并行计算( 当某个处理的运算量很大的时候,可以将该运算任务拆分为多个子任务,等待所有子任务都执行完之后,父任务再拿到所有子任务运行结果进行汇总)。
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CountDownLatchExample1 {
    private final static int threadCount = 20;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    test(threadNum);
                } catch (Exception e) {
                    log.error("exception", e);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        log.info("finish");
        exec.shutdown();
    }

    private static  void test(int threadNum) throws Exception {
        Thread.sleep(100);
        log.info("{}", threadNum);
        Thread.sleep(100);
    }
}

输出结果

15:54:06.904 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 0
15:54:06.904 [pool-1-thread-8] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 7
15:54:06.905 [pool-1-thread-19] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 18
15:54:06.904 [pool-1-thread-6] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 5
15:54:06.904 [pool-1-thread-7] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 6
15:54:06.904 [pool-1-thread-4] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 3
15:54:06.904 [pool-1-thread-5] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 4
15:54:06.904 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 2
15:54:06.904 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 1
15:54:06.904 [pool-1-thread-9] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 8
15:54:06.905 [pool-1-thread-18] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 17
15:54:06.904 [pool-1-thread-10] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 9
15:54:06.905 [pool-1-thread-17] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 16
15:54:06.904 [pool-1-thread-11] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 10
15:54:06.905 [pool-1-thread-20] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 19
15:54:06.905 [pool-1-thread-16] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 15
15:54:06.905 [pool-1-thread-13] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 12
15:54:06.904 [pool-1-thread-12] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 11
15:54:06.905 [pool-1-thread-15] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 14
15:54:06.905 [pool-1-thread-14] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - 13
15:54:07.009 [main] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample1 - finish

案例2(起了很多线程去完成任务,但是这个任务指向给指定的时间,操作这个时间没做完,也不管了

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CountDownLatchExample2 {
    private final static int threadCount = 200;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    test(threadNum);
                } catch (Exception e) {
                    log.error("exception", e);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(10, TimeUnit.MILLISECONDS);
        log.info("finish");
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        Thread.sleep(1000);
        log.info("{}", threadNum);
    }
}
输出结果
15:57:13.433 [main] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - finish
15:57:14.423 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 0
15:57:14.435 [pool-1-thread-4] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 3
15:57:14.436 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 2
15:57:14.436 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 1
15:57:14.438 [pool-1-thread-5] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 4
15:57:14.441 [pool-1-thread-8] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 7
15:57:14.442 [pool-1-thread-7] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 6
15:57:14.442 [pool-1-thread-6] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 5
15:57:14.442 [pool-1-thread-11] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 10
15:57:14.443 [pool-1-thread-10] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 9
15:57:14.443 [pool-1-thread-9] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 8
15:57:14.443 [pool-1-thread-13] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 12
15:57:14.443 [pool-1-thread-12] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 11
15:57:14.443 [pool-1-thread-15] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 14
15:57:14.444 [pool-1-thread-14] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 13
15:57:14.444 [pool-1-thread-20] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 19
15:57:14.444 [pool-1-thread-19] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 18
15:57:14.447 [pool-1-thread-18] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 17
15:57:14.447 [pool-1-thread-16] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 15
15:57:14.447 [pool-1-thread-17] INFO com.mmall.concurrency.example.aqs.CountDownLatchExample2 - 16
调用shutdown并不是第一时间将线程都全部销毁掉,而是让当前已有的线程全部执行完,之后再把这个线程池销毁掉。
Semaphore 信号量(控制并发访问的线程个数,类似于有限的链表,常用语仅能提供有限个访问的资源,比如 控制数据库连接数)
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreExample1 {
    private final static int threadCount = 20;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    semaphore.acquire(); // 每次获取一个许可
                    test(threadNum);
                    semaphore.release(); // 每次释放一个许可
                } catch (Exception e) {
                    log.error("exception", e);
                }
            });
        }
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000);
    }
}

输出结果(一次3条输出)

15:59:19.291 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 1
15:59:19.291 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 0
15:59:19.291 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 2
15:59:20.309 [pool-1-thread-4] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 3
15:59:20.310 [pool-1-thread-5] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 4
15:59:20.310 [pool-1-thread-6] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 5
15:59:21.310 [pool-1-thread-7] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 6
15:59:21.310 [pool-1-thread-8] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 7
15:59:21.311 [pool-1-thread-9] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 8
15:59:22.310 [pool-1-thread-10] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 9
15:59:22.310 [pool-1-thread-11] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 10
15:59:22.311 [pool-1-thread-12] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 11
15:59:23.311 [pool-1-thread-13] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 12
15:59:23.311 [pool-1-thread-14] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 13
15:59:23.312 [pool-1-thread-15] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 14
15:59:24.311 [pool-1-thread-16] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 15
15:59:24.311 [pool-1-thread-17] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 16
15:59:24.312 [pool-1-thread-18] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 17
15:59:25.312 [pool-1-thread-20] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 19
15:59:25.312 [pool-1-thread-19] INFO com.mmall.concurrency.example.aqs.SemaphoreExample1 - 18

import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreExample2 {
    private final static int threadCount = 20;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    semaphore.acquire(3); // 每次获取多个许可
                    test(threadNum);
                    semaphore.release(3); // 每次释放多个许可
                } catch (Exception e) {
                    log.error("exception", e);
                }
            });
        }
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000);
    }
}

输出结果(一次一条输出)

16:00:37.474 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 0
16:00:38.480 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 1
16:00:39.480 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 2
16:00:40.481 [pool-1-thread-4] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 3
16:00:41.482 [pool-1-thread-5] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 4
16:00:42.482 [pool-1-thread-6] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 5
16:00:43.483 [pool-1-thread-7] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 6
16:00:44.483 [pool-1-thread-8] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 7
16:00:45.484 [pool-1-thread-9] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 8
16:00:46.484 [pool-1-thread-10] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 9
16:00:47.484 [pool-1-thread-11] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 10
16:00:48.485 [pool-1-thread-12] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 11
16:00:49.485 [pool-1-thread-13] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 12
16:00:50.485 [pool-1-thread-14] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 13
16:00:51.486 [pool-1-thread-15] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 14
16:00:52.486 [pool-1-thread-17] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 16
16:00:53.486 [pool-1-thread-16] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 15
16:00:54.486 [pool-1-thread-18] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 17
16:00:55.487 [pool-1-thread-19] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 18
16:00:56.488 [pool-1-thread-20] INFO com.mmall.concurrency.example.aqs.SemaphoreExample2 - 19

案例(假如当前并发数是3,超过3个就丢弃)

import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

@Slf4j
public class SemaphoreExample3 {
    private final static int threadCount = 20;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    if (semaphore.tryAcquire()) { // 尝试获取一个许可
                        test(threadNum);
                        semaphore.release(); // 释放一个许可
                    }
                } catch (Exception e) {
                    log.error("exception", e);
                }
            });
        }
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000);
    }
}

输出结果

16:01:45.406 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.SemaphoreExample3 - 1
16:01:45.405 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.SemaphoreExample3 - 0
16:01:45.413 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.SemaphoreExample3 - 2
尝试获得许可,只让执行5秒
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

@Slf4j
public class SemaphoreExample4 {
    private final static int threadCount = 20;
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    if (semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS)) { // 尝试获取一个许可
                        test(threadNum);
                        semaphore.release(); // 释放一个许可
                    }
                } catch (Exception e) {
                    log.error("exception", e);
                }
            });
        }
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000);
    }
}

输出结果(一次三条输出)

16:02:31.454 [pool-1-thread-3] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 2
16:02:31.454 [pool-1-thread-1] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 0
16:02:31.454 [pool-1-thread-2] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 1
16:02:32.463 [pool-1-thread-4] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 3
16:02:32.464 [pool-1-thread-5] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 4
16:02:32.464 [pool-1-thread-6] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 5
16:02:33.464 [pool-1-thread-8] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 7
16:02:33.464 [pool-1-thread-7] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 6
16:02:33.465 [pool-1-thread-9] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 8
16:02:34.465 [pool-1-thread-10] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 9
16:02:34.465 [pool-1-thread-11] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 10
16:02:34.466 [pool-1-thread-12] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 11
16:02:35.465 [pool-1-thread-13] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 12
16:02:35.466 [pool-1-thread-14] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 13
16:02:35.466 [pool-1-thread-15] INFO com.mmall.concurrency.example.aqs.SemaphoreExample4 - 14


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Andy2019/article/details/79784000

智能推荐

稍微深入分析Ubuntu环境下安装NVIDIA驱动导致黑屏的原因_prime-select nvidia 黑屏-程序员宅基地

文章浏览阅读8.2k次,点赞5次,收藏48次。本文承接之前写的有关如何用正确姿势安装NVIDIA驱动的博文 (https://blog.csdn.net/Edward_ed_liu/article/details/109552761)。首先之所以要更新Linux内核,是因为不更新内核就无法使用笔记本自带的无线网卡。其次,目前NVIDIA官方不建议把Linux内核更新到最新版(5.9),而且这条消息只在英文的官网才有,中文的则是广告。TWICE如果强行更新到5.9版本,之后的Cuda安装表面上会显示成功,但在实际使用Cuda的过程中._prime-select nvidia 黑屏

java中间件有哪些_金九银十期间成功斩获58万架构师Offer!六面字节跳动面经和面试题分享 - 小梦爱Java...-程序员宅基地

文章浏览阅读167次。金九银十期间成功斩获58万Offer!六面字节跳动面经(成功关键:吃透九大核心知识+狂刷大厂面试真题)第一轮:团队面试第一轮基本上是你的团队成员面试你,是和你同级或者高你一个P的师兄来面你,我的话基本没问什么特别的,主要还是讲自己简历上的做的项目,这里需要你很熟悉自己的项目才行,我个人觉得这里你要把项目里你的角色做了什么没做什么讲清楚,然后最好能把自己做的那部分重点展开来讲,然后面试官会从你讲的内..._java架构师 中间件简历

【规则引擎】一、规则引擎简介_规则引擎 风险 功能-程序员宅基地

文章浏览阅读1.9k次。(第一章规则引擎学习入门之规则引擎简介)# 系列文章目录规则引擎简介前言一、为什么要使用规则引擎?1.不使用规则引擎的规则执行现状2. 规则引擎优点二、规则引擎的功能三、规则引擎的分类实现1.事中规则实现2.事后规则实现四、规则引擎调研1.开源规则引擎2.商业规则引擎五、Drools六、Aviator前言规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策。接受数据输入,解释业务规则,并根据业务规则做出业务决策。–来自百_规则引擎 风险 功能

oracle设置生成归档日志大小,Oracle 改变归档日志大小-程序员宅基地

文章浏览阅读1k次。该变归档日志大小只有改变日志组的大小!方法:加入新的大的日志文件,然后删掉旧的小的日志文件假设现有三个日志组,每个组内有一个成员,每个成员的大小为1MB,现在想把此三个日志组的成员大小都改为10MB1、创建2个新的日志组alter database add logfile group 4 ('D:\ORACLE\ORADATA\ORADB\REDO04_1.LOG') size 1024k;alt..._oracle归档日志空间大小

this.options在chrome浏览器提示undefined的解决办法_options is undefined-程序员宅基地

文章浏览阅读7.5k次。很早用的一段三级联动下拉菜单最近发现在chrome里不能联动下拉了,ie下正常,很奇怪,这段代码在之前有段时间经常用,没出现过什么问题,后来调试发现在“this.options.value”处提示“this.options is unfioned”,应该是浏览器之间js用法不同的问题,查资料测试后,改成“this.value”就正常了_options is undefined

整合Spring Cloud Bus报错_failed to instantiate [org.springframework.boot.ac-程序员宅基地

文章浏览阅读680次。spring: cloud: refresh: enabled: false2020-09-20 20:41:40.882 ERROR 13292 —[ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter :Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException.Me_failed to instantiate [org.springframework.boot.actuate.endpoint.web.servlet

随便推点

当下 React 项目该放弃的以及更好用的技术推荐-程序员宅基地

文章浏览阅读210次。React 版本推荐当前 React 都已经发布 18 了,虽然是个 alpha 版本,但是 17 确实也已经有大厂在用了。目前如果你的版本还停留在 v16.8 之前的话还是尽早升了吧。毕..._react 放弃

C++基础-资源管理:堆、栈与 RAII_c++heap内存堆管理-程序员宅基地

文章浏览阅读161次。堆,英文是 heap,在内存管理的语境下,指的是动态内存分配的区域,和数据结构中的“大根堆和小根堆”不是一个概念。同时,这里堆分配的内存需要手工释放,否则会造成内存泄漏。 C++ 标准里有一个和堆相关的概念是自由存储区,英文是 free store,特指使用 new 和 delete 来分配和释放内存的区域。一般而言,这是堆的一个子集。_c++heap内存堆管理

CentOS7中安装MySQL8.0.21爬坑记录:1045-Access denied、Job for firewalld.service failed等异常_error: failed to read file "/proc/sys/net/netfilte-程序员宅基地

文章浏览阅读1.3k次。在CentOS7.3中安装了MySQL8.0.21之后,就开启了一段漫长的爬坑历程,简要回顾如下:一、从Win10中用Navicat连接安装好的MySQL服务器出现如下异常:1045 - Access denied for user ‘root’@‘192.168.101.151’(using password: YES) 于是,在网上查阅了多篇博客,结论可能是3306端口没有加入到防火墙的允许列表。之后,开始研究了CentOS7中的防火墙,发现RHEL6之前版本用的防火墙管理工具都是iptab_error: failed to read file "/proc/sys/net/netfilter/nf_conntrack_helper": [e

Java位运算技巧_java巧用位运算记录用户-程序员宅基地

文章浏览阅读3.2k次,点赞6次,收藏16次。位运算作为底层的基本运算操作,往往是和'高效'二字沾边,适当的运用位运算来优化系统的核心代码,会让你的代码变得十分的精妙。以下是我所遇之的一些简单的位运算技巧作为博文记录。1.获得int型最大值 public static void main(String[] args) { int maxInt = (1 << 31) - 1; ..._java巧用位运算记录用户

HTML的列表标签,表格table和表单标签_html用ul写表格-程序员宅基地

文章浏览阅读710次。名词1名词1解释1...名词2名词2解释1名词2解释2..._html用ul写表格

HDU - 4333 Revolving Digits(扩展KMP)-程序员宅基地

文章浏览阅读187次。题目链接:点击查看题目大意:给出一个由 n 个数位组成的数字,现在可以通过将其不同的后缀移到前面来组成 n 个新的数字,现在要求出 n 个新数字进行去重后,有多少个新数字分别大于、等于、小于原数字如:1234进行上述转移可以得到的四个新数字分别为:1234,4123,3412,2341题目分析:如果暴力的比较虽然看似只需要枚举 n 个新的字符串,但是每个字符串的比较还需要花费O(n)的..._hdu - 4333