Skip to content

Commit a5a79bf

Browse files
committed
commit Exercise_30_09
1 parent b12b886 commit a5a79bf

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package exercise_30_09;
2+
3+
import java.util.HashSet;
4+
import java.util.Iterator;
5+
6+
public class Exercise_30_09 {
7+
private static HashSet<Integer> set = new HashSet<>();
8+
9+
public static void main(String[] args) {
10+
Thread thread1 = new Thread(new AddToSetTask());
11+
Thread thread2 = new Thread(new TraverseSetTask());
12+
thread1.start();
13+
thread2.start();
14+
}
15+
16+
public static class AddToSetTask implements Runnable {
17+
18+
@Override
19+
public void run() {
20+
for(int i = 0; i < 1000000; i++) {
21+
set.add(i);
22+
}
23+
try {
24+
Thread.sleep(1000);
25+
}
26+
catch (InterruptedException ex) {
27+
}
28+
}
29+
}
30+
31+
public static class TraverseSetTask implements Runnable {
32+
33+
@Override
34+
public void run() {
35+
try {
36+
while(true) {
37+
Iterator<Integer> iterator = set.iterator();
38+
while(iterator.hasNext()) {
39+
System.out.println(iterator.next());
40+
}
41+
Thread.sleep(1000);
42+
}
43+
}
44+
catch (InterruptedException ex) {
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)