目录

  • 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
Thread Synchronization

Thread Synchronization


}When multiple threads share anobject and it is modified by one or more of them, indeterminate results may occurunless access to the shared object is managed properly.

}The problem can be solved by giving only one thread at a time exclusive accessto code that manipulates the shared object.

During that time, other threads desiring to manipulate the object are kept waiting.

When the thread with exclusive access to the object finishes manipulating it, one ofthe threads that was waiting is allowed to proceed.

}This process, called thread synchronization, coordinates access to shared data by multiple concurrent threads.Ensures that each thread accessing a shared object excludes all other threads from doing so simultaneously—this is called mutual exclusion.

}A common way to perform synchronization is to use Java’s built-in monitors.

  ◦Every object has a monitor and a monitor lock (or intrinsiclock).Can be held by a maximum of only one thread at any time.

  ◦A thread must acquire the lock before proceeding with the operation.Other threads attempting to performan operation that requires the same lock will be blocked.

}To specify that a thread must holda monitor lock to execute a block of code, the code should be placed in a synchronized statement.

  ◦Said to be guarded by the monitor lock

}The synchronized statements are declared using the synchronized keyword:

–synchronized (object)
{
  
statements
} //end synchronized statement

}where object is the object whose monitor lock will be acquiredobjectis normally this if it’s the object in which the synchronized statement appears.

}When a synchronized statement finishes executing, the object’s monitor lock is released.

}Java also allows synchronized methods.




.