Java Threads and Concurrency Flashcards
6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Java Threads and Concurrency flashcards as text
Which of the following is a correct way to create a new thread in Java?
Answer: Extend Thread class or implement Runnable interface
In Java, threads can be created by extending `Thread` or implementing `Runnable` and passing it to a `Thread` constructor.
What does the `synchronized` keyword ensure in Java?
Answer: That only one thread executes the synchronized block/method at a time
`synchronized` acquires a monitor lock on an object, ensuring mutual exclusion so only one thread can execute the guarded code at a time.
What is a deadlock in Java threading?
Answer: Two or more threads permanently blocked waiting for each other's locks
Deadlock occurs when two or more threads each hold a lock the other needs, causing them to wait indefinitely.
What is the difference between `Thread.sleep()` and `Object.wait()` in Java?
Answer: wait() releases the object's lock; sleep() does not release any lock
`wait()` releases the object's monitor lock and suspends the thread until notified, while `sleep()` pauses the thread without releasing any locks.
Which thread state indicates that a thread is eligible to run but waiting for CPU time?
Answer: RUNNABLE
A thread in the `RUNNABLE` state is ready to execute and waiting for the CPU scheduler to assign it processor time.
What method must be overridden when implementing the `Runnable` interface?
Answer: run()
The `Runnable` interface has a single abstract method `run()`, which contains the code the thread will execute.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy