Java - Advanced Flashcards
Explain the concept of the Java Memory Model (JMM)
The Java Memory Model (JMM) defines the rules and guarantees for how threads interact with memory in a multi-threaded environment
How does the JMM ensure thread safety?
It ensures thread safety by providing synchronization and visibility guarantees.
Synchronization is achieved through the use of synchronized blocks or methods, which enforce exclusive access to shared resources.
Visibility guarantees are provided by the JMM’s rules for how changes made by one thread become visible to other threads.
This is accomplished through the use of volatile variables, locks, and happens-before relationships.
Concurrency
Concurrency refers to the ability of a system to handle multiple tasks concurrently, where tasks can start, run, and complete in overlapping time periods.
In Java, concurrency is achieved using threads, where multiple threads execute concurrently, sharing the same CPU resources.
An example of concurrency in Java is a web server handling multiple client requests simultaneously.
Parallelism
Parallelism, on the other hand, involves the simultaneous execution of multiple tasks to improve performance by utilizing multiple CPUs or CPU cores.
It is a form of concurrency where tasks are divided into smaller subtasks that can be executed independently.
An example of parallelism in Java is using the Java 8 Stream API to process a large collection of data in parallel using multiple threads.
How does garbage collection work in Java?
Garbage collection in Java automatically reclaims memory occupied by objects that are no longer reachable.
It frees developers from managing memory explicitly.
The Java Virtual Machine (JVM) performs garbage collection by identifying objects that are no longer referenced by any live threads.
The garbage collector identifies these objects and reclaims their memory.
Different types of garbage collectors?
Serial garbage collector
Parallel garbage collector
Concurrent Mark Sweep (CMS) garbage collector
Garbage-First (G1) garbage collector
Serial garbage collector
It uses a single thread for garbage collection and pauses all application threads during the collection process.
It is suitable for small applications with low memory requirements.
Parallel garbage collector
It uses multiple threads for garbage collection, which reduces the pause time. It is suitable for applications with larger heaps and multi-core systems.
Concurrent Mark Sweep (CMS) garbage collector
It minimizes pauses by performing most of the garbage collection work concurrently with the application’s execution. It is suitable for applications that require low pause times.
Garbage-First (G1) garbage collector
It divides the heap into multiple regions and performs concurrent garbage collection with high throughput and low pause times. It is suitable for large heaps and applications with strict pause time requirements.
What are the different ways to achieve inter-thread communication in Java?
Wait and notify
Blocking queues
Condition variables
Wait and notify
The wait() and notify() methods, along with synchronized blocks or methods, allow threads to wait for a condition to be satisfied and notify other threads when the condition changes.
For example, a producer-consumer scenario where a producer thread notifies the consumer thread when new data is available.
Blocking queues
The java.util.concurrent package provides blocking queue implementations like LinkedBlockingQueue and ArrayBlockingQueue.
These queues allow threads to block when trying to retrieve an element from an empty queue or insert an element into a full queue.
They handle the synchronization internally.
Condition variables
The java.util.concurrent.locks.Condition interface provides more flexible inter-thread communication compared to wait and notify.
It allows threads to wait for specific conditions to be met.
For example, a thread waiting for a queue to become non-empty can use a condition variable.
Explain the concept of the Java ClassLoader
The Java ClassLoader is responsible for loading classes into the JVM at runtime. It is a crucial part of the Java runtime environment.
The ClassLoader searches for classes in a specific order, following the delegation model. When a class is requested, the ClassLoader first checks if it has already been loaded. If not, it delegates the request to its parent ClassLoader. If the parent cannot find the class, the ClassLoader attempts to load the class itself.