Machine-translated by Google. Questions are authored in English.
Top

Threads - Pointing out the correct statements Questions and Answers

Answer: A

An unlock on a monitor is ordered before every subsequent lock of the same monitor, so everything one thread wrote inside a synchronized block is visible to the next thread that enters a block on the same object. Mutual exclusion and visibility come together, and both depend on it being the same monitor.

Please log in or register to report this question.

Answer: B

Only the documented blocking calls, wait, join and sleep, respond to an interrupt by throwing. A thread parked inside a plain stream read simply has its interrupt status set and stays blocked until data arrives or the stream is closed. Closing it from another thread is the usual way to break out.

Please log in or register to report this question.

Answer: C

countDown() only ever decreases the count, and once it reaches zero every await() returns and continues to return immediately thereafter. There is no way back, which is what distinguishes it from a CyclicBarrier. countDown itself never blocks.

Please log in or register to report this question.

Answer: D

Each thread holds one lock and waits for the other, so neither can proceed and neither will release what it has. Monitors are released when a synchronized region exits, which never happens here. The standard remedy is to impose one global order in which locks are always acquired.

Please log in or register to report this question.

Answer: A

wait unlocks only the object it was called on; any other monitor the thread holds stays locked while it waits, possibly for a long time. That is a common route into deadlock, and the reason to keep a wait loop free of unrelated locks.

Please log in or register to report this question.

Answer: B

In starvation the system as a whole progresses but one thread keeps losing out, typically because greedy neighbours hold a shared lock most of the time. That is different from deadlock, where nothing progresses, and from livelock, where threads react to each other and stay busy achieving nothing.

Please log in or register to report this question.

Answer: C

Livelocked threads are not blocked at all: each keeps responding to the other's action, so both stay runnable and busy while the work never advances. Because nothing is blocked, a thread dump looks healthy, which makes it harder to spot than deadlock.

Please log in or register to report this question.

Answer: D

The modifier describes the variable holding the reference, so replacing the whole array is visible while writing one slot is not. Element-level visibility needs a lock or java.util.concurrent.atomic.AtomicIntegerArray and its relatives. This is a frequent and quiet source of stale reads.

Please log in or register to report this question.

Answer: A

An immutable object has no state to change, so there is no interleaving to protect against, and the memory model guarantees that correctly constructed final fields are visible to any thread that sees the reference. The caveat is construction: the object must not let this escape before it is finished.

Please log in or register to report this question.

Answer: B

Each thread has its own stack, so a local primitive is inherently confined and needs no protection. What can be shared is an object a local variable points at, if the reference is also stored somewhere reachable by another thread, so it is the object that needs guarding, not the variable.

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Threads - Pointing out the correct statements Questions and Answers || Threads - Pointing out the correct statements MCQ Questions and Answers || Threads - Pointing out the correct statements GK Questions and Answers || Threads - Pointing out the correct statements GK MCQ Questions || Threads - Pointing out the correct statements Multiple Choice Questions and Answers || Threads - Pointing out the correct statements GK || GK on Threads - Pointing out the correct statements || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming