• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how value of instance Variable affected by MultiThread?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






Output::A0B0A1A2B1..........so on...A9B8B9.

Object contain  copy of its own instance Variable that means tt object of class Thread1 contain one copy of Instance Variable "int i".
In above code  two object A and B used to create two Thread.Both thread  accessing runMethod() through tt object and changing value of Instance Variable  int i . If there is one copy of instance variable then how both Thread accessing instance variable independently without interfering  or changing value of instance Variable int i
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Brother wrote:Object contain  copy of its own instance Variable that means tt object of class Thread1 contain one copy of Instance Variable "int i".

Yes each object of Thread1 will have their own copy of an instance variable int i

Kumar Brother wrote:If there is one copy of instance variable then how both Thread accessing instance variable independently without interfering  or changing value of instance Variable int i  


No, each object of MyThread1 will have It's own separate copy of fields of type Thread1 tt; and type Thread thrd; so object referred by tt will have It's own instance variable int i.

here in below codeyou created two object so there will be two separate copies of instance variable int i.
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you created two object so there will be two separate copies of instance variable int i

.

do you  mean each thread contains separate copy of instance variable??
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Each object contains an instance field. It is not a copy and it is not in the Threads. You are causing yourself confusion by calling the class MyThread or Thread1 when it is not actually a Thread.

I deleted a post because you said the same thing twice.
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • An object of MyThread1 referred by mt1 has:
    • An instance variable tt ( This tt refers to an object of Thread1 which has an instance variable int i ) and your invoking runMethod() on this tt.
    • An instance variable thrd which refers to an object of Thread(java.lang.Thread).
  • An object of MyThread1 referred by mt2 has:
    • An instance variable tt ( This tt refers to an object of Thread1 which has an instance variable int i ) and your invoking runMethod() on this tt.
    • An instance variable thrd which refers to an object of Thread (java.lang.Thread).
    As Campbell mentioned, you better change the names of those classes and also runMethod (not similar like run) which are really confusing.

     
    Lalit Sahu
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:

  • An object of MyThread1 referred by mt1 has:
    • An instance variable tt ( This tt refers to an object of Thread1 which has an instance variable int i ) and your invoking runMethod() on this tt.
    • An instance variable thrd which refers to an object of Thread(java.lang.Thread).
  • An object of MyThread1 referred by mt2 has:
    • An instance variable tt ( This tt refers to an object of Thread1 which has an instance variable int i ) and your invoking runMethod() on this tt.
    • An instance variable thrd which refers to an object of Thread (java.lang.Thread).
    As Campbell mentioned, you better change the names of those classes and also runMethod (not similar like run) which are really confusing.




     class Thread1 contains instance varaible
     class MyThread does not know about instance variable of class Thread1 as  there is no inheritance.
     how does  object mt1 know about instance Variable int i of class Thread1( hard to grasp). would you explain it ?
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kumar Brother wrote:how does  object mt1 know about instance Variable int i of class Thread1( hard to grasp). would you explain it ?

    Because object mt1 of class MyThread1 has tt as an instance variable Or field of type Thread1 where your are declaring and initializing It like this Thread1 tt=new Thread1(), means mt1 can access variable int i of  object of Thread1 which is assigned to tt.
     
    Lalit Sahu
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:

    Kumar Brother wrote:how does  object mt1 know about instance Variable int i of class Thread1( hard to grasp). would you explain it ?

    Because object mt1 of class MyThread1 has tt as an instance variable Or field of type Thread1 where your are declaring and initializing It like this Thread1 tt=new Thread1(), means mt1 can access variable int i of  object of Thread1 which is assigned to tt.


    Thk dude.How i miss such an importance concept while learning Java.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
    Note: Please avoid using different text colors rather than black which is a bit difficult to read. Hope you cooperate.
    reply
      Bookmark Topic Watch Topic
    • New Topic