Exiting threads

Discussion in 'Java' started by Ouabaine, Mar 9, 2008.

  1. Ouabaine

    Ouabaine Guest

    Hello all,

    In my game, I want to display an animation when I am loading the data
    (sounds and graphics). I plan to do this with another thread.
    My question : is the thread exited (thread killed, all the resources
    cleared) when you exit from the run() method? Or do I have to call a
    specific function to clean it?

    Thanks for your answers.

    Francois
    Ouabaine, Mar 9, 2008
    #1
    1. Advertising

  2. Ouabaine

    Chris Guest

    Ouabaine wrote:
    > Hello all,
    >
    > In my game, I want to display an animation when I am loading the data
    > (sounds and graphics). I plan to do this with another thread.
    > My question : is the thread exited (thread killed, all the resources
    > cleared) when you exit from the run() method? Or do I have to call a
    > specific function to clean it?
    >
    > Thanks for your answers.
    >
    > Francois


    When the run() method exits, the thread is dead and no longer consumes
    thread-related resources. The thread object itself, though, may persist
    if you still have a reference to it.

    For example:

    MyThread thread = new MyThread();
    thread.start();

    // Do other stuff here.
    // Thread runs on its own, eventually dies.

    // At this point, if the thread variable still points
    // to an instance of MyThread, then it and everything it contains
    // is still in memory.

    // Do this:

    thread = null;

    // to make it eligible for garbage collection
    Chris, Mar 9, 2008
    #2
    1. Advertising

  3. Ouabaine

    Lew Guest

    Chris wrote:
    > When the run() method exits, the thread is dead and no longer consumes
    > thread-related resources. The thread object itself, though, may persist
    > if you still have a reference to it.
    >
    > For example:
    >
    > MyThread thread = new MyThread();
    > thread.start();
    >
    > // Do other stuff here.
    > // Thread runs on its own, eventually dies.
    >
    > // At this point, if the thread variable still points
    > // to an instance of MyThread, then it and everything it contains
    > // is still in memory.
    >
    > // Do this:
    >
    > thread = null;


    Or better yet, just have the variable 'thread' pass out of scope.

    Typical use:

    public static void main( String [] args )
    {
    Runnable r = factory.createBasedOn( args );
    Thread t = new Thread( r );
    t.start();
    }

    't' goes out of scope after main() returns, so the variable reference
    vanishes, enabling GC for the Thread object when it's finished.

    This is idiomatically emphasized with

    public static void main( String [] args )
    {
    Runnable r = factory.createBasedOn( args );
    new Thread( r ).start();
    }

    --
    Lew
    Lew, Mar 9, 2008
    #3
  4. Ouabaine

    Ouabaine Guest

    Thanks to you both.

    Francois
    Ouabaine, Mar 10, 2008
    #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. jiten

    exiting from state machine

    jiten, Jun 22, 2005, in forum: VHDL
    Replies:
    3
    Views:
    486
  2. Anne
    Replies:
    0
    Views:
    357
  3. =?Utf-8?B?Qm9uag==?=

    problem with aspnet exiting

    =?Utf-8?B?Qm9uag==?=, Nov 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    337
  4. =?Utf-8?B?YmJkb2J1ZGR5?=

    Exiting an ASP.net application

    =?Utf-8?B?YmJkb2J1ZGR5?=, Jun 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    528
    samitha
    Sep 21, 2006
  5. Prince Al

    Exiting threads via signal

    Prince Al, Nov 11, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    134
    Wanna-Be Sys Admin
    Nov 12, 2009
Loading...

Share This Page