aspose file tools
The moose likes Threads and Synchronization and the fly likes Execute thread from another class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Execute thread from another class?" Watch "Execute thread from another class?" New topic
Author

Execute thread from another class?

J Gib
Greenhorn

Joined: Jul 20, 2006
Posts: 5
Hey everyone,
Heres my situation: I'm already extending Applet so extending Thread is not an option. I've already used the "Run()" method in the class i've been coding, but i need to run another thread which does something completely different: (Checks to see if a txt file has been changed every 60 seconds) -- since i can't have a second "Run" method in that class, im guessing that i need to make a second class with it's own Run() and somehow execute that method from my original class. Does that sound about right? if so, how do i do it?! I've been researching this for several days with no luck.. if anybody can help me out or possibly suggest some alternatives i would really appreciate it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Hi,

Welcome to JavaRanch!

Yes, you can simply define a second class that implements Runnable, and just pass an instance of that to the Thread constructor instead of "this".

new Thread(new MyOtherClass()).start();

If MyOtherClass needs access to parts of the main class, then you have several alternatives. The easiest one may be to simply use an inner class -- a class defined inside another class. The inner class has access to its outer class's private members.



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Execute thread from another class?
 
Similar Threads
Thread & main()
Will a started thread run to completion in this example?
One time pass teardown and setup in Cactus
run()
Single Object Concept in synchronization