This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!