I have doubt in acessing the instance members for threads.
in multithreaded environment will the instance members such as the instance variables modified by one thread visibel to other tread.That is to say will every thread have separate copy of instance members (variables and methods)or do they share the same copy in case of static members I know that they are shared between the threads and so one thread can get to see the modified value of the another thread but for instance members I think every thread will have separate copy of instance members.
Threads classes behave in exactly the same way as ordinary classes, regarding accessibility of instance and static members. So, if class MyThread has a private member myData, others classes don't have access to it. Does that clear your doubt? [ May 18, 2007: Message edited by: Leandro Melo ]
The volatile keyword is used on variables that may be modified simultaneously by other threads. This warns the compiler to fetch them fresh each time, rather than caching them in registers. This also inhibits certain optimisations that assume no other thread will change the values unexpectedly. Since other threads cannot see local variables, there is never any need to mark local variables volatile.
So does it mean to say that if we create a thread by implementing Runnable interface and if we use same runnable object then different threads will share the instance members(variables and methods). If we use different runnable objects then two different threads will have separate copy of instance members(variables and methods).
But when we create a thread by extending Thread class will this be same or will it differ. I think when we create a thread by extending Thread Class different thread will share instance members(variables and methods)in all cases Please Help me in understanding this Problem.
Please Provide links where I can get complete information based on Threads.
Instance members belong to instances and not classes. The example of Mrishnan is what you need to understand. If you extend a class from Thread, each instance will have their separate copies of their instance members. So, they're not shared. However, if you're creating your threads through means of the Runnable interface, using the same instance of the Runnable interface, you'll have shared instance members, because they actually belong to the class that implements the Runnable interface (and not to the thread itself). Is it clear?