0% found this document useful (0 votes)
4 views4 pages

Java Multithreading Exam Guide

The document provides comprehensive study material on Java multithreading, covering the Thread Life Cycle, the Runnable interface, and thread synchronization with examples. It explains the various states of a thread, important methods, and demonstrates the use of the Runnable interface through a sample program. Additionally, it illustrates thread synchronization using a banking example to prevent race conditions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Multithreading Exam Guide

The document provides comprehensive study material on Java multithreading, covering the Thread Life Cycle, the Runnable interface, and thread synchronization with examples. It explains the various states of a thread, important methods, and demonstrates the use of the Runnable interface through a sample program. Additionally, it illustrates thread synchronization using a banking example to prevent race conditions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Subject: Java Programming (Unit: Multithreading)

Comprehensive Study Material for Exam Preparation

Q1. Explain the Thread Life Cycle in Java with a neat diagram.

Definition: The Thread Life Cycle refers to the various states a thread transitions through
from its birth to its death. In Java, these states are defined by the [Link] enum.

States of a Thread:

1. New (Born): A thread is in this state when an instance of the Thread class is created but
start() is not yet called.
2. Runnable: After start() is called, the thread is ready to run and waiting for CPU
allocation.
3. Running: The thread is currently executing its run() method.
4. Blocked / Waiting: The thread is alive but waiting for a resource or another thread to
perform an action.
5. Timed Waiting: The thread is waiting for a specified time (e.g.,
[Link](1000)).
6. Terminated (Dead): The thread has finished its execution.

Important Methods:

• start(): Registers the thread with the scheduler.


• sleep(long ms): Suspends thread for a fixed time.
• join(): Makes the current thread wait until the called thread dies.
• yield(): Pauses the current thread to give others a chance.
Q2. What is the Runnable Interface? Provide a full
implementation example.

Definition: The Runnable interface is a functional interface used to define a task that can
be executed by a thread. It contains a single method public void run(). This approach
is preferred over extending the Thread class because it allows for multiple inheritance.

Example Program:

class MyTask implements Runnable {


private String name;
public MyTask(String name) { [Link] = name; }

@Override
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](name + " processing step: " + i);
try {
[Link](500); // State: Timed_Waiting
} catch (InterruptedException e) {
[Link](e);
}
}
[Link](name + " has COMPLETED.");
}
}

public class RunnableExamExample {


public static void main(String[] args) {
MyTask obj = new MyTask("Exam-Thread");
Thread t1 = new Thread(obj); // Thread creation
[Link](); // Transitions to Runnable
}
}

Output:

Exam-Thread processing step: 1


Exam-Thread processing step: 2
Exam-Thread processing step: 3
Exam-Thread has COMPLETED.
Q3. Explain Thread Synchronization with a real-world banking
example.

Definition: Synchronization is a mechanism that allows only one thread at a time to access a
shared resource. This is crucial to prevent Race Conditions, where data becomes inconsistent
due to simultaneous access.

Example Program (Synchronized Method):

class ATM {
private int balance = 1000;

// 'synchronized' keyword locks the method for one thread at a time


public synchronized void withdraw(String name, int amount) {
[Link](name + " is checking balance...");
if (balance >= amount) {
[Link](name + " - Balance sufficient. Withdrawing
" + amount);
try { [Link](1000); } catch (Exception e) {}
balance -= amount;
[Link](name + " - Transaction successful. New
Balance: " + balance);
} else {
[Link](name + " - Transaction failed. Insufficient
funds.");
}
}
}

public class SyncExamExample {


public static void main(String[] args) {
ATM sharedATM = new ATM();

Thread t1 = new Thread(() -> [Link]("Student-1",


700));
Thread t2 = new Thread(() -> [Link]("Student-2",
700));

[Link]();
[Link]();
}
}
Output:

Student-1 is checking balance...


Student-1 - Balance sufficient. Withdrawing 700
Student-1 - Transaction successful. New Balance: 300
Student-2 is checking balance...
Student-2 - Transaction failed. Insufficient funds.

Note: Without synchronized, both students could see 1000 balance at the same time and
withdraw 700, making the balance -400.

You might also like