Advanced Java Concurrency: Mastering Thread Pools & Executors
An advanced quiz on Java's concurrency mechanisms focusing on thread pools and executors.
Questions in this quiz
10 questions on Advanced Java Concurrency: Mastering Thread Pools & Executors. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary advantage of using a CachedThreadPool in Java?
- AIt creates a fixed number of threads.
- BIt reuses previously constructed threads.
- CIt schedules tasks at a fixed rate.
- DIt limits the number of concurrent threads.
2.In Java's Executor framework, which method is used to submit a task for execution?
- Aexecute()
- Bsubmit()
- Cinvoke()
- Drun()
3.How does a ScheduledThreadPoolExecutor differ from a FixedThreadPool?
- AIt has a fixed number of threads.
- BIt can schedule commands to run after a delay.
- CIt uses a single thread for execution.
- DIt does not support task scheduling.
4.Which of the following is true about the ForkJoinPool in Java?
- AIt uses a work-stealing algorithm.
- BIt can only execute Runnable tasks.
- CIt does not support parallelism.
- DIt is optimized for I/O-bound tasks.
5.What happens if a task submitted to an ExecutorService throws an exception?
- AThe exception is ignored.
- BThe exception is caught and stored in the Future.
- CThe executor shuts down immediately.
- DThe task is retried automatically.
6.Why is it generally better to use Executors.newFixedThreadPool() instead of manually creating threads?
- AIt manages thread lifecycle and resource allocation.
- BIt prevents deadlocks.
- CIt allows for dynamic thread creation.
- DIt automatically handles thread synchronization.
7.Which method in ExecutorService is used to attempt to stop all actively executing tasks?
- AshutdownNow()
- Bshutdown()
- Cterminate()
- Dstop()
8.What is the role of the Callable interface in Java concurrency?
- AIt manages thread pools.
- BIt defines a task that returns a result.
- CIt is used to create threads.
- DIt schedules tasks for execution.
9.In a ForkJoinPool, what is the purpose of the invokeAll() method?
- AIt executes tasks sequentially.
- BIt cancels all running tasks.
- CIt waits for all tasks to complete.
- DIt submits tasks for execution.
10.What is the primary use of the Executors.newSingleThreadExecutor() method?
- ATo schedule tasks at fixed intervals.
- BTo execute tasks in parallel.
- CTo create a pool with multiple threads.
- DTo ensure tasks are executed sequentially.