• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass parameters to child threads.  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a main thread. I want to create threads from the main thread. so code will be like this,

main class


childThread class


I want to pass list to run() method. With-out making list as global variable.


Thanks All:
Ramakrishna K.C
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would make a private member variable in class childThread, and set it in the constructor of class childThread or in the createThreads method.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't the createThreads method be in your threadDemo class and shouldn't it be creating a new childThread instance for each new thread to run.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would make a private member variable in class childThread
.
It'll become global right? If global, i don't want to make that.

Thanks:
Ramakrishna K.C
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It'll become global right? If global, i don't want to make that.

No, a private member variable is not global it's private to the instance.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh.. okay.. I got that. then ma sample code will be,



then, for each instance ma list is separate and update-able right?

Thanks:
Ramakrishna K.C
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna,

As Tony said put the createThreads method in you threadDemo class. You will be fine.

Note: threadDemo? I think camelCase should be used for method names not class names. Its not an error but still wanted to point it out.

Sorry late reply, I think your code will work fine, give it a try and get back. If the answers here helped give a thumbs up to them.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then, for each instance ma list is separate and update-able right?

Yes but as I pointed out earlier your code to create the child threads uses the same childThread object for each new Thread so in your case all threads will be using the 'list' variable.
Note also that if you do change your code to create new instances of childThread but pass the same List object to each childThread constructor then again each thread will be accessing the same List object.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Partheban Udayakumar

I fixed/implemented code as Tony said and I got what I expected. But, it s a just confirmation that Is that private variable update-able dynamacally each time or not. I'll try/see that in code, but just a confirmation.

Thanks All
Ramakrishna K.C
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I got the expected solution fro previous case. But, In the same way I'm doing for AtomicInteger also. I'm not getting it. My sample code is HERE (last post).
Can anyone Please check and tell me why I'm not getting expected result.

Thanks All,
Ramakrishna K.C

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!