• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Getting Data From Nested Threads.  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Future interface. The main thread can kick off a Future and then get the results. The main thread can poll or block while waiting.

Now if you want to return data to the calling object instance and not the calling thread, the worker Runnable can call a set method back on the calling object. That takes place on the worker thread, so depending on what your program does the main thread still might have to poll or block to see when the result is available.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vanessa Astle:
Okay, so say I'm dealing with nested threads.



What's a "nested thread"?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:


What's a "nested thread"?



A thread inside of another thread I assume?
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct. A thread that gives birth to itself. It's really just recursion.
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your answer Stan! That's definitely what I was looking for and it will save me a giant mess and keep my program OO.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!