41 CRITICAL SECTION

DESCRIPTION

A Critical Section is a segment of code that must be executed by only one thread at a time to produce the expected results. When more than one thread is allowed to execute this code segment, it could produce unpredictable results. By this definition, a critical section looks very similar to the concept of a Monitor discussed in Section III — Basic Patterns. The following is the list of similarities and differences between Monitors and Critical Sections:

  • A Critical Section is a stricter form of a Monitor.
  • A Monitor locks a single object whereas a Critical Section requires a lock on an entire class of objects.
  • In Java:
    • The implementation of a Monitor on a method requires the method to be declared using the synchronized ...

Get Software Architecture Design Patterns in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.