File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Chapter_30/exercise_30_09 Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments