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++) {
process(roots[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
setDaemon(true);
}

// 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
s.so.


System.loadLibrary("libraryName");

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();
thread.start();

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


thread.start();