Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Synchronization in Java

Multi-threaded programs may often come to a situation where multiple


threads try to access the same resources and finally produce erroneous
and unforeseen results.
Why use Java Synchronization?

• Java Synchronization is used to make sure by some synchronization


method that only one thread can access the resource at a given point
in time.
Java Synchronized Blocks

• Java provides a way of creating threads and synchronizing their tasks


using synchronized blocks.
• A synchronized block in Java is synchronized on some object.
• All synchronized blocks synchronize on the same object and can only
have one thread executed inside them at a time.
• All other threads attempting to enter the synchronized block are
blocked until the thread inside the synchronized block exits the block.
General Form of Synchronized Block

• synchronized(sync_object)
•{
• // Access shared variables and other
• // shared resources
•}
Types of Synchronization

There are two synchronizations in Java mentioned below:


Process Synchronization :Process Synchronization is a technique used to
coordinate the execution of multiple processes. It ensures that the shared
resources are safe and in order.

Thread Synchronization: Thread Synchronization is used to coordinate and


ordering of the execution of the threads in a multi-threaded program.
There are two types of thread synchronization are mentioned below:
• Mutual Exclusive
• Cooperation (Inter-thread communication in Java)
Mutual Exclusive

• Mutual Exclusive helps keep threads from interfering with one


another while sharing data. There are three types of Mutual Exclusive
mentioned below:
• Synchronized method.
• Synchronized block.
• Static synchronization.
Inter-thread Communication in Java

• Inter-thread communication in Java is a mechanism in which a thread


is paused running in its critical section and another thread is allowed
to enter (or lock) in the same critical section to be executed.
• Inter-thread communication is also known as Cooperation in Java.
How Java multi-threading tackles this problem?

Java uses three methods, namely, wait(), notify(), and notifyAll(). All
these methods belong to object class as final so that all classes have
them. They must be used within a synchronized block only.
• wait(): It tells the calling thread to give up the lock and go to sleep
until some other thread enters the same monitor and calls notify().
• notify(): It wakes up one single thread called wait() on the same
object. It should be noted that calling notify() does not give up a lock
on a resource.
• notifyAll(): It wakes up all the threads called wait() on the same
object.
Event Handling in Java
An event can be defined as changing the state of an object or behavior by performing
actions. Actions can be a button click, cursor movement, keypress through keyboard or
page scrolling, etc.
The java.awt.event package can be used to provide various event classes.
Classification of Events
• Foreground Events :Foreground events are the events that require user interaction to
generate, i.e., foreground events are generated due to interaction by the user on
components in Graphic User Interface (GUI). Interactions are nothing but clicking on a
button, scrolling the scroll bar, cursor moments, etc.
• Background Events : Events that don’t require interactions of users to generate are
known as background events. Examples of these events are operating system
failures/interrupts, operation completion, etc
Event Handling
It is a mechanism to control the events and to decide what should
happen after an event occur. To handle the events, Java follows
the Delegation Event model.
Delegation Event model.

• Source: Events are generated from the source. There are various
sources like buttons, checkboxes, list, menu-item, choice, scrollbar,
text components, windows, etc., to generate events.
• Listeners: Listeners are used for handling the events generated from
the source. Each of these listeners represents interfaces that are
responsible for handling events.
To perform Event Handling, we need to register the source with the
listener.
• Registering the Source With Listener
Syntax: addTypeListener()

where Type represents the type of event.


Example 1: For KeyEvent we use addKeyListener() to register.
Example 2:that For ActionEvent we use addActionListener() to register.

You might also like