aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
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)


Harish<br />SCJP 1.4 (85%)
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

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


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads