This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

shared resourec + class devel declaration  RSS feed

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a Test class that defines instance variable. If you have more than one thread and each one is operating on different instance of Test, will that instance variable still be a shared resource? I mean will there be any race condition in this case.

If all thread operate on same instance of Test, then instance variable will be shared one just like servlet should not have any class level declaration.

Thanks & Regards

Naseem
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
There is a Test class that defines instance variable. If you have more than one thread and each one is operating on different instance of Test, will that instance variable still be a shared resource? I mean will there be any race condition in this case.


Instance variables are just that -- instance variables. Each instance of the (Test) class that has that variable will have its own instance of the variable. Changing the reference in one Test class will *not* affect another instance of the Test class.

As for whether "there be any race condition", it's not that simple. While each instance of the Test class may have its own copy of the variable, the variable may be a class that contain static variables. So, any race condition is still dependent on how the instance variable is used.

Henry
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




You mean to say that in this case, even if different thread has different instances of Test, but still race condition could be possible because Test has-a Test2 which has a static declaration.

Am I right?

Regards


Naseem
[ July 03, 2006: Message edited by: Naseem Khan ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!