Oopsunit 3
Oopsunit 3
unit3
Threads
A CPU core is a physical processing unit within a CPU (Central Processing Unit) that
performs calculations and executes instructions.The number of cores in a CPU
determines how many tasks it can run at once.
Concurrency vs. Parallelism:
Concurrency: execute alternately (switching between tasks).
Parallelism: execute simultaneously (on multi-core processors).
What is Multithreading?
Multithreading is the ability of a program to execute multiple threads
concurrently, meaning it can perform several tasks seemingly at the same time,
improving performance and resource utilization.
Locking-
Intrinsic Locking: An intrinsic lock is a built-in locking mechanism provided by every
Java object. It is automatically associated with each object instance and used when
synchronizing critical sections using the synchronized keyword.
Extrinsic Locking: Extrinsic locks in Java, provide manual, explicit control over thread
synchronization. Extrinsic locks are external objects that allow for advanced features and
multiple condition variables for complex thread coordination. They also support fairness
policies, ensuring threads acquire locks in a first-come, first-served manner, reducing
starvation. While offering greater flexibility and control, extrinsic locks require careful
management of lock() and unlock() calls to avoid deadlocks or resource leaks, making
them more complex but powerful for scenarios requiring precise synchronization.
A deadlock happens when two or more threads are waiting for each other
forever, and none of them can continue.
Simple Example:
Thread A locks Resource 1 and waits for Resource 2.
Thread B locks Resource 2 and waits for Resource 1.
Now both threads are stuck, because each is holding something the other
needs — this is a deadlock.
2. Events
These are actions that happen on the component (like clicks, typing,
etc.)
3. Event Handlers
These are methods or classes that handle the events and define what
should happen when an event occurs.
Component classes
Component classes in Java are the building blocks used to create graphical user
interfaces (GUIs). These classes are part of the java.awt package and represent different
visual elements like buttons, labels, text fields, and checkboxes.
Each of these elements is called a "component" because it can be placed on a window or
panel.All component classes inherit from the main Component class, which provides basic
features like setting size, color, and position. By using these classes, we can build windows
that users can see and interact with, making Java programs user-friendly and interactive.
Constructor
Method
events
properties
Button
Constructor: Creates a new Button object and sets its label.
Properties: Characteristics of the button (size, color, text, etc.).
Methods: Used to set the properties of the button, like its size, color, and font.
Events: Handles what happens when a user interacts with the button, like clicking it.
In Java, the Container class is a special type of component that can hold other
components (like buttons, labels, text fields, etc.). It's part of the AWT (Abstract
Window Toolkit) and is a subclass of the Component class. Containers are essential in
building complex GUIs because they can group and organize various components.
GridBagLayout
GridBagLayout is a flexible layout manager that places components in a grid of
rows and columns, allowing components to span multiple cells and resize
dynamically based on the window size. It arranges components in a grid, but the rows
and columns can have different sizes. It supports alignment, spacing, padding, and resizing
very flexibly.
Swing Package
Swing is a part of Java's standard library used to create Graphical User
Interfaces (GUI).
It builds on AWT (Abstract Window Toolkit) but is more powerful and flexible.
An AWT Event in Java is an object that represents a user interaction with a GUI
component.
It tells the program what happened, like:
A button was clicked
A key was pressed
The mouse was moved
•Event is a broad term used across all programming languages and frameworks.
•AWT Event is specific to Java's AWT package and refers to events generated
by AWT components.
EVENT HANDLER
An event handler is the method or block of code that gets executed when an event
occurs.
WHAT IS EVENT HANDLING?
Event Handling is the mechanism that controls the event and decides what should happen if
an event occurs. This mechanism have the code which is known as event handler that is
executed when an event occurs.
The Delegation Event Model is the mechanism Java uses to handle events in GUI
applications (like AWT or Swing).
It is based on the idea of delegating the task of handling an event to a separate object
(called a listener).
Source Listener
A source is the GUI component •A listener is an object that waits for an
(like a button, checkbox, window, etc.) event and handles it.
that generates events .When a user •It is created by implementing a listener
interacts with the component, it creates interface
an event object.This object is then •The listener contains event handler
passed to the registered listener methods that are executed when the event
occurs.
BENEFITS
The event source (UI component) and the event handler (logic) are separated.
Makes the code more modular and easier to maintain.
To handle events, the source must be registered with a listener. Java provides
specific methods for registering listeners based on the type of event.
Java provides a variety of event classes and corresponding listener interfaces.
Below table demonstrates the most commonly used event classes and their
associated
listener interfaces:
Yes, you can call run()
directly, but... It won’t start
a new thread!
A thread-safe class is one that allows multiple
threads to access and modify its data and
methods concurrently without causing data
corruption, race conditions, or unexpected
behavior.