Linux web host - Chapter 15 Multithreading 843 thread gets interrupted by
Chapter 15 Multithreading 843 thread gets interrupted by a higher priority thread) before that thread s peers get a chance to execute. With timeslicing, each thread receives a brief burst of processor time called a quantum during which that thread can execute. At the completion of the quantum, even if that thread has not finished executing, the operating system takes the processor away from that thread and gives it to the next thread of equal priority (if one is available). The job of the Java scheduler is to keep the highest priority thread running at all times, and if timeslicing is available, to ensure that several equally high-priority threads each execute for a quantum in round-robin fashion (i.e., these threads can be timesliced). Figure 15.2 illustrates Java s multilevel priority queue for threads. In the figure, threads A and B each execute for a quantum in round-robin fashion until both threads complete execution. Next, thread C runs to completion. Then, threads D, E and F each execute for a quantum in round-robin fashion until they all complete execution. This process continues until all threads run to completion. Note that new higher-priority threads could postpone possibly indefinitely the execution of lower priority threads. Such indefinite postponement often is referred to more colorfully as starvation. A thread s priority can be adjusted with method setPriority, which takes an int argument. If the argument is not in the range 1 through 10, setPriority throws an IllegalArgumentException. Method getPriority returns the thread s priority. A thread can call the yield method to give other threads a chance to execute. Actually, whenever a higher priority thread becomes ready, the operating system preempts the current thread. So, a thread cannot yield to a higher priority thread, because the first thread would have been preempted when the higher priority thread became ready. Similarly, yield always allows the highest priority-ready thread to run, so if only lower priority threads are ready at the time of a yield call, the current thread will be the highest priority thread and will continue executing. Therefore, a thread yields to give threads of an equal priority a chance to run. On a timesliced system this is unnecessary, because threads of equal priority will each execute for their quantum (or until they lose the processor for some other reason), and other threads of equal priority will execute in round- robin fashion. Thus yield is appropriate for nontimesliced systems in which a thread would ordinarily run to completion before another thread of equal priority would have an opportunity to run. Performance Tip 15.3 On nontimesliced systems, cooperating threads of equal priority should periodically call yield to enable their peers to proceed smoothly. Portability Tip 15.2 Java applets and applications should be programmed to work on all Java platforms to realize Java s goal of true portability. When designing applets and applications that use threads, you must consider the threading capabilities of all the platforms on which the applets and applications will execute. A thread executes unless it dies, it becomes blocked by the operating system for input/ output (or some other reason), it calls sleep, it calls wait, it calls yield, it is preempted by a thread of higher priority or its quantum expires. A thread with a higher priority than the running thread can become ready (and hence preempt the running thread) if a sleeping thread finishes sleeping, if I/O completes for a thread waiting for that I/O or if either notifyor notifyAll is called on a thread that has called wait.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.