Okay, so say I'm dealing with nested threads. (I'm just making a simple one up). And I've got a variable that is changing. When my last nested
thread is complete, how can I return that variable to the class that contains the main method?
public class ECounter
{
public static void main(
String[] args)
{
Execution anExecution = new Execution(0,"sleepwalker");
Thread aThread = new Thread(anExecution);
aThread.start();
int eCount = /*Here is where I want the number of e's that were
counted in the nested threads.*/
}
}
public class Execution implements Runnable
{
private int counter;
private String
word;
public Execution(int counter, String word)
{
this.counter = counter;
this.word = word;
}
public void run()
{
if(word.startsWith("e"))
counter++;
word = word.substring(1,(word.length()-1));
if(word.length() == 0)
{
/*Here is the point when I want a value returned. I want to return
the counter to the main class, when the String word is empty. But I
have no idea how, since I can't change the "void" return type for
the run method to "int"!*/
}
else
{
Execution nextExecution = new Execution(counter,word);
Thread nextThread = new Thread(nextExecution);
nextThread.start();
}
}
}
Thanks so much for your time!!!