Java Reference
In-Depth Information
• To ensure that certain actions are taken before the VM exits, Java provides Run
time::addShutdownHook —it can safely execute arbitrary code before the JVM
exits.
• The finalize() method is an instance method, and finalizers act on instances.
There is no equivalent mechanism for finalizing a class.
• A finalizer is an instance method that takes no arguments and returns no value.
There can be only one finalizer per class, and it must be named finalize() .
• A finalizer can throw any kind of exception or error, but when a finalizer is
automatically invoked by the garbage collection subsystem, any exception or
error it throws is ignored and serves only to cause the finalizer method to
return.
Java's Support for Concurrency
The idea of a thread is that of a lightweight unit of execution—smaller than a pro‐
cess, but still capable of executing arbitrary Java code. The usual way that this is
implemented is for each thread to be a fully fledged unit of execution to the operat‐
ing system but to belong to a process, with the address space of the process being
shared between all threads comprising that process. This means that each thread
can be scheduled independently and has its own stack and program counter but
shares memory and objects with other threads in the same process.
The Java platform has had support for multithreaded programming from the very
first version. The platform exposes the ability to create new threads of execution to
the developer. This is usually as simple as:
Thread t = new Thread (() -> { System . out . println ( "Hello Thread" );});
t . start ();
This small piece of code creates and starts a new thread, which executes the body of
the lambda expression and then executes. For programmers coming from older ver‐
sions of Java, the lambda is effectively being converted to an instance of the Runna
ble interface before being passed to the Thread constructor.
The threading mechanism allows new threads to execute concurrently with the
original application thread and the threads that the JVM itself starts up for various
purposes.
For most implementations of the Java platform, application
threads have their access to the CPU controlled by the operat‐
ing system scheduler —a built-in part of the OS that is respon‐
sible for managing timeslices of processor time (and that will
not allow an application thread to exceed its allocated time).
In more recent versions of Java, an increasing trend towards runtime-managed con‐
currency has appeared. This is the idea that for many purposes explicit management
Search WWH ::




Custom Search