Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Both Threads Printing their own variable x value.

 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this Example when both threads are running they are printing their own values of variable x,I consider that if variable x is a global varibale for the class, then if one thread makes from x=7 to x= 8 then second thread should consider x=8,but it is considering it x=7 why ?

Please Expalin.

[NK: Added code tags. Please UseCodeTags while posting code]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new T() means a new instance of T. So you have two new instances of T, each with its own instance variable. x is an instance variable of T. It's not static. Another instance of the same class won't interfere with it.
 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christope Understood.

Then how can we create Dirty Data read problem in this Example.
Means if one thread makes x= 8 then next one should read it like x= 8 not 7.
please show me the way in the same example.

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using the same instance ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic