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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com