• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

even on synchronizing, why o/p cannot be determined??  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is the output of the following code when compiled and run? Select two correct answers.

A. The output will always be a.i=0 a.i=0
B. The output cannot be determined.
C. The output will always be a.i=0 a.i=-1
D. t1 and t2 access the same member variable a.i.
E. Compilation error.

Ans. B,D
Actually i thought it would be A,D. I tried compiling and running the code, the o/ps were different for most of my executions. I'm still unable to figure out why so???


[HENRY: Fixed Code Tags]
[ November 26, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the method that is synchronized is an instance method and the two threads are built using two different instances of the class.

Since different methods are being accessed by the two threads, the synchronized statement doesn't do any good.
 
Kalpesh Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean that these two instances of class Question52 do not interfere with each others synchronized code AT ALL ?? and if that is true, then this is like only 1 thread accessing 1 instance so synch. doesn't matter.

plz correct me if my understanding is wrong.
[ November 27, 2006: Message edited by: Kalpesh Jain ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried compiling program you mentioned but i got the ouput as

a.i=1 a.i=0
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Kalpesh]: Does it mean that these two instances of class Question52 do not interfere with each others synchronized code AT ALL ?? and if that is true, then this is like only 1 thread accessing 1 instance so synch. doesn't matter.

No, the two threads can and do interfere with each other. What Keith was saying is that it's as if there were no synchronization at all - nothing prevents the two threads from executing aMethod() and/or bMethod() simultaneously. And since these methods are accessing the same static variable i, this can create all sorts of problems, depending on what order the two threads do things in.

Suguna, try running the program multiple times. you should see different output - maybe not different every time, but at least different some of the time.
[ November 27, 2006: Message edited by: Jim Yingst ]
 
Kalpesh Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clarifies a lot..
My Thanks and regards to JIM & Keith
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!