• 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 to pass parameters to child threads.

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

reply
    Bookmark Topic Watch Topic
  • New Topic