getting value form the run method of thread

F

focode

dear folks i have a situation before me i have to return a value from
the run method

the code is something like this

// lot of imports...
public class something implements Runnable {

Thread newThread;
String tempmassage,newmassage;


//somewhere in the program
newThread = new Thread(this);
newThread.start();

publiv void run()
{
// some code goes here to get message which is an object of
Message in javax.wireless.meassaging api
tempmassage = message.toString();
newmassage = tempmassage.concat(tempmassage).concat(" ");
}
}

i have to get the current value of newmassage in another program

come on duds help me out
 
J

Joshua Cranmer

focode said:
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.
 
L

Lew

Joshua said:
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.

Joshua said:
While we're nitpicking, Java coding conventions stipulate that classes
should begin with a capital letter.

Joshua said:
At this point your program becomes uncompilable, since statements are
not permitted outside of methods.

Joshua said:
It's `public'--spelling counts.

Joshua said:
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.

The usual approach in Swing work is to use SwingWorker to invoke non-GUI work
from the EDT, and EventQueue#invokeLater() to invoke GUI work from a non-EDT
thread.

Outside of Swing, there are Executor, Executors, Future and FutureTask.
 
R

Roedy Green

i have to get the current value of newmassage in another program

see the java.util.concurrent.Future interface and the methods that use
it

see http://mindprod.com/jgloss/thread.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top