Java Multithreading: 18 flashcards
All 18 flashcards from this kit, free and with no sign-up.
1. What is a thread in Java?
Show the answer
A thread is a lightweight subprocess, the smallest unit of execution, that runs concurrently within a program.
2. What is multithreading in Java?
Show the answer
Multithreading is a process of executing multiple threads simultaneously to maximize CPU utilization and improve performance.
3. What are the two primary ways to create a thread in Java?
Show the answer
By extending the `Thread` class or implementing the `Runnable` interface.
4. Which method must be overridden when implementing `Runnable`?
Show the answer
The `run()` method, which contains the code that will be executed by the thread.
5. What is the purpose of the `start()` method?
Show the answer
It creates a new thread and begins execution of the `run()` method on that new thread.
6. What is the difference between `start()` and `run()`?
Show the answer
`start()` launches a new thread and then calls `run()`, while `run()` just executes the code in the current thread without creating a new one.
7. What is the `Thread` class's `sleep()` method used for?
Show the answer
It pauses the execution of the current thread for a specified number of milliseconds.
8. What exception must be handled when calling `Thread.sleep()`?
Show the answer
`InterruptedException`.
9. What is the `join()` method used for?
Show the answer
It waits for a thread to die (finish its execution) before the current thread continues.
10. What is the purpose of the `setPriority()` method?
Show the answer
It sets the priority of a thread, which influences the order in which threads are scheduled for execution.
11. What is the default priority of a thread in Java?
Show the answer
5, which is `Thread.NORM_PRIORITY`.
12. What is the `synchronized` keyword used for?
Show the answer
It ensures that only one thread can access a method or block at a time, preventing race conditions.
13. What is a race condition?
Show the answer
A situation where multiple threads access shared data concurrently, and the final outcome depends on the timing of their execution, leading to inconsistent results.
14. What is the `volatile` keyword used for?
Show the answer
It ensures that changes to a variable are always visible to other threads, preventing caching inconsistencies.
15. What is the `wait()`, `notify()`, and `notifyAll()` mechanism used for?
Show the answer
It is used for inter-thread communication, allowing threads to wait for a condition and be notified when it changes.
16. What is a deadlock in multithreading?
Show the answer
A situation where two or more threads are blocked forever, each waiting for a resource held by the other.
17. What is the `ExecutorService` framework used for?
Show the answer
It provides a higher-level API for managing and executing threads, such as thread pools, instead of manually creating threads.
18. What is a thread pool?
Show the answer
A collection of pre-initialized threads that can be reused to execute tasks, reducing the overhead of creating new threads.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages