Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronized (expression) block

The synchronized statement has the form shown above. Which of the following are true statements?

a. A compile-time error occurs if the expression produces a value of any reference type
b. A compile-time error occurs if the expression produces a value of any primitive type
c. A compile-time error does not occur if the expression is of type boolean
d. The sychronized block may be processed normally if the expression is null
e. If execution of the block completes normally, then the lock is released
f. If execution of the block completes abruptly, then the lock is released
g. A thread can hold more than one lock at a time
h. Synchronized statements can be nested
i. Synchronized statements with identical expressions can be nested


given answer is :b,e,f,g,h,i


i didnt get the question
can anyone explain the above one(explain for each option)
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option b : (A compile-time error occurs if the expression produces a value of any primitive type)
Synchronized block will only accept objects not primitives.

Option e & Option f If execution of the block completes normally (or) abruptly, then the lock is released)
After synchronized block is executed, Object lock acquired by the thread before executing this block is released; it doesn't matter whether thread exits the block normally or abnormally.

Option g A thread can hold more than one lock at a time)
Thread executing the below synchronized block(block A) will acquire two locks.(object1, object2).

Example:

//block A
synchronized (object1) {
synchronized (object2) {
doSomething();
}
}

Option h :
See above example.


--Prabhu
 
Cheenu Subramanian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will give it a try. Correct me if I am wrong..

Either a method can be synchronized or an object can be synchronized(Synchronized block).

a. A compile-time error occurs if the expression produces a value of any reference type
A Compile time error will not occur as the expression produces a reference type . Hence false

b. A compile-time error occurs if the expression produces a value of any primitive type
A Compile time error will occur as the expression produces a primitive type. Hence true

c. A compile-time error does not occur if the expression is of type boolean
Since boolean(a primitive ) is produced, error does occur. Hence false.

d. The sychronized block may be processed normally if the expression is null
I am not sure about this

e. If execution of the block completes normally, then the lock is released
As the synchronized block is completed, lock is released. Hence true

f. If execution of the block completes abruptly, then the lock is released
As the synchronized block has completed though abruptly, lock is released. Hence true

g. A thread can hold more than one lock at a time
I am not sure about this too.

h. Synchronized statements can be nested
Synchronized blocks can be nested. Hence true

i. Synchronized statements with identical expressions can be nested
Not sure

A detailed explanation would be help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic