1.
Thread Life Cycle Process
The thread life cycle in Java includes the following states:
1. New: Thread is created but not started.
2. Runnable: Thread is ready to execute and waiting for CPU allocation.
3. Running: Thread is executing its task.
4. Blocked/Waiting: Thread is waiting for a resource or a signal.
5. Terminated: Thread has completed its task or is stopped.
---
2. Methods for Creating Threads in Java
1. Extending the Thread Class:
A class extends Thread and overrides the run() method.
The thread starts by calling the start() method.
2. Implementing the Runnable Interface:
A class implements Runnable and defines the run() method.
A Thread object is created with the Runnable instance, and start() is called.
3. Using Callable and Future:
Introduced in Java 5 for threads that return a result or throw exceptions.
Uses ExecutorService to manage threads.
---
3. Thread Priority
Thread priority determines the execution precedence of threads.
Range: 1 (Thread.MIN_PRIORITY) to 10 (Thread.MAX_PRIORITY).
Default priority is 5 (Thread.NORM_PRIORITY).
Priorities serve as hints for thread scheduling, but the actual execution depends on the OS.
---
4. Synchronization
Synchronization ensures that only one thread accesses a critical section of code at a time to avoid data inc
Synchronized Methods: Locks the entire method.
Synchronized Blocks: Synchronizes only a specific block of code.
Prevents race conditions in multi-threaded environments.
---
5. Inter-Thread Communication
Inter-thread communication enables threads to coordinate their actions.
Uses wait(), notify(), and notifyAll() methods.
Threads use wait() to pause and release the lock, and notify() or notifyAll() to signal waiting threads.
---
6. Deadlock
A deadlock occurs when two or more threads are waiting for each other to release resources, resulting in a
Happens when threads hold and wait for resources in a circular dependency.
Prevented using proper resource management techniques like avoiding nested locks or using a timeout me
---
7. Applet Life Cycle Process
The applet life cycle includes the following stages:
1. init(): Initializes the applet.
2. start(): Begins or resumes execution.
3. paint(): Renders the content on the screen.
4. stop(): Pauses the applet when it is not visible.
5. destroy(): Cleans up resources before termination.
---
8. Event Listener Interfaces
Java provides several event listener interfaces for handling events:
1. ActionListener: Handles action events like button clicks.
2. KeyListener: Handles keyboard events such as key presses and releases.
3. MouseListener: Handles mouse events like clicks and movements.
4. FocusListener: Monitors focus gained or lost by components.
5. WindowListener: Handles window events like open, close, and minimize.
---
9. Key Events
Key events are generated when a user interacts with the keyboard.
KeyListener Interface:
keyPressed(): Triggered when a key is pressed.
keyReleased(): Triggered when a key is released.
keyTyped(): Triggered when a key is typed.
---
10. Mouse Events
Mouse events occur during user interaction with the mouse.
MouseListener Interface: Handles events like mouse clicks, presses, releases, entry, and exit.
MouseMotionListener Interface: Handles mouse movement and dragging events.
---
11. AWT Controls
AWT provides GUI components for user interaction:
1. Button: A clickable button.
2. Label: Displays static text.
3. TextField: Allows single-line text input.
4. TextArea: Allows multi-line text input.
5. Checkbox: A toggle control for true/false input.
6. Choice: A dropdown list.
7. List: Displays a list of items for selection.
---
12. AWT Layout Managers
Layout managers define how components are arranged in a container:
1. FlowLayout: Aligns components in a row, wrapping to the next row if needed.
2. BorderLayout: Divides the container into North, South, East, West, and Center regions.
3. GridLayout: Arranges components in a grid of equally sized cells.
4. CardLayout: Stacks components like cards; only one is visible at a time.
5. GridBagLayout: Flexible and complex layout manager for precise positioning.