• 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

re-write help needed

 
Ranch Hand
Posts: 57
  • 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 simple class like this



I have another class that has this code, which runs the MyClass.MyMethod, but i want ot be able to re-use the Threads class to runother methods as well, how can i rewrite it, so that I can call other methods in the run()?




Thanks for any help
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have another class that has this code, which runs the MyClass.MyMethod, but i want ot be able to re-use the Threads class to runother methods as well, how can i rewrite it, so that I can call other methods in the run()?



A few suggestions...

1. Add a constructor to the RemindTask class to config it -- what methods to call, where to store the result etc.

2. Don't cancel() the Timer object in your RemindTask class, as you may want to reuse it.

3. You may also want to separate the scheduling of the task, in order to use the timer for multiple requests.

Henry
 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply

what do you mean by 'add a constructor class'

sorry for not understanding
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dale conn:
thanks for the reply

what do you mean by 'add a constructor class'

sorry for not understanding



Add a constructor -- not a class. In pseudo code, something like this...



Now, of course, this is based on personal preference. There are probably many better ways to do this.

Henry
 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there, i'm still not getting this.

if i have a basic class like this and i want to pass a method to the run()in the Threads class, how do i do this?

I will only need to pass the methods in the MyTestClass to the run() when the methods in MyTestClass are called by a Client app

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't pass anything to the run() method from Thread because the method public void run() in Runnable doesn't have any parameters.

You can create a class definition that has a reference to an object of your class as its instance variable and implement Runnable.

Then create a Thread using an instance of the class definition.
[ June 23, 2006: Message edited by: Keith Lynn ]
 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there

is it possible for you to show me an example?

how do i do this ?

>You can create a class definition that has a reference to an object of your >class as its instance variable and implement Runnable.

>Then create a Thread using an instance of the class definition.


Thsi is where i am stuck. How do i create a class defintion of MyTestClass and pass myMethod1 as an instance of the class defintion?

thanks for your help
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a skeleton class that kind of gives you an idea of what I mean.

public class MyThread implements Runnable {
MyClass myClass;

public MyThread(MyClass myClass) {
this.myClass = myClass;
}

public void run() {
...
}
}


Notice that the run method inherited from Runnable becomes an instance method of the class definition. So in the run method you have access to the instance variables in the class definition.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic