Java allows for certain keywords to be followed by a statement or a statement block. For example:
if (true)
System.out.println("true");
do
System.out.println("true");
while (true);
compiles as well as
if(true) {
System.out.println("true");
}
do {
System.out.println("true");
} while (true);
This is also true for keywords like for
, while
etc.
However, some keywords don't allow this. synchronized
requires a block statement. Same for try ... catch ... finally
, which requires at least two block statements following the keywords. For example:
try {
System.out.println("try");
} finally {
System.out.println("finally");
}
synchronized(this) {
System.out.println("synchronized");
}
works, but the following doesn't compile:
try
System.out.println("try");
finally
System.out.println("finally");
synchronized (this)
System.out.println("synchronized");
So why do some keywords in Java require a block statement, while others allow a block statement as well as a single statement? Is this an inconsistency in language design, or is there a certain reason for this?
We need to synchronize the shared resources to ensure that at a time only one thread is able to access the shared resource. If an Object is shared by multiple threads then there is need of synchronization in order to avoid the Object's state to be getting corrupted. Synchronization is needed when Object is mutable.
As long as it's there, it sits idle. Note also that wait() forces the thread to release its lock. This means that it must own the lock of an object before calling the wait() method of that (same) object. Hence the thread must be in one of the object's synchronized methods or synchronized block before calling wait().
A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.
Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method. A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource.
You get a dangling else-like ambiguity if you try to allow leaving out the braces. Whilst this could be solved in a similar fashion to the dangling-else, probably best not to.
Consider
try
try
fn();
catch (GException exc)
g();
catch (HException exc)
h();
catch (IException exc)
i();
Does that mean
try
try
fn();
catch (GException exc)
g();
catch (HException exc)
h();
catch (IException exc)
i();
or
try
try
fn();
catch (GException exc)
g();
catch (HException exc)
h();
catch (IException exc)
i();
I believe in CLU, catch blocks were around just one statement (may be wrong).
It's just the design decision of the language, and its compiler mechanics.
I agree with the decision. Not requiring a code block might make the code shorter, but it's a sure fire way to cause confusion and create unforeseen consequences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With