Re: getting value form the run method of thread
focode wrote:
dear folks i have a situation before me i have to return a value from
the run method
For the record: `I' is always capitalized when using it as a subject
pronoun. And also, proper punctuation makes sentences a lot easier to read.
// lot of imports...
public class something implements Runnable {
While we're nitpicking, Java coding conventions stipulate that classes
should begin with a capital letter.
Thread newThread;
String tempmassage,newmassage;
And a `message' is some information that is conveyed between multiple
people, while a `massage' is something that someone gives to you to make
you feel good. And camelCase is the preferred convention for variable
and method names.
//somewhere in the program
newThread = new Thread(this);
newThread.start();
At this point your program becomes uncompilable, since statements are
not permitted outside of methods.
publiv void run()
It's `public'--spelling counts.
i have to get the current value of newmassage in another program
From another program, or from another thread in the same program? It
matters--one is an issue of interprocess communication, the other merely
cross-thread synchronization.
come on duds help me out
While this may come as a shock to you, not everyone who reads this is
male. And if I strictly stick to your spelling, I do not take kindly to
your rather brash insult (there's a big difference between `dud' and
`dude').
Now, to answer your question, as I understand it:
The way to communicate a value between two different varies changes
based on the circumstances. Sometimes, the spawned thread is an
asynchronous computation, where the value at the end of the computation
is the one needed to be communicated. The simplest way here is probably
to use the Future mechanism (found in java.util.concurrent).
A case I have come across a few times is the need to synchronously query
the user for input and act on that input from a non-GUI thread.
Excluding the work of the event dispatch, the solution here is the
standard wait() and notify() idiom.
With the little information I have, it seems you might be better served
by the latter.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth