Can someone please read this and tell me what they think. I think that these two sentences contradict each other, and if this is the general consensus then i am pissed off at this book.
The question:
Which of the following statements are true?
(a) No two threads can ever simultaneousy execute synchronized methods on the same Object.
(b) Methods declared synchronized should not be recursive, since the Objects monitor will not allow simultaneous invocations of the method.
(c) synchronized methods can only call other synchronized methods directly.
(d) Inside a synchronized method one can assume that no other
threads are currently excuting a method in the same class.
The answer is a. but this is incorrect in my opinion. This is the explination that the book gives and it seems to contradict itself.
"No two threads can ever simultaneousy execute synchronized methods on the same Object. This does not prevent one thread from executing a non-synchronized method while another thread executes a synchronized method on the same object."
Seriously, this is out of my scope, at least at present. But here is a comment if you need one:
The explanation says a thread can execute a method non-synchronously, while another is executing it synchronously. This means two threads cannot execute a method synchronously at the same time. Both 'synchronously' and 'at the same time' must be considered here.
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
Yes youre right. Im sorry. I have too look into this more though, becuase when a thread wishes to invoke a static method it first must aquire a lock on the class, thus preventing another thread from running a method within that class as i understand it.
We have the Java keyword synchronized and we have the concept synchronized due to timing.
No more than one thread at a time can execute an Object's method that has the keyword synchronized in its method definition.
synchronized methodA() can be executed simultaneously with
methodB() since methodB() has no "synchronized" keyword in its definition, but only one thread can execute methodA at this time. synchronized methodC() cannot be executed during the time that synchronized methodA() is being executed.
Long time no see. But is there a diffrence in executing a syncronized static method and excuting a syncronized non-static method? Dosent the static method require a lock on the class thus causing a syncronized non-static method to block?
I suggest you confirm this answer with Sun. In my opinion, the phrasing in my sources and my test program seem to imply that "1) static" and "2) non-static" methods are two separate "physicalinstances". Programmers usually deal with "logicalinstances". I tend to think that this has to do with "loading" methods into memory (stack versus heap / static versus instance). So by this reasoning, I justify the surprising results of my program that did not block when "synchronized static" and "synchronized non-static" methods were invoked at the same time.
If "Object" in
(a) No two threads can ever simultaneousy execute synchronized methods on the same Object.
means "Instance" or more specifically "Logical Instance", then they are really only asking about non-static synchronized methods anyway. On the Certification Exam, there is a comment section for each question. You could use the comment section to specify that you are assuming that they mean non-static methods.
If they ask you directly, as you have asked me, then I'd say it does not block. You should write your own test program to confirm this. I had strange results on one day, then consistent results on following days. I almost posted a strange matrix using various combinations of the following:
Test t = new Test();
Thread t1 = new Thread(t);
Thread t1 or t2 = new Thread(new Test());
...invoking "public static synchronized methodA(boolean isThread)" from main(String[]) using "methodA(false)" and from run() using methodA(true).
To quote the author of a practice exam after discussing a challenging question, "You will be very unlucky to be asked such a question".
If you get an answer from Sun, maybe you can post it along with your "resolved technique".
I tend to think that this has to do with "loading" methods into memory (stack versus heap / static versus instance).
Right, so i guess this would apply the same as in variables. Since static variables are allocated at load time on the data segment and dynamic variables are allocated at run time on the stack.
Im going to write my own program tonight and fiddle around to se what results i get. And while im at it ill mess with yours too!