• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried compiling program you mentioned but i got the ouput as

a.i=1 a.i=0
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That clarifies a lot..
My Thanks and regards to JIM & Keith
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic