• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Confusion in threads

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both instance members and static members are shared between threads. If a thread1 changes one instance variable it might also effect thread2.
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please provide help

Thanks
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic