K and B master exam clarification---Threads

I felt that nothing can be said for sure about this answer,as the method is static synchronized and there will be only one thread accessing that method at any time eventhough we create multiple thread objects.

Note that even though you have two threads executing at the same time, that is not really relevant to the question.

What they are saying in their solution is that even though you can't control when one thread will be executing the method, you do know that inside each run() method, the statements are executed in sequence.

That is

say("a"); say("b"); say("c");

are executed in sequence.

This also happens when you are printing the Strings containing the numbers.

So you know that c can't be printed before a because say("c") is executed after say("a"). [ November 02, 2006: Message edited by: Keith Lynn ]

In this example synchronized means, that first invocation of say methed must end before next invocation can be done.Thus in possible results set "a" is always followed by "b" and so on. Without synchronization JVM can first perform say("b") befor say("a").

Sorry for my english..

prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162

posted Nov 02, 2006 09:56:00

0

Originally posted by Keith Lynn: Note that even though you have two threads executing at the same time, that is not really relevant to the question.

What they are saying in their solution is that even though you can't control when one thread will be executing the method, you do know that inside each run() method, the statements are executed in sequence.

That is

say("a"); say("b"); say("c");

are executed in sequence.

This also happens when you are printing the Strings containing the numbers.

So you know that c can't be printed before a because say("c") is executed after say("a").