2

I wanted to ask how do I detect a critical region in Java? I understand that it's to implement mutual exclusion with two atomic processes but what are the general things I just look for to determine that this is a critical region.

I'm very curious to know whether:

  • all synchronized methods in Java critical regions?

  • are critical regions only found in concurrent systems in Java?

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 1
    "Are critical regions only found in concurrent systems in Java?" - How would you **define** "a critical region"? If a system is not "concurrent" - how can the system meet that definition? – Fildor Oct 24 '16 at 09:01
  • A critical section is what you define it to be. You can have critical sections which are critical from a performance rather than a thread safety point of view. – Peter Lawrey Oct 24 '16 at 09:03
  • You don't detect them. You *design* them. Unclear what you're asking. – user207421 Oct 24 '16 at 09:11
  • I believe you think that critical region are part of the code where synchronisation take places, or maybe werethere is a common resource for multiple thread. Is that what you mean ? Before the edit, there was written you read this from multiple source, what are these sources ? And of course it's not clear, that's why he is asking this question. – Asoub Oct 24 '16 at 09:29
  • 2
    @PeterLawrey in this question he should be referring 'critical section' under the context of concurrency. :)) – Supun Wijerathne Oct 24 '16 at 13:45

3 Answers3

3

In concurrency a Critical Section is simply a code fragment where one thread can write/read into/from memory when some other is writing into the same memory or one thread is going to write into the memory when some other thread is reading from the same memory.

Summary

  • Thread1 - write | Thread2 - write | Safeness - No

  • Thread1 - write | Thread2 - read | Safeness - No

  • Thread1 - read | Thread2 - write | Safeness - No

  • Thread1 - read | Thread2 - read | Safeness - Yes

i.e. They are the section in our program where it can cause possible errors/malfunctions if we don't implement proper control over it. (Ex: locks, semaphores, barriers etc.)

And it is not only applied for Java. It applies to any programming language which supports threads.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 1
    +1, I didn't knew this was an official term in concurent programming: https://en.wikipedia.org/wiki/Critical_section (even if it's section rather than region as OP said). As for the second question, I'm not sure the question was about Java especially, but more like : "are critical regions only found in concurrent systems ?", but the answer would obviously be yes, as this term only concern parrallel programming. – Asoub Oct 24 '16 at 12:19
  • @Asoub yes. The word 'critical' can have different interpretations as per the context it is used. I just stated what it means in the context of concurrent programming. :)) And thnx for your source, I added that to my answer. :)) – Supun Wijerathne Oct 24 '16 at 13:39
2

all synchronized methods in Java critical regions?

Not only synchronized methods/blocks, code using LockAPI too safeguard critical region ( section)

Multiple ways of guarding critical section.

1. synchronized methods:

public synchronized void incrementCounter(){
    ++counter;
}

2. synchronized statements

public int getCounter(){
    synchronized(this){
        return counter;
    }
}

3. Lock API

class X {
   private final ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body, which is critical section
     } finally {
       lock.unlock()
     }
   }
 }

Useful SE questions:

Why use a ReentrantLock if one can use synchronized(this)?

Avoid synchronized(this) in Java?

are critical regions only found in concurrent systems in Java?

No. They can be found in other languages too.

Related SE question:

C# version of java's synchronized keyword?

Does PHP have the equivilant to Java 'synchronized' , or is this not required?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

You need to check if some value is shared between multiple threads, if yes can it be deemed "corrupt" when the value is set or get but these threads, If a use case breaks due to this corrupt value lock it down its a critical region.

Bot
  • 1
  • 1