getting value form the run method of thread

Discussion in 'Java' started by focode, Jul 11, 2009.

  1. focode

    focode Guest

    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
    focode, Jul 11, 2009
    #1
    1. Advertising

  2. 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
    Joshua Cranmer, Jul 11, 2009
    #2
    1. Advertising

  3. focode

    Lew Guest

    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
    Lew, Jul 11, 2009
    #3
  4. focode

    Roedy Green Guest

    On Sat, 11 Jul 2009 04:02:59 -0700 (PDT), focode
    <> 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.
    Roedy Green, Jul 12, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    2,032
    Roland
    Jun 16, 2005
  2. wanwan
    Replies:
    3
    Views:
    1,038
    James McGill
    Feb 24, 2006
  3. Jeffrey Barish
    Replies:
    0
    Views:
    299
    Jeffrey Barish
    May 28, 2004
  4. Replies:
    5
    Views:
    1,593
    Roedy Green
    Jun 20, 2008
  5. KevinSimonson
    Replies:
    7
    Views:
    341
    Screamin Lord Byron
    Oct 18, 2010
Loading...

Share This Page