From e86dcf9ce68b9875676f0999d6f766d88326d13f Mon Sep 17 00:00:00 2001 From: guide Date: Thu, 3 Mar 2022 16:20:18 +0800 Subject: [PATCH] =?UTF-8?q?[docs=20fix]=20java=E5=9F=BA=E7=A1=80-=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vuepress/themeConfig.ts | 2 +- docs/java/basis/java-basic-questions-03.md | 12 +++++++++++- docs/java/concurrent/java-concurrent-questions-01.md | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/.vuepress/themeConfig.ts b/docs/.vuepress/themeConfig.ts index 3da19d92..26329b9f 100644 --- a/docs/.vuepress/themeConfig.ts +++ b/docs/.vuepress/themeConfig.ts @@ -41,7 +41,7 @@ export default defineThemeConfig({ // }, pwa: { favicon: "/favicon.ico", - cachePic: false, + cachePic: true, cacheHTML: false, apple: { icon: "/assets/icon/apple-icon-152.png", diff --git a/docs/java/basis/java-basic-questions-03.md b/docs/java/basis/java-basic-questions-03.md index b5a4559f..a4320573 100644 --- a/docs/java/basis/java-basic-questions-03.md +++ b/docs/java/basis/java-basic-questions-03.md @@ -265,7 +265,17 @@ Catch Exception -> RuntimeException Finally ``` -**注意:不要在 finally 语句块中使用 return!** 当 try 语句和 finally 语句中都有 return 语句时,try 语句块中的 return 语句不会被执行。 +**注意:不要在 finally 语句块中使用 return!** 当 try 语句和 finally 语句中都有 return 语句时,try 语句块中的 return 语句会被忽略。这是因为 try 语句中的 return 返回值会先被暂存在一个本地变量中,当执行到 finally 语句中的 return 之后,这个本地变量的值就变为了 finally 语句中的 return 返回值。 + +[jvm 官方文档](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10.2.5)中有明确提到: + +> If the `try` clause executes a *return*, the compiled code does the following: +> +> 1. Saves the return value (if any) in a local variable. +> 2. Executes a *jsr* to the code for the `finally` clause. +> 3. Upon return from the `finally` clause, returns the value saved in the local variable. + +先执行一部分的,先把返回的结果存到一段内存中; 示例: diff --git a/docs/java/concurrent/java-concurrent-questions-01.md b/docs/java/concurrent/java-concurrent-questions-01.md index 2cf5c7cf..641409fa 100644 --- a/docs/java/concurrent/java-concurrent-questions-01.md +++ b/docs/java/concurrent/java-concurrent-questions-01.md @@ -228,7 +228,7 @@ Thread[线程 2,5,main]waiting get resource1 避免死锁就是在资源分配时,借助于算法(比如银行家算法)对资源分配进行计算评估,使其进入安全状态。 -**安全状态** 指的是系统能够按照某种进程推进顺序(P1、P2、P3.....Pn)来为每个进程分配所需资源,直到满足每个进程对资源的最大需求,使每个进程都可顺利完成。称序列为安全序列。 +> **安全状态** 指的是系统能够按照某种线程推进顺序(P1、P2、P3.....Pn)来为每个线程分配所需资源,直到满足每个线程对资源的最大需求,使每个线程都可顺利完成。称序列为安全序列。 我们对线程 2 的代码修改成下面这样就不会产生死锁了。