This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes UCertify Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "UCertify Questions" Watch "UCertify Questions" New topic

UCertify Questions

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35

A Java code uses two variables (var1 & var2). The code has two threads (th1 & th2). Which of the following can be guaranteed from the code.
A-The th1 and th2 threads will not interrupt each other if the var1 variable is accessed inside a synchronized method.

B-If var1 is an instance variable, the th1 and th2 threads will operate on the variable without interrupting each other.

C-A variable declared inside a static method cannot be accessed from more than one thread at a time.

D-IF var1 is a method-local variable, the th1 and th2 threads will operate on the variable without interrupting each other.


Why is A guaranteed ? If both threads get the same Runnable instance, then th1 would be "interrupted" if th2 already has the lock. Unless, this definition of interrupted is wrong.

Their explanation is "The synchronized keyword is used w/ a method declaration or a block of code to ensure that at a time only one thread can execute the method or the block of statements. Other threads wanting access to the method are forced to wait until the currently executing thread returns from the method"

Also, I thought 'B' would be correct, because that would be a good design, though not guaranteed. Which is why i think this is an ambiguous question.
It is sorta covered in the JavaRanch Style Guide.
subject: UCertify Questions
Similar Threads
SCJP practice question
When will be single threaded servlet mandatory to implement?
Need help for k&b book: thread question...
Note on Thread