目录

  • 1 Introduction
    • 1.1 Introduction
    • 1.2 Machine Lang, Assembly lang and High-level lang
    • 1.3 History of Java
    • 1.4 Characteristics of Java
    • 1.5 Typical Java Development Environment
    • 1.6 Introduction to Java Application
  • 2 Classes and Objects
    • 2.1 Primitive Types vs. Reference Types
    • 2.2 Classes and Objects
      • 2.2.1 Declaring a Class
      • 2.2.2 Local and Instance Variables
      • 2.2.3 Methods: A Deeper Look
      • 2.2.4 Constructors
  • 3 Control Statements
    • 3.1 Control Structures
    • 3.2 if Selection
    • 3.3 while Repetition
    • 3.4 for Repetition
    • 3.5 do…while repetition
    • 3.6 switch multiple-selection
  • 4 Arrays and Collections
    • 4.1 Arrays
    • 4.2 ArrayList
    • 4.3 Set
    • 4.4 Generic Programming
  • 5 Object-Oriented Programming: Inheritance
    • 5.1 Inheritance
    • 5.2 Superclasses and Subclasses
    • 5.3 Constructors in Subclasses
  • 6 Object-Oriented Programming: Polymorphism
    • 6.1 Polymorphism
    • 6.2 Polymorphic Behavior
    • 6.3 Enable and Disable Polymorphism
  • 7 Exception Handling
    • 7.1 Exceptions
    • 7.2 Error-Handling
  • 8 Java I/O
    • 8.1 Java I/O Streams
    • 8.2 Decorator Design Pattern
  • 9 GUI Components
    • 9.1 AWT and SWING
    • 9.2 Event Model
  • 10 Multithreading
    • 10.1 Life Cycle of a Thread
    • 10.2 Thread Synchronization
    • 10.3 Producer/Consumer
Life Cycle of a Thread

Introduction



}Operating systems onsingle-processor computers create the illusion of concurrent execution by rapidly switching between activities, but on such computers only a single instruction can execute at once.

}Java makes concurrency available toyou through the language and APIs.

}You specify that an application contains separate threads of execution each thread has its own method-call stack and program countercan execute concurrently with other threads while sharing application wide resources such as memory with them.

}This capability is called multithreading.

Thread States: Life Cycle of aThread


}At any time, a thread is said to bein one of several thread states.

}A new thread begins its life cycle in the new state.

}Remains there until started, which places it in the runnable state—considered to be executing its task.

}runnable thread can transition to the waiting statewhile it waits for another thread to perform a task.

Transitions back to the runnable state only when another thread notifies it to continue executing.

}runnable thread can enter the timedwaiting state for a specified interval oftime.

Transitions back to the runnable state when that time interval expires or when the event it’s waiting for occurs.

Cannotuse a processor, even if one is available.

}sleeping thread remains in the timed waiting statefor a designated period of time (called a sleep interval), after which it returns to the runnable state.

}runnable thread transitions to the blocked statewhen it attempts to perform a task that cannot be completed immediately and it must temporarily wait until that task completes.

}runnable thread enters the terminated state when it successfully completes its task or otherwise terminates (perhaps due toan error).




}At the operating-system level, Java’s runnable state typically encompasses two separate states (Fig. 26.2).

}When a thread first transitions tothe runnable state from the new state, it is inthereadystate.

}ready thread enters the running state(i.e., begins executing) when the operating system assigns it to a processor—also known as dispatching the thread.

}Typically, each thread is given a quantum or timeslice in which to perform its task.

}The process that an operating system uses to determine which thread to dispatch is called thread scheduling.



Thread Priorities and ThreadScheduling

}Every Java thread has a thread priority that helps determine the order inwhich threads are scheduled.

}Java priorities range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10).

}By default, every thread is givenpriority NORM_PRIORITY (a constant of 5). Each new thread inherits the priority of the thread that created it.

.



.