Skip to content

Commit a437c20

Browse files
committed
Update JVM and 多线程
1 parent 0d16f1b commit a437c20

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

MD/Java基础-JVM原理.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ STW总会发生,不管是新生代还是老年代,比如CMS在初始标记
136136
那么为什么一定要STW?因为在定位堆中的对象时JVM会记录下对所有对象的引用,如果在定位对象过程中,有新的对象被分配或者刚记录下的对象突然变得无法访问,就会导致一些问题,比如部分对象无法被回收,更严重的是如果GC期间分配的一个GC Root对象引用了准备被回收的对象,那么该对象就会被错误地回收。
137137

138138
## JVM调优
139-
https://www.ibm.com/developerworks/cn/java/j-lo-jvm-optimize-experience/index.html
139+
基础:https://www.ibm.com/developerworks/cn/java/j-lo-jvm-optimize-experience/index.html
140+
141+
案例:https://www.wangtianyi.top/blog/2018/07/27/jvmdiao-you-ru-men-er-shi-zhan-diao-you-parallelshou-ji-qi/
140142

141143
欢迎光临[我的博客](http://www.wangtianyi.top/?utm_source=github&utm_medium=github),发现更多技术资源~

MD/Java基础-多线程.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,55 @@ public class CountDownLatchTest {
139139
使用场景:
140140
有四个游戏玩家玩游戏,游戏有三个关卡,每个关卡必须要所有玩家都到达后才能允许通过。其实这个场景里的玩家中如果有玩家A先到了关卡1,他必须等到其他所有玩家都到达关卡1时才能通过,也就是说线程之间需要相互等待。
141141

142+
### 编程题
143+
交替打印奇偶数
144+
145+
```java
146+
public class PrintOddAndEvenShu {
147+
private int value = 0;
148+
149+
private synchronized void printOdd() {
150+
while (value <= 100) {
151+
if (value % 2 == 1) {
152+
System.out.println(Thread.currentThread() + ": -" + value++);
153+
this.notify();
154+
} else {
155+
try {
156+
this.wait();
157+
} catch (InterruptedException e) {
158+
e.printStackTrace();
159+
}
160+
}
161+
162+
}
163+
164+
}
165+
166+
private synchronized void printEven() {
167+
while (value <= 100) {
168+
if (value % 2 == 0) {
169+
System.out.println(Thread.currentThread() + ": --" + value++);
170+
this.notify();
171+
} else {
172+
try {
173+
this.wait();
174+
} catch (InterruptedException e) {
175+
e.printStackTrace();
176+
}
177+
}
178+
}
179+
}
180+
181+
public static void main(String[] args) throws InterruptedException {
182+
PrintOddAndEvenShu print = new PrintOddAndEvenShu();
183+
Thread t1 = new Thread(print::printOdd);
184+
Thread t2 = new Thread(print::printEven);
185+
t1.start();
186+
t2.start();
187+
t1.join();
188+
t2.join();
189+
}
190+
}
191+
```
192+
142193
欢迎光临[我的博客](http://www.wangtianyi.top/?utm_source=github&utm_medium=github),发现更多技术资源~

0 commit comments

Comments
 (0)