11import java .util .*;
22
33class Main {
4- static Deque <Integer > stack ;
5- static ArrayQueue <Integer > concurrentStack ;
6- static List <Integer >[] poppedValues ;
4+ static Deque <Integer > queue ;
5+ static ArrayQueue <Integer > concurrentQueue ;
6+ static List <Integer >[] deqValues ;
77 static int TH = 10 , NUM = 1000 ;
88
9- // Each unsafe thread pushes N numbers and pops N, adding
10- // them to its own poppedValues for checking; using Java's
11- // sequential stack implementation, ArrayDeque.
9+ // Each unsafe thread enqs N numbers and deqs N, adding
10+ // them to its own deqValues for checking; using Java's
11+ // sequential queue implementation, ArrayDeque.
1212 static Thread unsafe (int id , int x , int N ) {
1313 return new Thread (() -> {
14- String action = "push " ;
14+ String action = "enq " ;
1515 try {
1616 for (int i =0 , y =x ; i <N ; i ++)
17- stack . push (y ++);
17+ queue . addLast (y ++);
1818 Thread .sleep (1000 );
19- action = "pop " ;
19+ action = "deq " ;
2020 for (int i =0 ; i <N ; i ++)
21- poppedValues [id ].add (stack . pop ());
21+ deqValues [id ].add (queue . removeFirst ());
2222 }
2323 catch (Exception e ) { log (id +": failed " +action ); }
2424 });
2525 }
2626
27- // Each safe thread pushes N numbers and pops N, adding
28- // them to its own poppedValues for checking; using
29- // ArrayStack .
27+ // Each safe thread enqs N numbers and deqs N, adding
28+ // them to its own deqValues for checking; using
29+ // ArrayQueue .
3030 static Thread safe (int id , int x , int N ) {
3131 return new Thread (() -> {
32- String action = "push " ;
32+ String action = "enq " ;
3333 try {
3434 for (int i =0 , y =x ; i <N ; i ++)
35- concurrentStack . push (y ++);
35+ concurrentQueue . enq (y ++);
3636 Thread .sleep (1000 );
37- action = "pop " ;
37+ action = "deq " ;
3838 for (int i =0 ; i <N ; i ++)
39- poppedValues [id ].add (concurrentStack . pop ());
39+ deqValues [id ].add (concurrentQueue . deq ());
4040 }
4141 catch (Exception e ) { log (id +": failed " +action );
4242 e .printStackTrace (); }
4343 });
4444 }
4545
46- // Checks if each thread popped N values, and they are
46+ // Checks if each thread dequeued N values, and they are
4747 // globally unique.
4848 static boolean wasLIFO (int N ) {
4949 Set <Integer > set = new HashSet <>();
5050 boolean passed = true ;
5151 for (int i =0 ; i <TH ; i ++) {
52- int n = poppedValues [i ].size ();
52+ int n = deqValues [i ].size ();
5353 if (n != N ) {
54- log (i +": popped " +n +"/" +N +" values" );
54+ log (i +": dequeued " +n +"/" +N +" values" );
5555 passed = false ;
5656 }
57- for (Integer x : poppedValues [i ])
57+ for (Integer x : deqValues [i ])
5858 if (set .contains (x )) {
5959 log (i +": has duplicate value " +x );
6060 passed = false ;
6161 }
62- set .addAll (poppedValues [i ]);
62+ set .addAll (deqValues [i ]);
6363 }
6464 return passed ;
6565 }
6666
6767 @ SuppressWarnings ("unchecked" )
6868 static void testThreads (boolean safe ) {
69- stack = new ArrayDeque <>();
70- concurrentStack = new ArrayQueue <>(TH *NUM );
71- poppedValues = new List [TH ];
69+ queue = new ArrayDeque <>();
70+ concurrentQueue = new ArrayQueue <>(TH *NUM );
71+ deqValues = new List [TH ];
7272 for (int i =0 ; i <TH ; i ++)
73- poppedValues [i ] = new ArrayList <>();
73+ deqValues [i ] = new ArrayList <>();
7474 Thread [] threads = new Thread [TH ];
7575 for (int i =0 ; i <TH ; i ++) {
7676 threads [i ] = safe ?
@@ -86,11 +86,11 @@ static void testThreads(boolean safe) {
8686 }
8787
8888 public static void main (String [] args ) {
89- log ("Starting " +TH +" threads with sequential stack " );
89+ log ("Starting " +TH +" threads with sequential queue " );
9090 testThreads (false );
9191 log ("Was LIFO? " +wasLIFO (NUM ));
9292 log ("" );
93- log ("Starting " +TH +" threads with array stack " );
93+ log ("Starting " +TH +" threads with array queue " );
9494 testThreads (true );
9595 log ("Was LIFO? " +wasLIFO (NUM ));
9696 log ("" );
0 commit comments