IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes Use methods of another Thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Use methods of another Thread?" Watch "Use methods of another Thread?" New topic
Author

Use methods of another Thread?

Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
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
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Feb 07, 2002
Posts: 45
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
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Feb 07, 2002
Posts: 45
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
 
IntelliJ Java IDE
 
subject: Use methods of another Thread?
 
Threads others viewed
Marcus mock exam 2 (quest. #32)
Nested Class
no static method in inner class - why ?
Accessing inner class from method
creating an inner class
developer file tools