Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Execute thread from another class?  RSS feed

 
J Gib
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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