Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > getting value form the run method of thread

Reply
Thread Tools

getting value form the run method of thread

 
 
focode
Guest
Posts: n/a
 
      07-11-2009
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

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      07-11-2009
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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-11-2009
focode wrote:
>> dear folks i have a situation before me i have to return a value from
>> the run method


Joshua Cranmer wrote:
> 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.


focode wrote:
>> // lot of imports...
>> public class something implements Runnable {


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


focode wrote:
>> //somewhere in the program
>> newThread = new Thread(this);
>> newThread.start();


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


focode wrote:
>> publiv void run()


Joshua Cranmer wrote:
> It's `public'--spelling counts.


focode wrote:
>> come on duds help me out


Joshua Cranmer wrote:
> 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.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-12-2009
On Sat, 11 Jul 2009 04:02:59 -0700 (PDT), focode
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Code in a Separate Thread Have to be in a <run()> or Called by a <run()>? KevinSimonson Java 7 10-18-2010 06:39 PM
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
why we should call Thread.start(),not directly call Thread.run()? junzhang1983@gmail.com Java 5 06-20-2008 03:12 PM
Loop Form - extract display 'value' as well as form value? ASP General 2 02-20-2006 09:36 PM
Terminating program run from thread (not just the thread) Jeffrey Barish Python 0 05-28-2004 02:02 AM



Advertisments