Thread Help
Hi i am new in using threads. i have an application that according to the request executes some methods. i want to perform this method as thread. does the following sample code will appropriately perform method in thread as i want to:
public class request
{
public static void executeRequest (String ar)
{
// must start new thread upon each call
threadClass newThread = new threadClass();
newThread.start();
}
}
class threadClass implements Runnable
{
public void run()
{
// calls another method in another class
new getData();
}
}
Does this fulfill my purpose?
my another problem is if i want to return data it gets from calling getData method?
regards,
tom
[805 byte] By [
tomcl] at [2007-9-26 19:45:12]

You could make the threadClass an inner class in request, and then call a method in your request when the threadClass is done:
public class request {
class threadClass extends Thread {
public void run() {
// your code
addResult(value);
}
}
public synchronized void addResult(int value) { // doesn't have to be int
// do something with this value
}
}
If you use static methods, then make addResult static, and call the method as request.addResult(value). If it is fine as non-static, you might have to call addResult as request.this.addResult(value) or make the threadClass static.Just ask again if you can't make it work..
Optionally to inner class you can change the lines
threadClass newThread = new threadClass();
newThread.start();
to
Runnable newThread = new threadClass();
Thread t = new Thread(newThread);
t.start();
-Raine-