|
| 1 | +public class Test { |
| 2 | + public static void main(String[] args) { |
| 3 | + System.out.println("=== Concurrent Collections Tests ==="); |
| 4 | + |
| 5 | + testThreadSafeVsUnsafeCollections(); |
| 6 | + testSynchronizedCollections(); |
| 7 | + testVectorOperations(); |
| 8 | + testHashtableOperations(); |
| 9 | + testStringBufferVsStringBuilder(); |
| 10 | + testCustomThreadSafeCollection(); |
| 11 | + |
| 12 | + System.out.println("Concurrent collections tests completed"); |
| 13 | + } |
| 14 | + |
| 15 | + private static void testThreadSafeVsUnsafeCollections() { |
| 16 | + System.out.println("Test 1: Thread-safe vs unsafe collections"); |
| 17 | + |
| 18 | + // Unsafe ArrayList test |
| 19 | + java.util.List<Integer> unsafeList = new java.util.ArrayList<>(); |
| 20 | + Thread[] unsafeWriters = new Thread[3]; |
| 21 | + |
| 22 | + for (int i = 0; i < 3; i++) { |
| 23 | + final int threadId = i; |
| 24 | + unsafeWriters[i] = new Thread(() -> { |
| 25 | + for (int j = 0; j < 100; j++) { |
| 26 | + unsafeList.add(threadId * 100 + j); |
| 27 | + } |
| 28 | + System.out.println("UnsafeWriter" + threadId + " completed"); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + for (Thread writer : unsafeWriters) { |
| 33 | + writer.start(); |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + for (Thread writer : unsafeWriters) { |
| 38 | + writer.join(); |
| 39 | + } |
| 40 | + } catch (InterruptedException e) { |
| 41 | + System.out.println("Unsafe writers interrupted"); |
| 42 | + } |
| 43 | + |
| 44 | + System.out.println("Unsafe list final size: " + unsafeList.size() + " (expected: 300)"); |
| 45 | + } |
| 46 | + |
| 47 | + private static void testSynchronizedCollections() { |
| 48 | + System.out.println("Test 2: Synchronized collections"); |
| 49 | + java.util.List<Integer> syncList = java.util.Collections.synchronizedList(new java.util.ArrayList<>()); |
| 50 | + Thread[] syncWriters = new Thread[3]; |
| 51 | + |
| 52 | + for (int i = 0; i < 3; i++) { |
| 53 | + final int threadId = i; |
| 54 | + syncWriters[i] = new Thread(() -> { |
| 55 | + for (int j = 0; j < 100; j++) { |
| 56 | + syncList.add(threadId * 100 + j); |
| 57 | + } |
| 58 | + System.out.println("SyncWriter" + threadId + " completed"); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + for (Thread writer : syncWriters) { |
| 63 | + writer.start(); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + for (Thread writer : syncWriters) { |
| 68 | + writer.join(); |
| 69 | + } |
| 70 | + } catch (InterruptedException e) { |
| 71 | + System.out.println("Sync writers interrupted"); |
| 72 | + } |
| 73 | + |
| 74 | + System.out.println("Sync list final size: " + syncList.size()); |
| 75 | + } |
| 76 | + |
| 77 | + private static void testVectorOperations() { |
| 78 | + System.out.println("Test 3: Vector operations"); |
| 79 | + java.util.Vector<String> vector = new java.util.Vector<>(); |
| 80 | + Thread[] vectorThreads = new Thread[2]; |
| 81 | + |
| 82 | + vectorThreads[0] = new Thread(() -> { |
| 83 | + for (int i = 0; i < 50; i++) { |
| 84 | + vector.add("Writer: " + i); |
| 85 | + if (i % 10 == 0) { |
| 86 | + System.out.println("VectorWriter: Added " + (i + 1) + " elements"); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + vectorThreads[1] = new Thread(() -> { |
| 92 | + try { |
| 93 | + Thread.sleep(100); // Let writer add some elements first |
| 94 | + } catch (InterruptedException e) { |
| 95 | + System.out.println("VectorReader interrupted during sleep"); |
| 96 | + } |
| 97 | + |
| 98 | + for (int i = 0; i < 10; i++) { |
| 99 | + if (!vector.isEmpty()) { |
| 100 | + String element = vector.get(0); |
| 101 | + System.out.println("VectorReader: Read " + element); |
| 102 | + } |
| 103 | + try { |
| 104 | + Thread.sleep(20); |
| 105 | + } catch (InterruptedException e) { |
| 106 | + System.out.println("VectorReader interrupted"); |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + vectorThreads[0].start(); |
| 113 | + vectorThreads[1].start(); |
| 114 | + |
| 115 | + try { |
| 116 | + for (Thread thread : vectorThreads) { |
| 117 | + thread.join(); |
| 118 | + } |
| 119 | + } catch (InterruptedException e) { |
| 120 | + System.out.println("Vector test interrupted"); |
| 121 | + } |
| 122 | + |
| 123 | + System.out.println("Vector final size: " + vector.size()); |
| 124 | + } |
| 125 | + |
| 126 | + private static void testHashtableOperations() { |
| 127 | + System.out.println("Test 4: Hashtable operations"); |
| 128 | + java.util.Hashtable<String, Integer> hashtable = new java.util.Hashtable<>(); |
| 129 | + Thread[] hashtableThreads = new Thread[3]; |
| 130 | + |
| 131 | + for (int i = 0; i < 3; i++) { |
| 132 | + final int threadId = i; |
| 133 | + hashtableThreads[i] = new Thread(() -> { |
| 134 | + for (int j = 0; j < 20; j++) { |
| 135 | + String key = "Thread" + threadId + "_Key" + j; |
| 136 | + hashtable.put(key, threadId * 100 + j); |
| 137 | + } |
| 138 | + System.out.println("HashtableThread" + threadId + " completed"); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + for (Thread thread : hashtableThreads) { |
| 143 | + thread.start(); |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + for (Thread thread : hashtableThreads) { |
| 148 | + thread.join(); |
| 149 | + } |
| 150 | + } catch (InterruptedException e) { |
| 151 | + System.out.println("Hashtable test interrupted"); |
| 152 | + } |
| 153 | + |
| 154 | + System.out.println("Hashtable final size: " + hashtable.size()); |
| 155 | + } |
| 156 | + |
| 157 | + private static void testStringBufferVsStringBuilder() { |
| 158 | + System.out.println("Test 5: StringBuffer vs StringBuilder"); |
| 159 | + StringBuffer stringBuffer = new StringBuffer(); |
| 160 | + StringBuilder stringBuilder = new StringBuilder(); |
| 161 | + |
| 162 | + Thread[] bufferThreads = new Thread[2]; |
| 163 | + bufferThreads[0] = new Thread(() -> { |
| 164 | + for (int i = 0; i < 1000; i++) { |
| 165 | + stringBuffer.append("A"); |
| 166 | + } |
| 167 | + System.out.println("StringBuffer thread completed"); |
| 168 | + }); |
| 169 | + |
| 170 | + bufferThreads[1] = new Thread(() -> { |
| 171 | + for (int i = 0; i < 1000; i++) { |
| 172 | + stringBuffer.append("B"); |
| 173 | + } |
| 174 | + System.out.println("StringBuffer thread 2 completed"); |
| 175 | + }); |
| 176 | + |
| 177 | + Thread[] builderThreads = new Thread[2]; |
| 178 | + builderThreads[0] = new Thread(() -> { |
| 179 | + for (int i = 0; i < 1000; i++) { |
| 180 | + stringBuilder.append("X"); |
| 181 | + } |
| 182 | + System.out.println("StringBuilder thread completed"); |
| 183 | + }); |
| 184 | + |
| 185 | + builderThreads[1] = new Thread(() -> { |
| 186 | + for (int i = 0; i < 1000; i++) { |
| 187 | + stringBuilder.append("Y"); |
| 188 | + } |
| 189 | + System.out.println("StringBuilder thread 2 completed"); |
| 190 | + }); |
| 191 | + |
| 192 | + // Start buffer threads |
| 193 | + for (Thread thread : bufferThreads) { |
| 194 | + thread.start(); |
| 195 | + } |
| 196 | + |
| 197 | + // Start builder threads |
| 198 | + for (Thread thread : builderThreads) { |
| 199 | + thread.start(); |
| 200 | + } |
| 201 | + |
| 202 | + try { |
| 203 | + for (Thread thread : bufferThreads) { |
| 204 | + thread.join(); |
| 205 | + } |
| 206 | + for (Thread thread : builderThreads) { |
| 207 | + thread.join(); |
| 208 | + } |
| 209 | + } catch (InterruptedException e) { |
| 210 | + System.out.println("String buffer/builder test interrupted"); |
| 211 | + } |
| 212 | + |
| 213 | + System.out.println("StringBuffer final length: " + stringBuffer.length() + " (expected: 2000)"); |
| 214 | + System.out.println("StringBuilder final length: " + stringBuilder.length() + " (may vary due to race conditions)"); |
| 215 | + } |
| 216 | + |
| 217 | + private static void testCustomThreadSafeCollection() { |
| 218 | + System.out.println("Test 6: Custom thread-safe collection"); |
| 219 | + ThreadSafeCounter counter = new ThreadSafeCounter(); |
| 220 | + Thread[] counterThreads = new Thread[5]; |
| 221 | + |
| 222 | + for (int i = 0; i < 5; i++) { |
| 223 | + final int threadId = i; |
| 224 | + counterThreads[i] = new Thread(() -> { |
| 225 | + for (int j = 0; j < 200; j++) { |
| 226 | + counter.increment(); |
| 227 | + } |
| 228 | + System.out.println("CounterThread" + threadId + " completed, current count: " + counter.getValue()); |
| 229 | + }); |
| 230 | + } |
| 231 | + |
| 232 | + for (Thread thread : counterThreads) { |
| 233 | + thread.start(); |
| 234 | + } |
| 235 | + |
| 236 | + try { |
| 237 | + for (Thread thread : counterThreads) { |
| 238 | + thread.join(); |
| 239 | + } |
| 240 | + } catch (InterruptedException e) { |
| 241 | + System.out.println("Counter test interrupted"); |
| 242 | + } |
| 243 | + |
| 244 | + System.out.println("Final counter value: " + counter.getValue() + " (expected: 1000)"); |
| 245 | + } |
| 246 | + |
| 247 | + static class ThreadSafeCounter { |
| 248 | + private int count = 0; |
| 249 | + private final Object lock = new Object(); |
| 250 | + |
| 251 | + public void increment() { |
| 252 | + synchronized (lock) { |
| 253 | + count++; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + public void decrement() { |
| 258 | + synchronized (lock) { |
| 259 | + count--; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + public int getValue() { |
| 264 | + synchronized (lock) { |
| 265 | + return count; |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | +} |
0 commit comments