/
'. 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]);
}
/
'. On Windows, each drive is a root. For example the C drive is represented by the root C:\
.<
filerots.length; i++) {Throwable
. By catching Throwable
, it is possible to handle all unexpected conditions.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.Throwable
. In general, errors and exceptions should not be masked from the caller.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);
}
}
}
}
System.exit()
, the completion of the last non-daemon thread, or the interruption of the application (control-C) by the user.// Register a shutdown thread
Runtime.getRuntime().addShutdownHook(new Thread() {
// This method is called during shutdown
public void run() {
// Do shutdown work ...
}
});
On Windows,loadLibrary("s")
loadss.dll
. On Solaris, it loadss.so
.
System.loadLibrary("libraryName");
run()
method. When the thread is started, it will invoke the object's run()
method.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();
run()
method.run()
method will be invoked when the thread is started.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();