Skip to content

Commit 624b90f

Browse files
committed
add java-concurrent.md
1 parent d2f71fe commit 624b90f

File tree

29 files changed

+4723
-145
lines changed

29 files changed

+4723
-145
lines changed

docs/assets/images/touxiang.png

755 KB
Loading

docs/blog/posts/code-complete.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: Code-Complete
33
draft: false
44
authors: [huyi]
55
date: 2024-08-10
6-
slug: chance
6+
slug: code-complete
7+
description: 记录并总结常见技术概念和技术知识。
78
以下所言,或来自读书笔记,或来自书籍的勾画,或只是回忆...作为参考。
89
categories:
910
- 学习笔记

docs/blog/posts/java-concurrent.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Java-Concurrent
3+
draft: false
4+
authors: [huyi]
5+
date: 2024-09-12
6+
slug: juc
7+
description: 关于Java并发编程的一些知识总结与整理
8+
以下所言,或来自读书笔记,或来自书籍的勾画,或只是回忆...作为参考。
9+
categories:
10+
- 学习笔记
11+
- 并发
12+
- Java
13+
---
14+
15+
关于Java并发编程的一些知识总结与整理。 <!-- more -->
16+
17+
# Java并发
18+
19+
20+
21+
## 乐观锁与悲观锁
22+
23+
现实生活中,有资源就有竞争,我认为锁就是对有限资源的一种争取;程序中的锁也是一样,对共享资源(内容)进行操作,需要保证操作的安全性,即保证共享资源的完整性和一致性。
24+
25+
- **乐观锁**:认为共享资源每次访问的时候都不会出问题,不需要加锁,只需要**在提交修改时再判断对应的资源是否符合预期**(即在最终操作数据时做校验,而不是一上来就认为不行)。具体可以使用**数据库版本号或CAS技术**来实现。
26+
- **悲观锁**:认为共享资源每次访问时都会有问题,所以**每次获取数据都需要加锁(独享)**,直到自己操作完成后释放锁之后,其他线程才可以进行操作。Java中的`synchronized``ReentrantLock`等独占锁就是悲观锁思想的实现。
27+
28+
29+
30+
## CAS
31+
32+
**CAS(compare and swap)**是一种**乐观锁**的实现方式,全称是"**比较并交换**",是Java中一种**无锁的原子性操作**
33+
34+
CAS操作包含****个操作数——内存位置(V)、期望的原值(A)和新值(B)。
35+
36+
**工作原理**:当且仅当内存位置V的值等于预期原值A时,将该内存位置V的值替换为新值B。并且整个比较和更新操作都是原子操作。
37+
38+
> 无锁:指的是不通过锁机制对共享数据来实现线程安全
39+
40+
`java.util.concurrent.atomic`包下提供了一系列原子类(e.g. AtomicInteger、AtomicLong),它们内部都使用CAS操作来保证线程安全。底层通过sun.misc.Unsafe#compareAndSetInt方法实现(注:该方法是native方法)。
41+
42+
或者说CAS机制其实是通过`native boolean compareAndSetInt()`方法实现的,AtomicLong底层调用了该方法。
43+
44+
45+
46+
## 线程池
47+
48+
> 背景:当任务比较繁重,线程创建过多会带来调度开销,即线程的频繁创建与销毁,类似于TCP连接的频繁创建与销毁
49+
50+
线程池是一种线程使用模式,线程池中维护着多个线程,等待调度器分配可并发执行的任务,避免了在处理短时间任务时创建与销毁线程的代价。
51+
52+
特点/好处:
53+
54+
- **降低资源消耗**:重复利用已创建的线程来避免频繁的线程创建和销毁造成的消耗
55+
- **提高响应速度**:当出现可执行任务时,不需要等待线程的创建,任务可以立马被执行
56+
- **提高线程的可管理性**:线程是稀缺资源,如果无节制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控

docs/index.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# About
22

3-
For full documentation visit [mkdocs.org](https://www.mkdocs.org).
3+
Hello,Welcome to cxyhuky's blog.
44

5-
## Commands
5+
I am a server-side programmer.
66

7-
* `mkdocs new [dir-name]` - Create a new project.
8-
* `mkdocs serve` - Start the live-reloading docs server.
9-
* `mkdocs build` - Build the documentation site.
10-
* `mkdocs -h` - Print help message and exit.
7+
这只是一个开始!
118

12-
## Project layout
9+
## Hobby
1310

14-
mkdocs.yml # The configuration file.
15-
docs/
16-
index.md # The documentation homepage.
17-
... # Other markdown pages, images and other files.
11+
outdoors, mountain climbing, on foot.
12+
13+
I hope to complete my first snow mountain climb in 2025.
14+
15+
<img src=".\assets\images\touxiang.png" style="zoom:40%;" />

docs/templates/main.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{% extends "base.html" %}
2+
3+
{% block announce %}
4+
<center>
5+
You can follow <strong>@shenxiangzhuang</strong> on
6+
<a href="https://github.com/shenxiangzhuang">
7+
<span class="twemoji">
8+
{% include ".icons/fontawesome/brands/square-github.svg" %}
9+
</span>
10+
<strong>GitHub</strong>
11+
</a>
12+
or
13+
<strong>@MathewShen</strong> on
14+
<a href="https://twitter.com/MathewShen42">
15+
<span class="twemoji">
16+
{% include ".icons/fontawesome/brands/x-twitter.svg" %}
17+
</span>
18+
<strong>Twitter</strong>
19+
</a>
20+
and
21+
<a href="https://www.zhihu.com/people/shen-xiang-zhuang">
22+
<strong>知乎</strong>
23+
</a>
24+
or
25+
<strong>AI Glimpse</strong> on
26+
<!--暂时用 Why Beer 文章链接作为入口-->
27+
<a href="https://mp.weixin.qq.com/s/oq8viA9WFI87vUDtpNaV0Q">
28+
<span class="twemoji">
29+
{% include ".icons/fontawesome/brands/wechat.svg" %}
30+
</span>
31+
<strong>WeChat</strong>
32+
</a>
33+
</center>
34+
{% endblock %}
35+
36+
37+
{% block content %}
38+
{{ super() }}
39+
40+
<!-- Giscus -->
41+
<script src="https://giscus.app/client.js"
42+
data-repo="shenxiangzhuang/shenxiangzhuang.github.io"
43+
data-repo-id="MDEwOlJlcG9zaXRvcnk4MjU0MjM1OQ=="
44+
data-category="Announcements"
45+
data-category-id="DIC_kwDOBOt_F84CTHBH"
46+
data-mapping="pathname"
47+
data-strict="0"
48+
data-reactions-enabled="1"
49+
data-emit-metadata="0"
50+
data-input-position="top"
51+
data-theme="light"
52+
data-lang="en"
53+
crossorigin="anonymous"
54+
async>
55+
</script>
56+
57+
<!-- Synchronize Giscus theme with palette -->
58+
<script>
59+
var giscus = document.querySelector("script[src*=giscus]")
60+
61+
/* Set palette on initial load */
62+
var palette = __md_get("__palette")
63+
if (palette && typeof palette.color === "object") {
64+
var theme = palette.color.scheme === "slate" ? "dark" : "light"
65+
giscus.setAttribute("data-theme", theme)
66+
67+
68+
}
69+
70+
/* Register event handlers after documented loaded */
71+
document.addEventListener("DOMContentLoaded", function() {
72+
var ref = document.querySelector("[data-md-component=palette]")
73+
ref.addEventListener("change", function() {
74+
var palette = __md_get("__palette")
75+
if (palette && typeof palette.color === "object") {
76+
var theme = palette.color.scheme === "slate" ? "dark" : "light"
77+
78+
/* Instruct Giscus to change theme */
79+
var frame = document.querySelector(".giscus-frame")
80+
frame.contentWindow.postMessage(
81+
{ giscus: { setConfig: { theme } } },
82+
"https://giscus.app"
83+
)
84+
}
85+
})
86+
})
87+
</script>
88+
{% endblock %}

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ theme:
3838
- navigation.indexes
3939
- navigation.prune
4040
- navigation.sections
41+
- search.suggest
4142

4243
plugins:
4344
- autorefs
@@ -50,6 +51,7 @@ plugins:
5051
- tags
5152
- blog:
5253
draft: true
54+
- search:
5355
# - minify:
5456
# minify_html: true
5557
# - bibtex:

site/404.html

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,45 @@
114114

115115

116116

117+
<label class="md-header__button md-icon" for="__search">
118+
119+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
120+
</label>
121+
<div class="md-search" data-md-component="search" role="dialog">
122+
<label class="md-search__overlay" for="__search"></label>
123+
<div class="md-search__inner" role="search">
124+
<form class="md-search__form" name="search">
125+
<input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
126+
<label class="md-search__icon md-icon" for="__search">
127+
128+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
129+
130+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
131+
</label>
132+
<nav class="md-search__options" aria-label="Search">
133+
134+
<button type="reset" class="md-search__icon md-icon" title="Clear" aria-label="Clear" tabindex="-1">
135+
136+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
137+
</button>
138+
</nav>
139+
140+
<div class="md-search__suggest" data-md-component="search-suggest"></div>
141+
142+
</form>
143+
<div class="md-search__output">
144+
<div class="md-search__scrollwrap" tabindex="0" data-md-scrollfix>
145+
<div class="md-search-result" data-md-component="search-result">
146+
<div class="md-search-result__meta">
147+
Initializing search
148+
</div>
149+
<ol class="md-search-result__list" role="presentation"></ol>
150+
</div>
151+
</div>
152+
</div>
153+
</div>
154+
</div>
155+
117156

118157
<div class="md-header__source">
119158
<a href="https://github.com/cxyhuky/cxyhuky.github.io" title="Go to repository" class="md-source" data-md-component="source">
@@ -747,7 +786,7 @@ <h1>404 - Not found</h1>
747786
</div>
748787

749788

750-
<script id="__config" type="application/json">{"base": "/", "features": ["navigation.tabs", "navigation.tabs.sticky", "navigation.top", "navigation.path", "navigation.instant", "navigation.indexes", "navigation.prune", "navigation.sections"], "search": "/assets/javascripts/workers/search.b8dbb3d2.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
789+
<script id="__config" type="application/json">{"base": "/", "features": ["navigation.tabs", "navigation.tabs.sticky", "navigation.top", "navigation.path", "navigation.instant", "navigation.indexes", "navigation.prune", "navigation.sections", "search.suggest"], "search": "/assets/javascripts/workers/search.b8dbb3d2.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
751790

752791

753792
<script src="/assets/javascripts/bundle.fe8b6f2b.min.js"></script>

0 commit comments

Comments
 (0)