1+ package com .tns .tests ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class ConcurrentAccessTest {
6+
7+ public interface Callback {
8+ void invoke (ArrayList list1 , ArrayList list2 , ArrayList list3 , ArrayList list4 , ArrayList list5 ,
9+ ArrayList list6 , ArrayList list7 , ArrayList list8 , ArrayList list9 , ArrayList list10 );
10+ }
11+
12+ public interface ErrorCallback {
13+ void onError (Throwable error );
14+ }
15+
16+ /**
17+ * Calls the callback from a background thread multiple times.
18+ * @param callback The callback to invoke
19+ * @param times Number of times to call the callback (default 50)
20+ */
21+ public static void callFromBackgroundThread (final Callback callback , final int times ) {
22+ Thread thread = new Thread (new Runnable () {
23+ @ Override
24+ public void run () {
25+ for (int i = 0 ; i < times ; i ++) {
26+ invokeCallbackWithArrayLists (callback , i );
27+ }
28+ }
29+ });
30+ thread .start ();
31+ }
32+
33+ /**
34+ * Calls the callback synchronously from the current thread.
35+ * @param callback The callback to invoke
36+ * @param times Number of times to call the callback (default 50)
37+ */
38+ public static void callSynchronously (Callback callback , int times ) {
39+ for (int i = 0 ; i < times ; i ++) {
40+ invokeCallbackWithArrayLists (callback , i );
41+ }
42+ }
43+
44+ /**
45+ * Helper method that creates 10 ArrayLists and invokes the callback with them.
46+ * Each ArrayList contains some data based on the iteration number.
47+ */
48+ private static void invokeCallbackWithArrayLists (Callback callback , int iteration ) {
49+ ArrayList <Integer > list1 = new ArrayList <>();
50+ ArrayList <Integer > list2 = new ArrayList <>();
51+ ArrayList <Integer > list3 = new ArrayList <>();
52+ ArrayList <Integer > list4 = new ArrayList <>();
53+ ArrayList <Integer > list5 = new ArrayList <>();
54+ ArrayList <Integer > list6 = new ArrayList <>();
55+ ArrayList <Integer > list7 = new ArrayList <>();
56+ ArrayList <Integer > list8 = new ArrayList <>();
57+ ArrayList <Integer > list9 = new ArrayList <>();
58+ ArrayList <Integer > list10 = new ArrayList <>();
59+
60+ // Add some data to each list
61+ for (int i = 0 ; i < 5 ; i ++) {
62+ list1 .add (iteration * 10 + i );
63+ list2 .add (iteration * 10 + i + 1 );
64+ list3 .add (iteration * 10 + i + 2 );
65+ list4 .add (iteration * 10 + i + 3 );
66+ list5 .add (iteration * 10 + i + 4 );
67+ list6 .add (iteration * 10 + i + 5 );
68+ list7 .add (iteration * 10 + i + 6 );
69+ list8 .add (iteration * 10 + i + 7 );
70+ list9 .add (iteration * 10 + i + 8 );
71+ list10 .add (iteration * 10 + i + 9 );
72+ }
73+
74+ callback .invoke (list1 , list2 , list3 , list4 , list5 , list6 , list7 , list8 , list9 , list10 );
75+ }
76+ }
0 commit comments