What is a Thread in Java?
A Thread is a lightweight process that runs within another process or thread. It is an independent path of execution in an application. JVM gives each thread its own method-call stack. When JVM starts it creates one thread that calls the main() method
What is a method-call stack in the context of a Thread?
Each thread in Java gets its own method-call stack from the JVM. This stack stores local variables - method parameters and the sequence of method calls for that thread only
How many threads does JVM start with by default?
JVM starts with one thread when it begins execution. This thread calls the main() method of the class passed as an argument to the java command
MEMORY BOOSTER: Thread in Java
Thread = lightweight process = independent execution path = own method-call stack. JVM starts with one thread (main). Each thread runs independently and can execute concurrently with other threads
What is the priority of a Thread and how is it used in scheduling?
Every thread has a priority from 1 to 10. The scheduler uses this priority to decide which thread gets CPU time first. Higher priority threads get preference over lower priority threads. This is called pre-emptive scheduling
What is the default priority of a thread in Java?
The default priority of a new thread is 5 which is NORM_PRIORITY. A new thread inherits the priority of its parent thread
What are the three priority constants available for a Thread in Java?
MIN_PRIORITY = 1 (lowest). NORM_PRIORITY = 5 (default). MAX_PRIORITY = 10 (highest)
How do you set the priority of a thread in Java?
Use setPriority(int priority) method. You can pass Thread.MIN_PRIORITY - Thread.NORM_PRIORITY or Thread.MAX_PRIORITY as the argument
MEMORY BOOSTER: Thread priority
Priority range: 1 to 10. MIN=1 - NORM=5 - MAX=10. Default is 5. Higher priority = more CPU preference. Set with setPriority(). Get with getPriority(). New thread inherits parent priority
What is the purpose of join() method in Thread class?
join() makes the current thread wait for the thread it is called on to finish before continuing execution. It ensures one thread completes before another starts
What happens when a thread calls join() on another thread?
The current thread stops executing and waits until the thread on which join() was called finishes running. Only then does the current thread resume
How can we ensure Parent thread waits for Child thread to finish?
Call join() on the child thread from the parent thread. The parent thread will block until the child thread finishes execution
MEMORY BOOSTER: join()
join() = wait for that thread to finish before I continue. importantThread.join() = current thread pauses until importantThread completes. Use it to enforce thread execution order
What is the fundamental difference between wait() and sleep() methods?
wait() is an Object level method that releases the monitor lock and can be woken up by notify(). sleep() is a static Thread method that does not release the monitor and cannot be woken up by notify()
Does wait() release the monitor lock?
Yes - when a thread calls wait() it releases the monitor lock and enters waiting state. Another thread can then acquire the lock and call notify() to wake it up
Does sleep() release the monitor lock?
No - sleep() does not release any locks or monitors. The thread simply pauses for the specified time while holding all its locks
Where must wait() and notify() be called?
wait() and notify() must be called within the same synchronized block that is synchronized on the same monitor object
MEMORY BOOSTER: wait() vs sleep()
wait() = Object method + releases lock + needs notify() to wake up + must be in synchronized block. sleep() = Thread static method + keeps lock + wakes up after time + no notify needed. wait = cooperative. sleep = timed pause
Is it possible to call run() instead of start() on a thread in Java?
Yes but it will not run as a separate thread. It executes as a normal method call in the current thread with no context switching. Only start() creates a new thread
MEMORY BOOSTER: run() vs start()
start() = creates new thread + calls run() in that new thread. run() directly = no new thread + executes in calling thread like a regular method. Always use start() to actually start a new thread
How does Multi-threading work in Java?
Java allows one process to execute multiple threads in parallel. Each thread is created from a process and can run independently. Java provides methods like start() - notify() - wait() and sleep() to manage threads
What are the advantages of Multi-threading?
Improved performance - simultaneous access to multiple applications - reduced number of servers needed - and simplified coding for certain parallel tasks
What are the disadvantages of Multi-threading?
Difficult to debug - difficult to manage concurrency - difficult to convert single-threaded code to multi-threaded - and risk of deadlocks
MEMORY BOOSTER: Multi-threading pros and cons
Pros: performance + parallelism + fewer servers + simpler parallel code. Cons: hard to debug + concurrency issues + porting difficulty + deadlock risk. Multi-threading is powerful but complex - use only when needed