Java Programming - Module 5 (Multithreading) Answers
Q1: What is a thread in Java? Explain its advantages.
A thread is a lightweight sub-process, the smallest unit of processing. Threads allow multitasking in
Java.
Advantages:
1. Multitasking
2. Resource sharing
3. Faster execution
4. Better CPU usage
5. Improves performance
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
Q2: How do you create a thread in Java using the Thread class?
Extend Thread, override run(), and call start().
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
Q3: What is the difference between a process and a thread?
Process - Independent execution with own memory.
Thread - Lightweight, shares memory with other threads.
Threads are faster and more memory-efficient.
Q4: Define multithreading. How is it useful in Java applications?
Multithreading allows multiple tasks to run at the same time.
Benefits:
- Faster execution
- Better CPU usage
- Useful in games, servers, real-time apps.
Q5: What is synchronization in Java?
Used to prevent thread conflicts on shared resources.
Example:
class Counter {
int count = 0;
synchronized void increment() {
count++;
}
Q6: What is a thread pool in Java?
A group of reusable threads.
Example:
ExecutorService service = Executors.newFixedThreadPool(3);
service.execute(() -> System.out.println("Running"));
service.shutdown();
Q7: Explain the Runnable interface in Java.
Runnable is an interface to define task logic.
Example:
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running...");
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
Q8: What is the join() method in Java threads?
join() waits for a thread to finish.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Running...");
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.join(); // waits for t to finish
System.out.println("Main ends");
Q9: How does the wait() and notify() method work in Java?
wait() - Pauses thread and releases lock.
notify() - Wakes one waiting thread.
Example:
synchronized(obj) {
obj.wait(); // thread waits
obj.notify(); // wakes thread
Q10: What is a deadlock in multithreading?
Deadlock is when two threads wait for each other to release resources and never proceed.
Avoid by avoiding nested locks.
Q11: Explain the life cycle of a thread in Java.
States: New -> Runnable -> Running -> (Waiting/Blocked) -> Terminated
Q12: Compare thread creation using Thread class vs Runnable interface.
Thread: extends Thread (no multiple inheritance).
Runnable: implements Runnable (preferred, more flexible).
Q13: How does the synchronized keyword help in concurrency?
It prevents multiple threads from accessing critical sections.
Example: synchronized void method() { // code }
Q14: What is thread starvation? How can it be avoided?
Starvation occurs when a thread is never scheduled.
Avoid by using fair locks (ReentrantLock(true)).
Q15: Explain the concept of thread priority in Java.
Priority from 1 to 10 (MIN to MAX).
Higher priority may run before lower.
Q16: ExecutorService framework in Java.
Manages thread pools.
Example:
ExecutorService ex = Executors.newFixedThreadPool(2);
ex.execute(task); ex.shutdown();
Q17: Callable and Future in Java.
Callable returns result, Future gets result.
Example:
Future<Integer> f = exec.submit(() -> 10);
f.get();
Q18: Difference between synchronized methods and blocks.
Method: locks whole method.
Block: locks part of method. Better performance.
Q19: How does Java handle inter-thread communication?
Using wait(), notify(), notifyAll() from Object class.
Q20: Concurrency utilities in Java.
1. ExecutorService - Manages threads.
2. CountDownLatch - Waits for other threads.
Q21: Advantages of thread pool in Java.
Reuses threads, better performance, avoids resource issues.
Q22 to Q25: Programs using Thread, Runnable, Anonymous, Lambda.
Each example demonstrates different ways to create and run threads in Java.
Q26: Thread synchronization techniques.
1. synchronized method
2. synchronized block
3. static synchronized method
Q27: Inter-thread communication using wait() and notify().
Shared resource uses wait() to pause and notify() to resume.
Q28: Thread states with diagram.
New -> Runnable -> Running -> Waiting/Blocked -> Terminated
Q29: Advantages and Disadvantages of Threads.
Advantages: Faster, efficient, multitasking.
Disadvantages: Complex, risk of deadlock.
Q30: Best practices for multithreading.
Use ExecutorService, avoid Thread.sleep, prefer concurrent utilities, minimize shared data.