• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K and B master exam clarification---Threads

 
prashanth kumar
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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").
 
prashanth kumar
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic