Unit 4: Multithreading and Generic Programming
Multitasking vs Multithreading
Multitasking means the operating system runs multiple programs at the same time.
Example:
- Music player
- Web browser
- Text editor (All running together)
Types:
- Process-based multitasking
- Each task is a separate process
Multithreading means a single program performs multiple tasks at the same time using threads.
Example:
In a browser:
- One thread loads page
- Another plays video
- Another downloads file
Differences Between Multitasking and Multithreading
| Aspect | Multitasking | Multithreading |
|---|---|---|
| Unit of execution | Process | Thread |
| Memory | Separate memory | Shared memory |
| Speed | Slower | Faster |
| Communication | Difficult | Easy |
| Example | Multiple applications | Multiple tasks in one app |
Thread Life Cycle
A thread goes through different states during execution.
Thread States:
- New - Thread object created
- Runnable - Ready to run
- Running - CPU executing thread
- Blocked / Waiting - Waiting for resource or signal
- Terminated (Dead) - Execution finished
Diagram (Text Representation): New → Runnable → Running → Waiting → Runnable → Dead
Creating Threads in Java
Java provides two main ways to create threads.
Method 1: Extending Thread Class
Important:
start()creates a new threadrun()contains thread logic
Method 2: Implementing Runnable Interface
Advantage:
- Supports multiple inheritance
- More flexible
Synchronizing Threads
Synchronization prevents multiple threads from accessing shared data at the same time.
Problem Without Synchronization:
- Data inconsistency
- Wrong output
Synchronized Method
Synchronized Block
Benefit:
- Thread safety
- Prevents race condition
Inter-Thread Communication
Threads communicate with each other using:
wait()- notify()
- notifyAll()
Example: Producer-Consumer problem
Use:
- Efficient resource sharing
- Avoids busy waiting
Daemon Threads
Daemon threads run in background to support other threads.
Example:
- Garbage Collector
- Auto-save
Characteristics:
- Runs in background
- Automatically stops when user threads finish
Example:
Thread Groups
A thread group is a collection of threads.
Purpose:
- Manage multiple threads together
- Set priorities
- Interrupt all threads at once
Example:
ThreadGroup Methods:
| Method | Use |
|---|---|
| activeCount() | Count active threads |
| interrupt() | Stop all threads |
| getName() | Group name |
Conclusion
Multithreading in Java allows:
- Faster execution
- Efficient CPU usage
- Responsive applications
Understanding thread creation, life cycle, synchronization, communication, daemon threads, and thread groups is essential for Java exams, interviews, and real-world systems.
Introduction to Generic Programming
Generic Programming in Java allows us to write a class or method that works with different data types while maintaining type safety.
Simple meaning: Write code once, use it for many data types (Integer, String, Double, etc.).
Without Generics (Problem):
With Generics (Solution):
Benefits:
- Compile-time error checking
- No type casting
- Cleaner and safer code
Generic Classes
A generic class is a class that can work with any data type, specified when creating its object.
Syntax:
-
T→ Type parameter (can be any name)
Example of Generic Class
Using Generic Class:
Advantages:
- Same class works for multiple data types
- Strong type checking
- Code reusability
Generic Methods
A generic method is a method that introduces its own type parameter, independent of the class.
Syntax: < T > returnType methodName(T value)
Example of Generic Method
Key Points:
- Can be static or non-static
- Can be inside normal or generic class
- Type is decided at method call
Bounded Types (Bounded Type Parameters)
Bounded types restrict the type parameter to a specific class or its subclasses.
Simple meaning:
You can say:
“Only numbers are allowed, not strings.”
Upper Bounded Type (extends)
Used to restrict type to a class and its subclasses.
Example:
Allowed Types:
- Integer
- Float
- Double
Not Allowed:
- String
- Character
Example Program:
Multiple Bounds
A type parameter can extend one class and multiple interfaces.
Restrictions and Limitations of Generics
Generics have some important limitations.
Cannot Use Primitive Types
Not allowed:
Allowed:
Cannot Create Object of Type Parameter
Not allowed:
Static Members Cannot Use Type Parameter
Not allowed:
Reason: Static members belong to class, not object.
Cannot Use instanceof with Generic Types
Not allowed: if(obj instanceof T)
Generic Arrays Are Not Allowed
❌ Not allowed:
Type Erasure
At runtime, Java removes generic type information.
Example:
Both become:
Difference: Generic Class vs Generic Method
| Feature | Generic Class | Generic Method |
|---|---|---|
| Scope | Whole class | Only method |
| Type parameter | Defined at class level | Defined at method level |
| Usage | Object creation | Method call |
Conclusion
Generic Programming in Java:
- Improves type safety
- Reduces runtime errors
- Enhances code reusability
- Is widely used in Java collections
Understanding generic classes, generic methods, bounded types, and their limitations is essential for Java programming, exams, and interviews.