0% found this document useful (0 votes)
5 views4 pages

Java AWT and Multithreading Detailed Notes

The document provides detailed notes on Java Multithreading and AWT (Abstract Window Toolkit). It covers multithreading concepts, advantages, thread creation methods, thread life cycle, synchronization, and inter-thread communication, along with examples. Additionally, it explains AWT for GUI applications, its components, layout managers, event handling, and compares AWT with Swing.

Uploaded by

shuzosama511
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java AWT and Multithreading Detailed Notes

The document provides detailed notes on Java Multithreading and AWT (Abstract Window Toolkit). It covers multithreading concepts, advantages, thread creation methods, thread life cycle, synchronization, and inter-thread communication, along with examples. Additionally, it explains AWT for GUI applications, its components, layout managers, event handling, and compares AWT with Swing.

Uploaded by

shuzosama511
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA AWT & MULTITHREADING - DETAILED

NOTES
1. MULTITHREADING IN JAVA

Definition: Multithreading is the process of executing multiple threads simultaneously to maximize


CPU utilization. Each thread runs independently but shares the same memory. A thread is the
smallest unit of a process.

Advantages:
• Better CPU utilization
• Faster execution
• Saves memory (threads share the same space)
• Reduces response time
• Enables parallel processing

Creating Threads:
1. By implementing the Runnable interface
2. By extending the Thread class

Example (Runnable Interface):


class SampleThread implements Runnable {
public void run() { [Link]("Thread is running..."); }
}
public class MyThreadTest {
public static void main(String[] args) {
Thread t = new Thread(new SampleThread());
[Link]();
}
}

Example (Extending Thread):


class SampleThread extends Thread {
public void run() { [Link]("Thread running..."); }
}
public class MyThreadTest {
public static void main(String[] args) { new SampleThread().start(); }
}

Common Thread Methods:


| Method | Description |
|---------|--------------|
| start() | Starts a new thread |
| run() | Code executed by thread |
| sleep(ms) | Suspends thread for given time |
| join() | Waits for another thread to finish |
| isAlive() | Checks if thread is still running |
| currentThread() | Returns current thread |
| setPriority() | Sets thread priority (1–10) |
| getPriority() | Gets thread priority |

Thread Life Cycle:


1. New
2. Runnable
3. Running
4. Non-Runnable (Waiting/Sleeping/Blocked)
5. Terminated

Synchronization:
Ensures only one thread accesses a shared resource at a time using the synchronized keyword.

Inter-thread Communication:
wait(), notify(), and notifyAll() are used for communication between synchronized threads.

Example (Inter-thread Communication):


class Customer {
int amount = 10000;
synchronized void withdraw(int amt) {
if(amount < amt) { [Link]("Waiting for deposit..."); try { wait(); } catch(Exception e){} }
amount -= amt; [Link]("Withdraw complete. Balance: " + amount);
}
synchronized void deposit(int amt) {
amount += amt; [Link]("Deposit complete: " + amount); notify();
}
}

Thread Priority:
• Range: 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY)
• Default: 5 (NORM_PRIORITY)

Case Study Example:


Thread generates random number every 1 second. If even → print square, if odd → print cube.

import [Link].*;
class NumberGenerator extends Thread {
public void run() {
Random r = new Random();
while(true) { int n = [Link](100);
if(n%2==0) [Link]("Square: "+n*n);
else [Link]("Cube: "+n*n*n);
try{[Link](1000);}catch(Exception e){}
}
}
}
public class MultiThreadApp { public static void main(String[] args){ new NumberGenerator().start();
}}

2. JAVA AWT (ABSTRACT WINDOW TOOLKIT)

Definition: AWT is a GUI library in Java used to create window-based applications. It provides
classes for windows, buttons, labels, and more.
Package: [Link]
Type: Heavyweight (depends on OS).

AWT Hierarchy:
Object → Component → Container → (Window / Panel) → Frame, Applet

Advantages:
• Provides GUI capabilities
• Event-driven architecture
• Easy layout management
• Platform independent (via JVM)

Steps to Create an AWT GUI:


1. Import [Link] and [Link]
2. Create Frame
3. Add Components
4. Set Layout
5. Set Size & make visible

Common AWT Components:


| Component | Description |
|------------|--------------|
| Frame | Main window |
| Label | Displays text |
| Button | Triggers an action |
| TextField | Takes single-line text input |
| Checkbox | On/Off toggle |
| Choice | Dropdown list |
| List | Scrollable list of items |

Layout Managers:
FlowLayout - Arranges in a row
BorderLayout - Divides window into N, S, E, W, Center
GridLayout - Rows & Columns

Example (Basic AWT Program):


import [Link].*;
public class SimpleAWT {
public static void main(String[] args){
Frame f = new Frame("AWT Example");
Label l = new Label("Hello AWT!");
Button b = new Button("Click");
[Link](l); [Link](b);
[Link](new FlowLayout());
[Link](250,150);
[Link](true);
}
}

Event Handling:
AWT uses the Delegation Event Model.
• Event Source – Component generating event (e.g., Button)
• Event Object – Contains event details (e.g., ActionEvent)
• Event Listener – Interface to handle event (e.g., ActionListener)

Steps:
1. Implement Listener Interface
2. Override method (actionPerformed, itemStateChanged, etc.)
3. Register listener using addXXXListener()

Example (Button Event):


import [Link].*;
import [Link].*;
public class ButtonExample extends Frame implements ActionListener {
Button b = new Button("Click Me"); 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]("Button Clicked!"); }
public static void main(String[] args){ new ButtonExample(); }
}

Case Study: Simple Calculator


import [Link].*;
import [Link].*;
public class SimpleCalc extends Frame implements ActionListener {
TextField t1 = new TextField(5), t2 = new TextField(5);
Button add = new Button("Add"); Label l = new Label("Result:");
SimpleCalc(){ add(t1); add(t2); add(add); add(l); [Link](this); setLayout(new
FlowLayout()); setSize(250,150); setVisible(true); }
public void actionPerformed(ActionEvent e){ int a=[Link]([Link]()); int
b=[Link]([Link]()); [Link]("Result: "+(a+b)); }
public static void main(String[] args){ new SimpleCalc(); }
}

Difference Between AWT and Swing:


| Feature | AWT | Swing |
|----------|------|--------|
| Type | Heavyweight | Lightweight |
| Look | Native OS | Customizable |
| Package | [Link] | [Link] |
| Components | Basic | Richer set |

You might also like