*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "UCertify Questions" Watch "UCertify Questions" New topic
Author

UCertify Questions

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
1.

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.

ANSWER: A & D.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
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