...
The signal()
method has better performance than signalAll()
when used securely.
Noncompliant Code Example (notify()
)
This noncompliant code example shows a complex multistep process being undertaken by several threads. Each thread executes the step identified by the time field. Each thread waits for the time
field to indicate that it is time to perform the corresponding thread's step. After performing the step, each thread increments time
and then notifies the thread that is responsible for the next step.
...
This noncompliant code example violates the liveness property. Each thread has a different condition predicate because each requires step
to have a different value before proceeding. The Object.notify()
method wakes up only one thread at a time. Unless it happens to wake up the thread that is required to perform the next step, the program will deadlock.
Compliant Solution (notifyAll()
)
In this compliant solution, each thread completes its step and then calls notifyAll()
to notify the waiting threads. The thread that is ready can then perform its task, while all the threads whose condition predicates are false (loop condition expression is true) promptly resume waiting.
...
Code Block | ||
---|---|---|
| ||
public final class ProcessStep implements Runnable { private static final Object lock = new Object(); private static int time = 0; private final int step; // Do operations when field time reaches this value public ProcessStep(int step) { this.step = step; } @Override public void run() { try { synchronized (lock) { while (time != step) { lock.wait(); } // Perform operations time++; lock.notifyAll(); // Use notifyAll() instead of notify() } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // Reset interrupted status } } } |
Noncompliant Code Example (Condition
Interface)
This noncompliant code example is similar to the noncompliant code example for notify()
but uses the Condition
interface for waiting and notification.
...
As with Object.notify()
, the signal()
method may awaken an arbitrary thread.
Compliant Solution (signalAll()
)
Wiki Markup |
---|
This compliant solution uses the {{signalAll()}} method to notify all waiting threads. Before {{await()}} returns, the current thread reacquires the lock associated with this condition. When the thread returns, it is guaranteed to hold this lock \[[API 2006|AA. Bibliography#API 06]\] The thread that is ready can perform its task, while all the threads whose condition predicates are false resume waiting. |
...
Code Block | ||
---|---|---|
| ||
public class ProcessStep implements Runnable { private static final Lock lock = new ReentrantLock(); private static final Condition condition = lock.newCondition(); private static int time = 0; private final int step; // Do operations when field time reaches this value public ProcessStep(int step) { this.step = step; } @Override public void run() { lock.lock(); try { while (time != step) { condition.await(); } // Perform operations time++; condition.signalAll(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // Reset interrupted status } finally { lock.unlock(); } } } |
Compliant Solution (Unique Condition Per Thread)
This compliant solution assigns each thread its own condition. All the Condition
objects are accessible to all the threads.
...
This compliant solution is safe only when untrusted code cannot create a thread with an instance of this class.
Risk Assessment
Notifying a single thread rather than all waiting threads can pose a threat to the liveness property of the system.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
THI04 THI02-J | low | unlikely | medium | P2 | L3 |
Related Guidelines
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b7f5b7f2d0562764-cf003b0b-48d84976-b285bd34-727c7f65069cd4e9606caa87"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="086406e6512dc765-40e1b6b7-406a4b99-bad997c9-81e708a388a3a93876cb8c59"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks | http://java.sun.com/docs/books/jls/third_edition/html/memory.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="405a859816e78544-316feb35-427447b3-af7881f5-5d149991e30a9d427d8a82aa"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 14.2.4, Notification | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="df2fe18df19cdbfb-810d0abf-464c4f6c-860f979d-621048a6614d7345d58aea1a"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. Bibliography#Bloch 01]] | Item 50: Never invoke wait outside a loop | ]]></ac:plain-text-body></ac:structured-macro> |
...