🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #19274

Re: Are there automatic threads?

#19274
From: James Rudnicki
Date: Mon, 23 Feb 1998 00:00
43 lines
2157 bytes

Max Polk wrote in message ...
>Various books espouse something like this for a Java application:
>
>class Example extends Frame implements WindowListener
>{
>   public static void main (String args[])
>   {
>      Example myself = new Example ("This is an example.");
>      myself.addWindowListener (myself);
>      myself.show ();
>   }                 // myself GOES AWAY HERE!!!!!
>   ...
>}
>
>My question is that since main is a static method, it is called
>without an object of type Example yet in existence.  Therefore we
>create an Example object in a local variable called "myself".
>
>Since non-windowed applications end when you fall off the end of main,
>then why doesn't "myself" go out of scope at the end of main and
>become subject to possible garbage collection and object deallocation?


If you trace through show() you will see that it blocks for modal dialogs.
Something to keep in mind.

Here, the event listeners have copies of references to myself.  They would
keep the object myself from being collected.  A thread is automatically
created for dispatching messages.  It is named "EventQueue-0" or something
like that.  Put the line:
      System.out.println( Thread.currentThread().getName() );
in a event handler to see which thread executes what code.  The event queue
dispatch thread has a reference to the listeners, that in turn have a
reference to the Frame object.  So as long as the dispatch queue is running,
the listeners will not be collected.

  So the answer is yes, an thread is automatically created that keeps the
Frame object referenced and thereby keeps it from being collected.

bye,
Jim
Technical Details
Message-ID: <6crco4$gdb9@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <MPG.f5961f423886485989690@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,f971d56aac0c447c
X-Google-Attributes: gidf74b3,public
From: "James Rudnicki" <rudnicki@best.com>
Subject: Re: Are there automatic threads?
Date: 1998/02/23
Message-ID: <6crco4$gdb9@forums.borland.com>
X-Deja-AN: 327766010
References: <MPG.f5961f423886485989690@forums.borland.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Organization: None
Newsgroups: borland.public.jbuilder.java.language