Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Exiting threads

Reply
Thread Tools

Exiting threads

 
 
Ouabaine
Guest
Posts: n/a
 
      03-09-2008
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


 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      03-09-2008
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

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-09-2008
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
 
Reply With Quote
 
Ouabaine
Guest
Posts: n/a
 
      03-10-2008
Thanks to you both.

Francois


 
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
Exiting threads via signal Prince Al Perl Misc 6 11-12-2009 11:05 PM
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments