Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

re-write help needed

 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic