• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread - instance variables  RSS feed

 
Prathik Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
According to JLS,all the instance variables are shared among the Threads.But in this example the initValue is null for each thread iniitally.Please let me know if i am missing anything.

Thanks in advance,
Prathik


[HENRY: Added Code Tags]
[ December 18, 2006: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each one of your five threads have their own Runnable (TestThread) object -- they are not sharing the same object.

Henry
 
Prathik Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.So when are instance variables shared?
Regards,
Prathik
 
Henry Wong
author
Sheriff
Posts: 23026
120
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 Prathik Kumar:
Thanks Henry.So when are instance variables shared?
Regards,
Prathik


No magic here... Instance objects are shared, when you share them. For example...



Henry
 
Prathik Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.So the way i am creating threads is same as creating object instances.am i right Henry?.
Thanks for your help.

Regards,
Satish
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by that. Your program has Thread instances and TestThread instances. The Thread instances correspond to actual threads of execution, but the TestThread instances do not (despite their misleading names) because TestThread does not extend Thread. That's OK, you don't have to extend Thread since you're creating Thread objects separately, that's fine - but to say "the way i am creating threads is same as creating object instances" seems very odd, to me. I'm afraid if someone says "yes" that might confirm some strange idea in your brain which we don't understand.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathik asked:
So the way i am creating threads is same as creating object instances.am i right?


The answer is in your ...



Yours,
Bu.
 
Prathik Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim and Bu for your answers.

Regards,
Prathik
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically a Thread or a runnable object is nothing but a Java object only.The class Thread (or a runnable object)is an implemetation of Java's multithreading model.

So there's no reason why a thread or for that matter a runnable object should be intantiated in a way different from java object instantiation
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!