Monday, November 27, 2006

Listing the File System Roots

UNIX file systems have a single root, `/'. On Windows, each drive is a root. For example the C drive is represented by the root C:\.

File[] filerots = File.listRoots();
for (int i=0; i < filerots.length; i++) {

Catching all Errors and Exceptions

All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all unexpected conditions.

There are several scenarios where it is good practice to catch Throwable. For example, in a server application, the threads that handle requests should catch Throwable and relay any errors or exceptions to the client. Another scenario is a long-running thread that performs some background activity. Such threads should catch Throwable, log any errors or exceptions, and then continue functioning.

It is rarely good practice for a method in a library to catch Throwable. In general, errors and exceptions should not be masked from the caller.

This example demonstrates a long-running thread that catches Throwable and logs the exception.

    class BgThread extends Thread {
// Create a logger. For more information on the logging api's,
Logger logger = Logger.getLogger("com.mycompany.mypackage");

BgThread() {
// As a daemon thread, this thread won't prevent the application from exiting

// Set to true to shut down this thread
boolean stop = false;

public void run() {
while (!stop) {
try {
// Perform work here
} catch (Throwable t) {
// Log the exception and continue
logger.log(Level.SEVERE, "Unexception exception", t);

Finding When your Application Is About to Exit

When an application is terminated normally, the application first starts any registered shutdown threads, waits for them to complete and then finally exits.

Normal termination can be caused by a call to System.exit(), the completion of the last non-daemon thread, or the interruption of the application (control-C) by the user.

Abnormal termination (which does not cause the shutdown threads to be started) is caused some major fault in the Java virtual machine or native library.

    // Register a shutdown thread
Runtime.getRuntime().addShutdownHook(new Thread() {
// This method is called during shutdown
public void run() {
// Do shutdown work ...

Loading Native Code

On Windows, loadLibrary("s") loads s.dll. On Solaris, it loads


Thursday, November 23, 2006

Creating a Thread

When a thread is created, it must be permanently bound to an object with a run() method. When the thread is started, it will invoke the object's run() method.

There are two ways to create a thread.

The first is to declare a class that extends Thread. When the class is instantiated, the thread and object are created together and the object is automatically bound to the thread. By calling the object's start() method, the thread is started and immediately calls the object's run() method. Here is some code to demonstrate this method.
    // This class extends Thread
class BasicThread1 extends Thread {
// This method is called when the thread runs
public void run() {
    // Create and start the thread
Thread thread = new BasicThread1();

The second way is to create the thread and supply it an object with a run() method.
This object will be permanently associated with the thread. The object's run() method will be invoked when the thread is started.
This method of thread creation is useful if you want many threads sharing an object.
Here is an example that creates a Runnable object and then creates a thread with the object.

    class BasicThread2 implements Runnable {
// This method is called when the thread runs
public void run() {
    // Create the object with the run() method
Runnable runnable = new BasicThread2();

// Create the thread supplying it with the runnable object
Thread thread = new Thread(runnable);

// Start the thread


Tuesday, October 31, 2006

The Java Virtual Machine History

The Java virtual machine is the cornerstone of the Java and Java 2 platforms. It is the component of the technology responsible for its hardware- and operating system- independence, the small size of its compiled code, and its ability to protect users from malicious programs.

The Java virtual machine is an abstract computing machine. Like a real computing machine, it has an instruction set and manipulates various memory areas at run time. It is reasonably common to implement a programming language using a virtual machine; the best-known virtual machine may be the P-Code machine of UCSD Pascal.

The first prototype implementation of the Java virtual machine, done at Sun Microsystems, Inc., emulated the Java virtual machine instruction set in software hosted by a handheld device that resembled a contemporary Personal Digital Assistant (PDA). Sun's current Java virtual machine implementations, components of its JavaTM 2 SDK and JavaTM 2 Runtime Environment products, emulate the Java virtual machine on Win32 and Solaris hosts in much more sophisticated ways. However, the Java virtual machine does not assume any particular implementation technology, host hardware, or host operating system. It is not inherently interpreted, but can just as well be implemented by compiling its instruction set to that of a silicon CPU. It may also be implemented in microcode or directly in silicon.

The Java virtual machine knows nothing of the Java programming language, only of a particular binary format, the class file format. A class file contains Java virtual machine instructions (or bytecodes) and a symbol table, as well as other ancillary information.

For the sake of security, the Java virtual machine imposes strong format and structural constraints on the code in a class file. However, any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine. Attracted by a generally available, machine-independent platform, implementors of other languages are turning to the Java virtual machine as a delivery vehicle for their languages.

Taken From:

How to get Java Memory Heap Size

// Get current size of heap in bytes

long heapSize = Runtime.getRuntime().totalMemory();

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.

long heapMaxSize = Runtime.getRuntime().maxMemory();

// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.

long heapFreeSize = Runtime.getRuntime().freeMemory();