aspose file tools*
The moose likes Beginning Java and the fly likes re-write help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "re-write help needed" Watch "re-write help needed" New topic
Author

re-write help needed

dale conn
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dale conn
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
thanks for the reply

what do you mean by 'add a constructor class'

sorry for not understanding
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Jun 16, 2006
Posts: 57
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jun 16, 2006
Posts: 57
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

Joined: Feb 07, 2005
Posts: 2367
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: re-write help needed