Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Threads - Thread Scheduling

  • execution of multiple threads in some order on a single CPU system is called scheduling
  • Java uses fixed-priority scheduling algorithms to decide which thread to execute
  • the thread with the highest priority runs first
  • if another thread with a higher priority is started, Java makes the lower priority thread wait
  • if more than one thread exists with the same priority, Java quickly switches between them in round-robin fashion BUT only if the operating system uses time-slicing (see below)

Priorities

  • it's possible to assign a thread priority
  • the Thread class contains three integer priority constants
    1. [ 1] MIN_PRIORITY
    2. [ 5] NORM_PRIORITY
    3. [10] MAX_PRIORITY
  • the default thread priority is NORM_PRIORITY
  • when a thread is created, it takes the priority of the thread which created it
  • you can check a threads priority using getPriority()
  • you can change a threads priority using setPriority()
  • if you change the priority on an executing thread to a lesser priority, it may stop executing as there may be another thread with a higher-priority (BB pg 259)

Actual Scheduling depends on the OS

  • the above act as a guide to scheduling however the actual implementation depends on the Operating System
  • most operating systems use one of two scheduling methods
    1. Preemptive scheduling
    2. Time slicing
  • In preemptive scheduling the highest priority thread continues to run until it dies, waits, or is preempted by a thread of higher priority
  • In time slicing a thread runs for a specific time and then enters the runnable state; at which point the scheduler decides wether to return to the thread or schedule a different thread (method used by Win95/NT)
  • DO NOT rely on thread priority as a guarantee that the highest priority thread will always be running; the operating system has the final say
  • priorities are used as guides to efficiency
  • priority manipulations CANNOT be used as a substitute for locking (see synchronization)

General Conventions for setting priorities (CPJ pg 16)

  • following represent the general conventions for setting thread priorities based on the type of activity the thread is involved in
    Range              Use
    -----   ----------------------------
      10    Crisis management
     7-9    Interactive, event-driven
     4-6    IO
     2-3    Background computation
      1     Run only if nothing else can
Overview Thread Class Runnable Interface Thread States Scheduling Ending a Thread
  Execution Synchronization Locking Protocols synchronized keyword wait() notify(), notifyAll()
  Thread Mechanics