Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how value of instance Variable affected by MultiThread?

 
Lalit Sahu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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??
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • 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: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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.
     
    Wink, wink, nudge, nudge, say no more, it's a tiny ad:
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!