Skip to content

Commit 16b8716

Browse files
committed
🔖 Java 容器示例
1 parent ff34dae commit 16b8716

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

codes/container/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>io.github.dunwu</groupId>
8+
<artifactId>javase-notes</artifactId>
9+
<version>1.0.1</version>
10+
</parent>
11+
<artifactId>javase-container</artifactId>
12+
<packaging>jar</packaging>
13+
<name>${project.artifactId}</name>
14+
<description>Java容器示例</description>
15+
16+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.dunwu.javase.container.base;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* Java 容器 fail-fast 机制示例
9+
* @author Zhang Peng
10+
* @date 2018/6/29
11+
*/
12+
public class FailFastDemo {
13+
private static List<Integer> list = new ArrayList<>();
14+
15+
private static class ThreadOne extends Thread {
16+
public void run() {
17+
Iterator<Integer> iterator = list.iterator();
18+
while (iterator.hasNext()) {
19+
int i = iterator.next();
20+
System.out.println("ThreadOne 遍历:" + i);
21+
try {
22+
Thread.sleep(10);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}
28+
}
29+
30+
private static class ThreadTwo extends Thread {
31+
public void run() {
32+
int i = 0;
33+
while (i < 6) {
34+
System.out.println("ThreadTwo run:" + i);
35+
if (i == 3) {
36+
list.remove(i);
37+
}
38+
i++;
39+
}
40+
}
41+
}
42+
43+
public static void main(String[] args) {
44+
for (int i = 0; i < 100; i++) {
45+
list.add(i);
46+
}
47+
new ThreadOne().start();
48+
new ThreadTwo().start();
49+
}
50+
}

codes/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<module>effective</module>
2222
<module>jdk8</module>
2323
<module>jvm</module>
24+
<module>container</module>
2425
<module>concurrent</module>
2526
<module>util</module>
2627
<module>oop</module>

0 commit comments

Comments
 (0)