• 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

Use methods of another Thread?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
is it possible to use methods from an instance of a thread in a nother instance of the thread?
I have create a main class and an inner-class (witch extends Thread). The main-class create some instance (hashes) of the inner-class. Some of the instances of the inner-class (threads) have to transfer data between eachother.
�f tried the way over the main class :
super.innerClassInstance.get(name).transferData("hello world");
thx
bb
p.s.:
sorry for my bad english
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I give an answer, please review our naming policy and change your alias. Not because I enjoy to be a pain in the backside, but just to retain suit & tie backwards compatibility
Threads are in no way different from other objects, so they can invoke methods on each other as long as they have (access to) references to each other - which in your case the hash should provide. Obviously, the usual caveats of threadsafe programming apply.
- Peter
 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
first, i hope thats better with my name
you are not a pain in my backside.
But, lets go back to my main problem, i tried the way over the main class.
I have showed myself steps in the sourcecode, but i always have the same problem (i do not rund the method i just print out the value of the instance):
1. i print "mainClass.innerClass". i get the the value of the thread itself.
2. When i print mainClass.innerClass.get(name) i only get the value of the complete hashtable i think (always Thread[Thread-0,5,]).
What's wrong? Where is my mistake?
thx
lt
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
To be honest I don't have a clear picture what you're trying to do, so I will just waffle on for a bit to cover my ignorance Code such as this should do exactly what you'd expect it to do - the threads (and their names) will be retrieved from the map; when the threads are executing Thread.currentThread() will give the right thread, etc.
What's the difference between the above and your code?
- Peter
 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
first :
I admit that I'm a greenhorn in java and thats my first time with object oriented programming
So, it wasn't your explaining but my isuficcient understanding (and some problems from my rudymentary knowledge of the english language).
I think, I need more practice and vocabulary, don't I ?
But lets go back. How i'd explain, i don't have any real practice, yet. So that what you did want to explain me, was on a level many steps over my head, but your help was great. I've learn something new and thats why i'm here.
My program is working now
The solution is :
((MyClass)outerClass.hashInstanceOfInnerClass.get(name)).method()
(I admit, that isn't my solution :roll: )
Now i beginning to understand
So,
thanks for help
Lars
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic