0% found this document useful (0 votes)
8 views5 pages

Multithreading Java Rohit Gupta

The document explains multithreading in Java, detailing its implementation through extending the Thread class or implementing the Runnable interface. It outlines the thread lifecycle stages, differences between wait() and suspend(), and the purpose of synchronization in threads. Additionally, it highlights the benefits of multithreading, including improved performance and efficient resource utilization.

Uploaded by

luthra.karan6744
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)
8 views5 pages

Multithreading Java Rohit Gupta

The document explains multithreading in Java, detailing its implementation through extending the Thread class or implementing the Runnable interface. It outlines the thread lifecycle stages, differences between wait() and suspend(), and the purpose of synchronization in threads. Additionally, it highlights the benefits of multithreading, including improved performance and efficient resource utilization.

Uploaded by

luthra.karan6744
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
You are on page 1/ 5

Name: Rohit Gupta

Q1. What is Multithreading and How is it Implemented?

Ans: Multithreading in Java refers to executing multiple threads at the same time. All threads share

the same memory space.

In Java, we can implement multithreading in two ways:

1. By extending the Thread class:

class MyThread extends Thread {

public void run() {

System.out.println("Thread is called");

class Main {

public static void main(String[] args) {

MyThread t1 = new MyThread();

t1.start();

2. By implementing the Runnable interface:

class MyThread2 implements Runnable {

public void run() {

System.out.println("Custom MyThread2 implementing Runnable called");


}

class Main2 {

public static void main(String[] args) {

MyThread2 t1 = new MyThread2();

Thread t2 = new Thread(t1);

t2.start();

Q2. Brief Description of Lifecycle in Multithreading

Ans: The stages of a thread lifecycle are:

1. New Born Stage:

A thread is created using an object of the class.

2. Ready Stage:

The thread is ready to run using the start() method.

3. Runnable Stage:

The thread runs using the run() method of the Runnable interface.

4. Block Pool Stage:

The thread is temporarily paused or blocked using:

- suspend(): Pauses the thread but doesn't release the lock. Deprecated due to deadlock risk.

- wait(): Pauses the thread and releases the lock. Needs notify()/notifyAll() to resume.
- sleep(): Pauses the thread for a fixed time and resumes automatically.

5. Terminate Stage:

The thread finishes execution and terminates.

Example:

class Test extends Thread {

public void run() {

try {

Thread.sleep(1000);

System.out.println("Hello Java");

} catch (InterruptedException e) {

e.printStackTrace();

public static void main(String[] args) {

Test t = new Test(); // New Born

t.start(); // Ready -> Runnable

Q3. Difference Between wait() and suspend()

1. wait():

- Used to pause a thread.

- Releases the lock.

- Resumes with notify()/notifyAll().


- Safe to use.

2. suspend():

- Used to pause a thread.

- Does not release the lock.

- May cause deadlock.

- Deprecated.

Q4. What is Synchronized in Threads? What is the Purpose of Thread Synchronization?

Ans: Synchronized means only one thread can access a block of code or method at a time. It helps

to prevent problems when multiple threads try to change the same data.

Purpose:

1. Avoid conflicts between threads.

2. Keep data safe and consistent.

3. Ensure smooth program execution without errors.

Q5. Benefits of Multithreading in Java

- Multithreading allows concurrent execution of tasks.

- Improves performance and responsiveness.

- Efficient use of system resources.

Benefits:

1. Faster execution through parallel processing.

2. Better CPU utilization.

3. Shared memory (less memory usage).

4. Improved responsiveness.
5. Better performance on multi-core systems.

You might also like