JAVA AWT & MULTITHREADING - COMPLETE
NOTES
1. MULTITHREADING IN JAVA
Definition: Multithreading is the process of executing multiple threads simultaneously to maximize
CPU utilization.
A Thread is a lightweight process that runs independently within a program.
Advantages:
• Better CPU utilization
• Faster execution
• Saves memory (threads share same space)
• Reduces response time
Creating Threads:
1. By implementing Runnable Interface
2. By extending Thread Class
Thread Methods:
start(), run(), sleep(), join(), isAlive(), currentThread(), setPriority(), getPriority()
Thread Life Cycle:
1. New → 2. Runnable → 3. Running → 4. Non-Runnable → 5. Terminated
Inter-thread Communication:
wait(), notify(), notifyAll() - used for communication between synchronized threads.
Thread Priority:
Range: 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), default = 5 (NORM_PRIORITY)
Synchronization:
Used to control access to shared resources.
Achieved using the 'synchronized' keyword.
Example (Short):
class MyThread extends Thread {
public void run() { [Link]("Running thread..."); }
public static void main(String[] args){ new MyThread().start(); }
}
2. JAVA AWT (Abstract Window Toolkit)
Definition: AWT is a part of Java used to create GUI (Graphical User Interface) applications.
Package: [Link]
Components: Frame, Button, Label, TextField, Checkbox, List, Choice, etc.
AWT Hierarchy:
Object → Component → Container → (Window / Panel) → Frame, Applet
Steps to Create AWT Program:
1. Import [Link] and [Link]
2. Create Frame
3. Add components
4. Set Layout
5. Set Size and Visibility
Important Components:
• Label - displays text
• Button - performs an action
• TextField - accepts text input
• Checkbox - toggle option
• Choice - dropdown list
• List - scrollable item list
Layouts:
FlowLayout, BorderLayout, GridLayout
Event Handling:
Uses Delegation Event Model
- Event Source: component generating event
- Event Object: holds event details
- Event Listener: interface that handles event
Example (Short):
import [Link].*;
import [Link].*;
public class ButtonExample extends Frame implements ActionListener {
Button b = new Button("Click"); Label l = new Label("Not Clicked");
ButtonExample(){ add(b); add(l); [Link](this); setLayout(new FlowLayout());
setSize(200,120); setVisible(true);}
public void actionPerformed(ActionEvent e){ [Link]("Clicked!"); }
public static void main(String[] args){ new ButtonExample(); }
}
Difference Between AWT and Swing:
| Feature | AWT | Swing |
|----------|------|--------|
| Type | Heavyweight | Lightweight |
| Look | Native OS | Custom Look |
| Package | [Link] | [Link] |