-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz-java-concurrent.html
300 lines (268 loc) · 12 KB
/
quiz-java-concurrent.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interview questions - Java (concurrent)</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="styles/shortcuts.css" rel="stylesheet">
<link href="styles/quiz.css" rel="stylesheet">
</head>
<body class="container">
<h1>Java interview questions - concurrent</h1>
<my-quiz>
<my-pair>
<my-question>Explain the difference between <code>synchronized</code> keyword and <code>ReentrantLock</code>
in Java concurrent programming.
<pre>
// Compare synchronization mechanisms
public class LockComparison {
// Synchronized method
public synchronized void synchronizedMethod() {}
// ReentrantLock usage
private final Lock lock = new ReentrantLock();
public void reentrantLockMethod() {
lock.lock();
try {
// Critical section
} finally {
lock.unlock();
}
}
}</pre>
</my-question>
<my-answer>Key differences include:
• <code>synchronized</code> is a keyword, while <code>ReentrantLock</code> is a class
• <code>ReentrantLock</code> offers more flexibility with methods like <code>tryLock()</code>,
<code>lockInterruptibly()</code>
• <code>ReentrantLock</code> allows fair locking policies
• <code>ReentrantLock</code> requires explicit locking and unlocking
• <code>synchronized</code> is more concise but less feature-rich
</my-answer>
</my-pair>
<my-pair>
<my-question>How does the <code>java.util.concurrent.atomic</code> package help in thread-safe operations?
<pre>
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicExample {
private AtomicInteger counter = new AtomicInteger(0);
public void increment() {
counter.incrementAndGet();
}
}</pre>
</my-question>
<my-answer>The <code>atomic</code> package provides lock-free thread-safe primitive types:
• Supports atomic operations without explicit synchronization
• Guarantees thread-safety for operations like increment, compare-and-set
• Uses low-level CPU instructions for efficient synchronization
• Prevents race conditions in multi-threaded environments
• Includes classes like <code>AtomicInteger</code>, <code>AtomicReference</code>,
<code>AtomicLong</code>
</my-answer>
</my-pair>
<my-pair>
<my-question>Describe the thread lifecycle states in Java and explain thread transitions.
<pre>
public class ThreadLifecycleDemo {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
// Thread work
});
thread.start(); // Triggers state transitions
}
}</pre>
</my-question>
<my-answer>Java thread lifecycle states:
• New: Thread created but not started
• Runnable: Ready to run, waiting for CPU allocation
• Running: Currently executing
• Blocked: Waiting to acquire a lock
• Waiting: Waiting indefinitely for another thread
• Timed Waiting: Waiting with a specified timeout
• Terminated: Execution completed
</my-answer>
</my-pair>
<my-pair>
<my-question>Implement a thread-safe singleton using double-checked locking.
<pre>
public class ThreadSafeSingleton {
private static volatile ThreadSafeSingleton instance;
private ThreadSafeSingleton() {}
public static ThreadSafeSingleton getInstance() {
if (instance == null) {
synchronized(ThreadSafeSingleton.class) {
if (instance == null) {
instance = new ThreadSafeSingleton();
}
}
}
return instance;
}
}</pre>
</my-question>
<my-answer>Double-checked locking ensures:
• Lazy initialization of singleton
• <code>volatile</code> keyword prevents instruction reordering
• First <code>null</code> check reduces synchronization overhead
• Inner synchronized block ensures thread-safety
• Only one instance is created across multiple threads
</my-answer>
</my-pair>
<my-pair>
<my-question>Compare <code>Executor</code>, <code>ExecutorService</code>, and
<code>ThreadPoolExecutor</code> in Java concurrent programming.
<pre>
import java.util.concurrent.*;
public class ExecutorDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> System.out.println("Task"));
}
}</pre>
</my-question>
<my-answer>Comparison of concurrent execution interfaces:
• <code>Executor</code>: Basic interface for executing tasks
• <code>ExecutorService</code>: Extends <code>Executor</code> with lifecycle management
• <code>ThreadPoolExecutor</code>: Concrete implementation of thread pool
• Provides thread reuse and management
• Controls thread creation, queuing, and execution strategies
</my-answer>
</my-pair>
<my-pair>
<my-question>Explain the <code>wait()</code> and <code>notify()</code> methods in inter-thread
communication.
<pre>
public class ProducerConsumerExample {
private synchronized void produce() throws InterruptedException {
while (condition) {
wait(); // Releases lock and waits
}
// Produce item
notify(); // Wakes up waiting thread
}
}</pre>
</my-question>
<my-answer>Inter-thread communication methods:
• <code>wait()</code>: Causes current thread to release lock and wait
• <code>notify()</code>: Wakes up a single waiting thread
• <code>notifyAll()</code>: Wakes up all waiting threads
• Must be called from synchronized context
• Used for complex thread synchronization scenarios
</my-answer>
</my-pair>
<my-pair>
<my-question>Describe the <code>ConcurrentHashMap</code> and its thread-safety mechanisms.
<pre>
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentMapDemo {
private ConcurrentHashMap<String, Integer> map =
new ConcurrentHashMap<>();
public void updateMap() {
map.put("key", 42);
}
}</pre>
</my-question>
<my-answer>Thread-safe map implementation:
• Allows concurrent read and write operations
• Uses segment locking instead of entire map locking
• Provides better performance than <code>Hashtable</code>
• Supports atomic operations like <code>putIfAbsent()</code>
• Prevents <code>ConcurrentModificationException</code>
</my-answer>
</my-pair>
<my-pair>
<my-question>What are the key differences between <code>CountDownLatch</code> and
<code>CyclicBarrier</code>?
<pre>
import java.util.concurrent.*;
public class SynchronizationExample {
public void usingCountDownLatch() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(3);
// Threads count down
latch.await(); // Wait for count to reach zero
}
}</pre>
</my-question>
<my-answer>Synchronization primitive differences:
• <code>CountDownLatch</code>: One-time synchronization
• <code>CyclicBarrier</code>: Reusable synchronization point
• <code>CountDownLatch</code> counts down, <code>CyclicBarrier</code> waits for all threads
• <code>CountDownLatch</code> cannot be reset
• <code>CyclicBarrier</code> can be reused after barrier is broken
</my-answer>
</my-pair>
<my-pair>
<my-question>Implement a producer-consumer solution using <code>BlockingQueue</code>.
<pre>
import java.util.concurrent.*;
public class ProducerConsumerBlockingQueue {
private BlockingQueue<Integer> queue =
new LinkedBlockingQueue<>(10);
public void produce() throws InterruptedException {
queue.put(1); // Blocks if queue is full
}
public void consume() throws InterruptedException {
Integer item = queue.take(); // Blocks if queue is empty
}
}</pre>
</my-question>
<my-answer>Benefits of <code>BlockingQueue</code>:
• Thread-safe data structure
• Automatic blocking on <code>put()</code> and <code>take()</code>
• Eliminates need for explicit synchronization
• Supports multiple implementation types
• Prevents busy-waiting and reduces complexity
</my-answer>
</my-pair>
<my-pair>
<my-question>Explain the purpose and usage of <code>Semaphore</code> in concurrent programming.
<pre>
import java.util.concurrent.*;
public class SemaphoreExample {
private Semaphore semaphore = new Semaphore(3);
public void accessResource() throws InterruptedException {
semaphore.acquire(); // Limit concurrent access
try {
// Access limited resource
} finally {
semaphore.release();
}
}
}</pre>
</my-question>
<my-answer>Semaphore characteristics:
• Controls access to a set of resources
• Limits number of concurrent threads
• Can be fair or non-fair
• Prevents resource overutilization
• Useful for connection pools, resource management
</my-answer>
</my-pair>
<my-pair>
<my-question>What are memory barriers and how do they prevent instruction reordering?
<pre>
public class MemoryBarrierExample {
private volatile int value;
public void updateValue() {
// Volatile ensures memory visibility
value = 42;
}
}</pre>
</my-question>
<my-answer>Memory barrier mechanisms:
• Prevent compiler and CPU instruction reordering
• Ensure visibility of changes across threads
• <code>volatile</code> keyword creates memory barriers
• Guarantees happens-before relationship
• Critical for managing shared memory in multi-threaded environments
</my-answer>
</my-pair>
</my-quiz>
</body>
<!-- Bootstrap JS and dependencies -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="scripts/shortcuts.js"></script>
<script src="scripts/quiz.js"></script>
</html>