m5 DSDV Notes
m5 DSDV Notes
* Java uses threads to allow multiple tasks to run simultaneously within a single program.
* Threads are lightweight processes that share the same memory but operate independently.
* The Java thread model helps in multitasking and improves application performance.
* The Thread class and Runnable interface are used to create and manage threads.
* Java provides built-in support for thread management, making it easier to handle concurrency.
* Thread management helps avoid issues like deadlocks and resource conflicts
* The main thread is the first thread that executes when a Java program starts.
* It’s created automatically and is the parent of all other threads in the program.
* Other threads can be created from the main thread to perform specific tasks.
* The main thread can be controlled using methods like setName(), getName(), and getPriority().
* If the main thread ends, all other non-daemon threads in the program will also terminate.
* Understanding the main thread is essential for coordinating other threads effectively.
Creating a Thread
* A thread can be created by extending the Thread class and overriding its run() method.
* Another way is by implementing the Runnable interface and defining the run() method.
* Threads created by extending Thread have the benefit of direct access to Thread methods.
* Implementing Runnable is more flexible as it allows a class to inherit from other classes.
* Proper synchronization is needed to avoid conflicts when multiple threads access shared
resources.
* Using multiple threads effectively can greatly enhance program speed and responsiveness.
* It’s useful for verifying whether a thread has completed its task.
* join() makes one thread wait until another thread finishes execution.
* This method helps coordinate tasks by ensuring a thread doesn’t proceed until another is
complete.
* Using isAlive() and join() ensures better control over thread execution.
* Enums Definition: Enums are lists of named, constant values, used to represent fixed sets of
values.
* Purpose: They make code easier to read and reduce mistakes by limiting values to predefined
options.
* Added Features: Enums can have fields (data), methods (actions), and constructors (initial setup).
* Keyword: The enum keyword defines enums, like enum Days { MONDAY, TUESDAY, ... }.
* Common Uses: Enums often represent things like days, directions, or states (e.g., success or
failure).
* Benefit: They make code clearer and simpler to maintain, especially in big programs.
Enumeration Fundamentals
* Constant Nature: Enum values are final (unchangeable) and static (shared).
* Implements Only: Enums can use interfaces but can't extend other classes, as they already extend
java.lang.Enum.
* Custom Behavior: Enums can have extra methods, data, and a constructor.
* Type Safety: Enums prevent invalid values by only allowing predefined options.
* Switch Compatibility: The switch statement works well with enums, making code decisions simpler.
* Thread Safety: Enums are safe to use in programs with multiple threads.
* Structured Values: They add organization to programs with a fixed set of values.
* values(): Returns an array of all enum values, which is useful for looping through them.
* Dynamic Use: These methods make working with enums more flexible.
* Conversion: valueOf() is helpful to convert user inputs or file data into enum values.
* Benefit: They make enums more accessible and structured for managing values in Java.
Character Wrapper:
* Useful in collections.
Boolean Wrapper:
Autoboxing:
Unboxing:
Autoboxing/Unboxing in Expressions:
Autoboxing/Unboxing in Methods
* Autoboxing allows you to pass primitive values to methods that expect objects, while unboxing
does the reverse.
* Example:
void addInteger(Integer x) {
System.out.println(x + 10);
addInteger(5);
* This feature improves compatibility and code readability by allowing methods to accept both
objects and primitives seamlessly.
Example
Boolean flag = true; // Autoboxed
Example
* This feature ensures that even non-numeric types (like boolean and char) can be used flexibly in
collections and methods.