Java Reference
In-Depth Information
has more intimate knowledge of the application implementation. Therefore,
the developer can identify which parts of the application can be shared between
threads and which should be isolated into distinct threads. But if your applica-
tion is actually a collection of individual functions that have little in common,
you are probably better off implementing this application in separate processes
for each function and allowing the operating system to manage them for you.
Make sure the algorithm behind the application is multithreaded. It doesn't do
a lot of good to create a thread and then immediately wait for it to complete. For
example, if your application needs to collect all the required transactions from
the database before it can analyze them, you should probably not create the trans-
action collection function as a thread. On the other hand, if you can start to an-
alyze the transactions as they are collected, it might be appropriate to implement
the collection function as a separate thread from the analysis function.
G ARBAGE C OLLECTION
At any construction site, there are builders who build the structures as well as
garbage collectors. These people (or systems) move around the site and remove any
unused materials and equipment. They are important contributors to the efficient
and effective operation of the site.
Java's runtime environment works pretty much the same way. Constructors
create new objects, and a built-in garbage collection system removes these objects
when they are no longer needed. Java determines whether an object is needed using
a complex (and ever-improving) algorithm, but the most important attribute of an
object (at least from the garbage collector's perspective) is whether there are any
current references to this object. If an object exists in a Java runtime environment
but is not referenced by any other object, it will likely be garbage collected, that is,
removed from the memory of the currently executing program.
Java's runtime system includes a garbage collector thread. This low-priority
system thread runs periodically, scanning the modifiable portion of memory, in
order to detect any objects that are not currently referenced. These are marked for
deletion (actually they are identified as not referenced by the scanning process).
These objects are subsequently deleted by the garbage-collection process, and the
memory associated with them is made available for other objects.
When an object is deleted, it may be appropriate for that object to first clean up
certain resources before it is deleted. For example, an object that contains a data-
base connection should probably close that connection as the last thing it does be-
fore it is deleted.
Search WWH ::




Custom Search