aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K and B master exam clarification---Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K and B master exam clarification---Threads" Watch "K and B master exam clarification---Threads" New topic
Author

K and B master exam clarification---Threads

prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Iam not able to understand the solution for this question




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.

But answer given are


Now iam confused with both question and answer?
Any takers?

Many Thanks
Prashanth


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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 ]
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
My Mistake..
I realised the answer..I thought all say method would spawn as an individual threads[dont know why i thought so ]...since the 2nd say method wont run till 1st say() method is returned[synchronous],the answer makes sens..

Thanks
Prashanth
Maciej Zpolski
Greenhorn

Joined: Nov 02, 2006
Posts: 10
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
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").

[ November 02, 2006: Message edited by: Keith Lynn ]


Thanks keith..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: K and B master exam clarification---Threads