Java Multithreading Notes
Multithreading in Java - Summary Notes
- Java has built-in support for multithreaded programming.
- A multithreaded program consists of multiple threads (paths of execution) running concurrently.
- Multithreading is a form of multitasking; it's more efficient because threads share the same process.
- Two types of multitasking:
- Process-based: multiple programs running at once.
- Thread-based: multiple tasks in a single program run simultaneously.
- Threads are lightweight and have lower overhead compared to processes.
- Java handles multithreading directly, which helps reduce idle CPU time (e.g., waiting for input or network
data).
The Java Thread Model - Summary Notes
- Java's runtime system relies on threads and is designed for asynchronous execution.
- Single-threaded systems use event loops that block the program during wait states, wasting CPU time.
- In Java, if one thread is blocked, others can continue to execute, improving efficiency.
- Works on both single-core (threads time-slice CPU) and multi-core systems (threads can run in parallel).
- Java's Fork/Join Framework enables parallel programming by splitting tasks and executing subtasks
concurrently.
- Thread states: Running, Ready, Suspended, Blocked, Terminated.
Thread Priorities - Summary Notes
- Each Java thread has a priority value which determines how threads are scheduled.
- Priorities affect scheduling decisions, not thread execution speed.
- Context switch rules:
1. Voluntary: thread yields/sleeps/blocks, highest-priority ready thread runs.
2. Preemption: higher-priority thread preempts lower-priority one.
- Equal priority threads may behave differently based on OS (round-robin or cooperative).
- Portability issues can arise due to OS-specific scheduling behavior.
Synchronization - Summary Notes
- Needed to avoid data conflicts in shared resources.
- Java uses monitors to allow only one thread to access a synchronized block at a time.
Page 1
Java Multithreading Notes
- Every object in Java has an implicit monitor; synchronized methods lock the object until execution
completes.
- This ensures thread-safe operations in multithreaded environments.
Messaging Between Threads - Summary Notes
- Threads communicate using synchronized methods and wait/notify mechanisms.
- A thread can wait inside a synchronized method until another thread notifies it.
- Lightweight alternative to OS-based inter-thread communication.
Thread Class and Runnable Interface - Summary Notes
- Java threads are implemented using the Thread class and Runnable interface.
- Thread represents a thread of execution.
- Runnable is implemented by any class whose instances are intended to be executed by a thread.
- Common Thread methods:
- getName(): gets the thread's name.
- getPriority(): gets the thread's priority.
- isAlive(): checks if thread is running.
- join(): waits for thread to finish.
- run(): contains the thread's execution code.
- sleep(): pauses execution temporarily.
- start(): starts the thread and invokes run() in a new call stack.
Page 2