Last Updated: December 6, 2025
32 quizzes
Start a new thread that executes the given Runnable task
Thread worker = new Thread(task); worker.();Click an option to fill the blank:
Ensure the current thread waits until another thread has finished before continuing
worker.();Click an option to fill the blank:
Mark a shared boolean so that updates are immediately visible to other threads
private boolean running = true;Click an option to fill the blank:
Which statement best describes a Java thread?
When implementing Runnable, where do you put the code that should run in the new thread?
Which state is a thread in immediately after new Thread(runnable) is created but before start() is called?
Which method should you use to pause the current thread for 100 milliseconds?
What is the default priority for a newly created Java thread?
Why is synchronization needed when multiple threads update a shared counter?
What happens if two threads synchronize on different lock objects when accessing the same field?
Which keyword ensures visibility of writes to a field across threads without using explicit locking?
Which situation most likely indicates a deadlock?
Which ExecutorService method stops accepting new tasks but lets submitted tasks finish?
What is a key advantage of using Callable over Runnable?
Which statement about CompletableFuture is true?
Order the steps to safely update a shared counter using ReentrantLock
Drag and drop to reorder, or use the arrows.
Order the steps to use a CountDownLatch so a main thread waits for workers to finish
Drag and drop to reorder, or use the arrows.
What is the output of this code involving thread state?
1public class StateDemo {
2 public static void main(String[] args) throws Exception {
3 Thread t = new Thread(() -> {});
4 System.out.println(t.getState());
5 }
6}What is the output regarding thread priority?
1public class PriorityDemo {
2 public static void main(String[] args) {
3 Thread t = new Thread(() -> {});
4 System.out.println(t.getPriority());
5 }
6}What is the single line printed by this synchronized method example?
1public class SyncDemo {
2 private int value = 10;
3 public synchronized int getValue() { return value; }
4 public static void main(String[] args) {
5 SyncDemo d = new SyncDemo();
6 System.out.println(d.getValue());
7 }
8}What does this ExecutorService example print?
1import java.util.concurrent.*;
2public class ExecDemo {
3 public static void main(String[] args) throws Exception {
4 ExecutorService ex = Executors.newSingleThreadExecutor();
5 Future<Integer> f = ex.submit(() -> 7 * 3);
6 System.out.println(f.get());
7 ex.shutdown();
8 }
9}What single line does this CompletableFuture chain print?
1import java.util.concurrent.*;
2public class CFUDemo {
3 public static void main(String[] args) throws Exception {
4 CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> "Hi")
5 .thenApply(s -> s + " there");
6 System.out.println(f.get());
7 }
8}Find the concurrency bug in this shared counter code
Click on the line(s) that contain the bug.
public class Counter { private int value = 0; public void increment() { value = value + 1; } public int get() { return value; }}Find the bug in this code that uses wait/notify for thread communication
Click on the line(s) that contain the bug.
public class QueueDemo { private final Object lock = new Object(); private boolean ready = false; public void awaitReady() throws InterruptedException { if (!ready) { lock.wait(); } } public void signalReady() { synchronized (lock) { ready = true; lock.notifyAll(); } }}Match each concurrency utility to its primary purpose
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each lock-related concept with its description
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to create a fixed-size thread pool and submit a task
import java.util.concurrent.*;public class PoolExample { public static void main(String[] args) { ExecutorService executor = Executors.(4); executor.(() -> System.out.println("Task")); executor.shutdown(); }}Click an option to fill blank 1:
Complete the code so multiple readers can access data while writers are exclusive
import java.util.concurrent.locks.*;public class Stats { private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private int value; public int read() { lock.().lock(); try { return value; } finally { lock.().unlock(); } } public void write(int v) { lock.().lock(); try { value = v; } finally { lock.().unlock(); } }}Click an option to fill blank 1:
Complete the atomic counter code to increment safely without synchronization
import java.util.concurrent.atomic.*;public class AtomicCounter { private final AtomicInteger counter = new AtomicInteger(0); public void inc() { counter.(); } public int get() { return counter.(); }}Click an option to fill blank 1:
Complete the code to create and start a virtual thread that runs a task
public class VirtualDemo { public static void main(String[] args) { Thread vt = Thread.().(() -> System.out.println("virtual")); }}Click an option to fill blank 1:
Click the line where a deadlock risk is introduced due to inconsistent lock ordering
Click on the line to select.
public class DeadlockRisk { private final Object lockA = new Object(); private final Object lockB = new Object(); public void method1() { synchronized (lockA) { synchronized (lockB) { System.out.println("m1"); } } } public void method2() { synchronized (lockB) { synchronized (lockA) { System.out.println("m2"); } } }}Click the line that can cause threads to spin without proper blocking in this busy-wait example
Click on the line to select.
public class VolatileFlag { private volatile boolean running = true; public void runLoop() { while (running) { // work } } public void stop() { running = false; }}