The Types class determines any constants that are used to identify SQL types
UNIT 13: MULTITHREADED APPLICATION
13.7 Scheduling and Priority
Calling stop() is not sufficient to guarantee that a thread will end. This is a serious problem for Java-enabled Web browsers; there is no guarantee that an applet will terminate when stop() is invoked on a thread belonging to the applet.
The destroy() method is stronger than the stop() method. The destroy() method is designed to terminate the thread without resorting to the ThreadDeath mechanism. The destroy() method stops the thread immediately, without cleanup; any resources held by the thread are not released.
CAUTION: The
destroy()
method is not implemented in the Java Development Kit, in all versions up to and including 1.1. Calling this method results in aNoSuchMethodError
exception. Although there has been no comment about when this method will be implemented, it is likely that it will not become available until JavaSoft can implement it in a way that cleans up the dying thread's environment (locked monitors, pending I/O, and so on).13.7 Scheduling and Priority
Thread scheduling is defined as the mechanism used to determine how RUNNABLE threads are allocated CPU time (that is, when they actually get to execute for a period of time on the computer's CPU). In general, scheduling is a complex subject that uses terms such as pre- emptive, round-robin scheduling, priority-based scheduling, time-sliced, and so on.
A thread-scheduling mechanism is either preemptive or nonpreemptive. With preemptive scheduling, the thread scheduler preempts (pauses) a running thread to allow different threads to execute. A nonpreemptive scheduler never interrupts a running thread; instead, the nonpreemptive scheduler relies on the running thread to yield control of the CPU so that other threads can execute. Under nonpreemptive scheduling, other threads may starve (never get CPU time) if the running thread fails to yield.
Among thread schedulers classified as preemptive, there is a further classification. A pre- emptive scheduler can be either time-sliced or nontime-sliced. With time-sliced scheduling, the scheduler allocates a period of time for which each thread can use the CPU; when that amount of time has elapsed, the scheduler preempts the thread and switches to a different
thread. A nontime-sliced scheduler does not use elapsed time to determine when to preempt a thread; it uses other criteria such as priority or I/O status.
Different operating systems and thread packages implement a variety of scheduling policies.
But Java is intended to be platform independent. The correctness of a Java program should not depend on what platform the program is running on, so the designers of Java decided to isolate the programmer from most platform dependencies by providing a single guarantee about thread scheduling: The highest priority RUNNABLE thread is always selected for execution above lower priority threads. (When multiple threads have equally high priorities, only one of those threads is guaranteed to be executing.)
Java threads are guaranteed to be preemptive, but not time sliced. If a higher priority thread (higher than the current thread) becomes RUNNABLE, the scheduler preempts the current thread. However, if an equal or lower priority thread becomes RUNNABLE, there is no guarantee that the new thread will ever be allocated CPU time until it becomes the highest priority RUNNABLE thread.
NOTE: The current implementation of the Java VM uses different thread packages on different platforms; thus, the behavior of the Java thread scheduler varies slightly from platform to platform. It is best to check with your Java VM supplier to determine whether the VM uses native threads and whether the platform's native threads are time sliced (some native threading packages, most notably Solaris threads, are not time sliced).
Even though Java threads are not guaranteed to be time sliced, this should not be a problem for the majority of Java applications and applets. Java threads release control of the CPU when they become NOT RUNNABLE. If a thread is waiting for I/O, is sleeping, or is waiting to enter a monitor, the thread scheduler will select a different thread for execution.
Generally, only threads that perform intensive numerical analysis (without I/O) will be a problem. A thread would have to be coded like the following example to prevent other threads from running (and such a thread would starve other threads only on some platforms--on Windows NT, for example, other threads would still be allowed to run):
int i = 0;
while (true) { i++;
}
There are a variety of techniques you can implement to prevent one thread from consuming too much CPU time:
Don't write code such as
while (true) { }
. It is acceptable to have infinite loops--as long as what takes place inside the loop involves I/O,sleep()
, or interthread coordination (using thewait()
andnotify()
methods, discussed later in this chapter).Occasionally call
Thread.yield()
when performing operations that are CPU intensive. Theyield()
method allows the scheduler to spend time executing other threads.Lower the priority of CPU-intensive threads. Threads with a lower priority run only when the higher priority threads have nothing to do. For example, the Java garbage collector thread is a low priority thread. Garbage collection takes place when there are no higher priority threads that need the CPU; this way, garbage collection does not needlessly stall the system
.
By using these techniques, your applications and applets will be well behaved on any Java platform.13.7.1 Setting Thread Priority
public final static int MAX_PRIORITY = 10;
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final int getPriority();
public final void setPriority(int newPriority);
Every thread has a priority. When a thread is created, it inherits the priority of the thread that created it. The priority can be adjusted subsequently using the setPriority() method.
The priority of a thread can be obtained using getPriority().
There are three symbolic constants defined in the Thread class that represent the range of priority values: MIN_PRIORITY, NORM_PRIORITY, and MAX_PRIORITY. The priority values range from 1 to 10, in increasing priority. An exception is thrown if you attempt to set priority values outside this range.
13.7.2 Waking Up a Thread public void interrupt();
public static boolean interrupted();
public boolean isInterrupted();
To send a wake-up message to a thread, call interrupt() on its Thread object. Calling interrupt() causes an InterruptedException to be thrown in the thread and sets a flag that can be checked by the running thread using the interrupted() or isInterrupted() method.
Calling Thread.interrupted() checks the interrupt status of the current thread and resets the interrupt status to false (in versions before 1.1, the interrupt status is not reset). Calling isInterrupted() on a Thread object (which can be other than the current thread) checks the interrupt status of that thread but does not change the status.
The interrupt() method is useful in waking a thread from a blocking operation such as I/O, wait(), or an attempt to enter a synchronized method.
CAUTION: The
interrupt()
method is not fully implemented in the JDK 1.0.x. Callinginterrupt()
on a thread sets theinterrupted
flag but does not throw anInterruptedException
or end a blocking operation in the target thread; threads must checkinterrupted()
to determinewhether the thread has been interrupted.
The
interrupt()
method is fully implemented in the Java virtual machine, version 1.1.13.7.3 Suspending and Resuming Thread Execution public final void suspend();
public final void resume();
Sometimes, it is necessary to pause a running thread. You can do so using the suspend() method. Calling the suspend() method ensures that a thread will not be run. The resume() method reverses the suspend() operation.
A call to suspend() puts the thread in the NOT RUNNABLE state. However, calling resume() does not guarantee that the target thread will become RUNNABLE; other events may have caused the thread to be NOT RUNNABLE (or DEAD).
4.7.4 Putting a Thread to Sleep
public static void sleep(long millisecond);
public static void sleep(long millisecond, int nanosecond);
To pause the current thread for a specified period of time, call one of the varieties of the sleep() method. For example, Thread.sleep(500) pauses the current thread for half a second, during which time the thread is in the NOT RUNNABLE state. When the specified time expires, the current thread again becomes RUNNABLE.
CAUTION: In the JDK versions 1.0.x and 1.1, the sleep(int millisecond, int nanosecond) method uses the nanosecond parameter to round the millisecond parameter to the nearest millisecond. Sleeping is not yet supported in nanosecond granularity.