| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e26afb3 commit ffcccdc
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -326,9 +326,9 @@ myStack.pop();//报错:java.lang.IllegalArgumentException: Stack is empty. | |||
| 326 | 326 | 当我们需要按照一定顺序来处理数据的时候可以考虑使用队列这个数据结构。 | |
| 327 | 327 | ||
| 328 | 328 | - **阻塞队列:** 阻塞队列可以看成在队列基础上加了阻塞操作的队列。当队列为空的时候,出队操作阻塞,当队列满的时候,入队操作阻塞。使用阻塞队列我们可以很容易实现“生产者 - 消费者“模型。 | |
| 329 | - - **线程池中的请求/任务队列:** 线程池中没有空闲线程时,新的任务请求线程资源时,线程池该如何处理呢?答案是将这些请求放在队列中,当有空闲线程的时候,会循环中反复从队列中获取任务来执行。队列分为无界队列(基于链表)和有界队列(基于数组)。无界队列的特点就是可以一直入列,除非系统资源耗尽,比如:`FixedThreadPool` 使用无界队列 `LinkedBlockingQueue`。但是有界队列就不一样了,当队列满的话后面再有任务/请求就会拒绝,在 Java 中的体现就是会抛出`java.util.concurrent.RejectedExecutionException` 异常。 | ||
| 330 | - - 栈:双端队列天生便可以实现栈的全部功能(`push`、`pop` 和 `peek`),并且在 Deque 接口中已经实现了相关方法。Stack 类已经和 Vector 一样被遗弃,现在在 Java 中普遍使用双端队列(Deque)来实现栈。 | ||
| 331 | - - 广度优先搜索(BFS),在图的广度优先搜索过程中,队列被用于存储待访问的节点,保证按照层次顺序遍历图的节点。 | ||
| 329 | + - **线程池中的请求/任务队列:** 当线程池中没有空闲线程时,新的任务请求线程资源会被如何处理呢?答案是这些任务会被放入任务队列中,等待线程池中的线程空闲后再从队列中取出任务执行。任务队列分为无界队列(基于链表实现)和有界队列(基于数组实现)。无界队列的特点是队列容量理论上没有限制,任务可以持续入队,直到系统资源耗尽。例如:`FixedThreadPool` 使用的阻塞队列 `LinkedBlockingQueue`,其默认容量为 `Integer.MAX_VALUE`,因此可以被视为“无界队列”。而有界队列则不同,当队列已满时,如果再有新任务提交,由于队列无法继续容纳任务,线程池会拒绝这些任务,并抛出 `java.util.concurrent.RejectedExecutionException` 异常。 | ||
| 330 | + - **栈**:双端队列天生便可以实现栈的全部功能(`push`、`pop` 和 `peek`),并且在 Deque 接口中已经实现了相关方法。Stack 类已经和 Vector 一样被遗弃,现在在 Java 中普遍使用双端队列(Deque)来实现栈。 | ||
| 331 | + - **广度优先搜索(BFS)**:在图的广度优先搜索过程中,队列被用于存储待访问的节点,保证按照层次顺序遍历图的节点。 | ||
| 332 | 332 | - Linux 内核进程队列(按优先级排队) | |
| 333 | 333 | - 现实生活中的派对,播放器上的播放列表; | |
| 334 | 334 | - 消息队列 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,16 @@ PS:篇幅问题,我这并没有对上面提到的分布式缓存选型做详 | |||
| 114 | 114 | ||
| 115 | 115 | Redis 除了可以用作缓存之外,还可以用于分布式锁、限流、消息队列、延时队列等场景,功能强大! | |
| 116 | 116 | ||
| 117 | + ### 为什么用 Redis 而不用本地缓存呢? | ||
| 118 | + | ||
| 119 | + | 特性 | 本地缓存 | Redis | | ||
| 120 | + | ------------ | ------------------------------------ | -------------------------------- | | ||
| 121 | + | 数据一致性 | 多服务器部署时存在数据不一致问题 | 数据一致 | | ||
| 122 | + | 内存限制 | 受限于单台服务器内存 | 独立部署,内存空间更大 | | ||
| 123 | + | 数据丢失风险 | 服务器宕机数据丢失 | 可持久化,数据不易丢失 | | ||
| 124 | + | 管理维护 | 分散,管理不便 | 集中管理,提供丰富的管理工具 | | ||
| 125 | + | 功能丰富性 | 功能有限,通常只提供简单的键值对存储 | 功能丰富,支持多种数据结构和功能 | | ||
| 126 | + | ||
| 117 | 127 | ### 常见的缓存读写策略有哪些? | |
| 118 | 128 | ||
| 119 | 129 | 关于常见的缓存读写策略的详细介绍,可以看我写的这篇文章:[3 种常用的缓存读写策略详解](https://javaguide.cn/database/redis/3-commonly-used-cache-read-and-write-strategies.html) 。 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,14 +89,6 @@ Finally | |||
| 89 | 89 | ||
| 90 | 90 | **注意:不要在 finally 语句块中使用 return!** 当 try 语句和 finally 语句中都有 return 语句时,try 语句块中的 return 语句会被忽略。这是因为 try 语句中的 return 返回值会先被暂存在一个本地变量中,当执行到 finally 语句中的 return 之后,这个本地变量的值就变为了 finally 语句中的 return 返回值。 | |
| 91 | 91 | ||
| 92 | - [jvm 官方文档](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10.2.5)中有明确提到: | ||
| 93 | - | ||
| 94 | - > If the `try` clause executes a _return_, the compiled code does the following: | ||
| 95 | - > | ||
| 96 | - > 1. Saves the return value (if any) in a local variable. | ||
| 97 | - > 2. Executes a _jsr_ to the code for the `finally` clause. | ||
| 98 | - > 3. Upon return from the `finally` clause, returns the value saved in the local variable. | ||
| 99 | - | ||
| 100 | 92 | 代码示例: | |
| 101 | 93 | ||
| 102 | 94 | ```java | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -276,23 +276,21 @@ TTL 改造的地方有两处: | |||
| 276 | 276 | ||
| 277 | 277 | 另外,《阿里巴巴 Java 开发手册》中强制线程池不允许使用 `Executors` 去创建,而是通过 `ThreadPoolExecutor` 构造函数的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险 | |
| 278 | 278 | ||
| 279 | - `Executors` 返回线程池对象的弊端如下: | ||
| 279 | + `Executors` 返回线程池对象的弊端如下(后文会详细介绍到): | ||
| 280 | 280 | ||
| 281 | - - `FixedThreadPool` 和 `SingleThreadExecutor`:使用的是有界阻塞队列是 `LinkedBlockingQueue` ,其任务队列的最大长度为 `Integer.MAX_VALUE` ,可能堆积大量的请求,从而导致 OOM。 | ||
| 281 | + - `FixedThreadPool` 和 `SingleThreadExecutor`:使用的是阻塞队列 `LinkedBlockingQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可以看作是无界的,可能堆积大量的请求,从而导致 OOM。 | ||
| 282 | 282 | - `CachedThreadPool`:使用的是同步队列 `SynchronousQueue`, 允许创建的线程数量为 `Integer.MAX_VALUE` ,如果任务数量过多且执行速度较慢,可能会创建大量的线程,从而导致 OOM。 | |
| 283 | - - `ScheduledThreadPool` 和 `SingleThreadScheduledExecutor` :使用的无界的延迟阻塞队列 `DelayedWorkQueue` ,任务队列最大长度为 `Integer.MAX_VALUE` ,可能堆积大量的请求,从而导致 OOM。 | ||
| 283 | + - `ScheduledThreadPool` 和 `SingleThreadScheduledExecutor`:使用的无界的延迟阻塞队列`DelayedWorkQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可能堆积大量的请求,从而导致 OOM。 | ||
| 284 | 284 | ||
| 285 | 285 | ```java | |
| 286 | - // 有界队列 LinkedBlockingQueue | ||
| 287 | 286 | public static ExecutorService newFixedThreadPool(int nThreads) { | |
| 288 | - | ||
| 287 | + // LinkedBlockingQueue 的默认长度为 Integer.MAX_VALUE,可以看作是无界的 | ||
| 289 | 288 | return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()); | |
| 290 | 289 | ||
| 291 | 290 | } | |
| 292 | 291 | ||
| 293 | - // 无界队列 LinkedBlockingQueue | ||
| 294 | 292 | public static ExecutorService newSingleThreadExecutor() { | |
| 295 | - | ||
| 293 | + // LinkedBlockingQueue 的默认长度为 Integer.MAX_VALUE,可以看作是无界的 | ||
| 296 | 294 | return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>())); | |
| 297 | 295 | ||
| 298 | 296 | } | |
@@ -886,6 +884,7 @@ public FutureTask(Runnable runnable, V result) { | |||
| 886 | 884 | `FutureTask`相当于对`Callable` 进行了封装,管理着任务执行的情况,存储了 `Callable` 的 `call` 方法的任务执行结果。 | |
| 887 | 885 | ||
| 888 | 886 | 关于更多 `Future` 的源码细节,可以肝这篇万字解析,写的很清楚:[Java是如何实现Future模式的?万字详解!](https://juejin.cn/post/6844904199625375757)。 | |
| 887 | + | ||
| 889 | 888 | ### CompletableFuture 类有什么用? | |
| 890 | 889 | ||
| 891 | 890 | `Future` 在实际使用过程中存在一些局限性比如不支持异步任务的编排组合、获取计算结果的 `get()` 方法为阻塞调用。 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ tag: | |||
| 13 | 13 | ||
| 14 | 14 | `Executors` 返回线程池对象的弊端如下(后文会详细介绍到): | |
| 15 | 15 | ||
| 16 | - - **`FixedThreadPool` 和 `SingleThreadExecutor`**:使用的是有界阻塞队列 `LinkedBlockingQueue`,任务队列的默认长度和最大长度为 `Integer.MAX_VALUE`,可能堆积大量的请求,从而导致 OOM。 | ||
| 16 | + - **`FixedThreadPool` 和 `SingleThreadExecutor`**:使用的是阻塞队列 `LinkedBlockingQueue`,任务队列的默认长度和最大长度为 `Integer.MAX_VALUE`,可以看作是无界队列,可能堆积大量的请求,从而导致 OOM。 | ||
| 17 | 17 | - **`CachedThreadPool`**:使用的是同步队列 `SynchronousQueue`,允许创建的线程数量为 `Integer.MAX_VALUE` ,可能会创建大量线程,从而导致 OOM。 | |
| 18 | 18 | - **`ScheduledThreadPool` 和 `SingleThreadScheduledExecutor`** : 使用的无界的延迟阻塞队列`DelayedWorkQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可能堆积大量的请求,从而导致 OOM。 | |
| 19 | 19 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,21 +183,19 @@ public static class CallerRunsPolicy implements RejectedExecutionHandler { | |||
| 183 | 183 | ||
| 184 | 184 | `Executors` 返回线程池对象的弊端如下(后文会详细介绍到): | |
| 185 | 185 | ||
| 186 | - - `FixedThreadPool` 和 `SingleThreadExecutor`:使用的是无界的 `LinkedBlockingQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可能堆积大量的请求,从而导致 OOM。 | ||
| 186 | + - `FixedThreadPool` 和 `SingleThreadExecutor`:使用的是阻塞队列 `LinkedBlockingQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可以看作是无界的,可能堆积大量的请求,从而导致 OOM。 | ||
| 187 | 187 | - `CachedThreadPool`:使用的是同步队列 `SynchronousQueue`, 允许创建的线程数量为 `Integer.MAX_VALUE` ,如果任务数量过多且执行速度较慢,可能会创建大量的线程,从而导致 OOM。 | |
| 188 | 188 | - `ScheduledThreadPool` 和 `SingleThreadScheduledExecutor`:使用的无界的延迟阻塞队列`DelayedWorkQueue`,任务队列最大长度为 `Integer.MAX_VALUE`,可能堆积大量的请求,从而导致 OOM。 | |
| 189 | 189 | ||
| 190 | 190 | ```java | |
| 191 | - // 无界队列 LinkedBlockingQueue | ||
| 192 | 191 | public static ExecutorService newFixedThreadPool(int nThreads) { | |
| 193 | - | ||
| 192 | + // LinkedBlockingQueue 的默认长度为 Integer.MAX_VALUE,可以看作是无界的 | ||
| 194 | 193 | return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()); | |
| 195 | 194 | ||
| 196 | 195 | } | |
| 197 | 196 | ||
| 198 | - // 无界队列 LinkedBlockingQueue | ||
| 199 | 197 | public static ExecutorService newSingleThreadExecutor() { | |
| 200 | - | ||
| 198 | + // LinkedBlockingQueue 的默认长度为 Integer.MAX_VALUE,可以看作是无界的 | ||
| 201 | 199 | return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>())); | |
| 202 | 200 | ||
| 203 | 201 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments