aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion in threads" Watch "Confusion in threads" New topic
Author

Confusion in threads

Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Hi All,

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.

Thanks
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

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 ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Both instance members and static members are shared between threads. If a thread1 changes one instance variable it might also effect thread2.
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
You might also be interested in volatile:

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.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
If the threads are run on the same instance of the Runnable then yes.



Try running it on different instances like :

In this case, the threads will have their one copy of the instance variables.
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Hi thanks for the help.

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.

Thanks
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Please provide help

Thanks
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi Subu.

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?

Additionally, you might wanna take a look at synchronization issues. Be carefull with the concept of "shared data" (maybe this is what is making you confused) in multithreading environments. Please, take a look at the following article:
- http://www.javaworld.com/javaworld/jw-07-1997/jw-07-hood.html
- http://www.onjava.com/pub/a/onjava/excerpt/jthreads3_ch6/index1.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in threads