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]
# 1

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

}

}

abnormal at 2007-7-3 12:48:02 > top of Java-index,Archived Forums,Java Programming...
# 2
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..
abnormal at 2007-7-3 12:48:02 > top of Java-index,Archived Forums,Java Programming...
# 3

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-

rainek at 2007-7-3 12:48:02 > top of Java-index,Archived Forums,Java Programming...
# 4
thanks a lot to both of you. i need to get return value from the method the thread class is calling. how can i do that?regards
tomcl at 2007-7-3 12:48:02 > top of Java-index,Archived Forums,Java Programming...