0% found this document useful (0 votes)
16 views

Multithreading

Multithreading allows concurrent execution of parts of a program to maximize CPU utilization. There are two main ways to create threads in Java: 1) by extending the Thread class and overriding the run() method, or 2) by implementing the Runnable interface and overriding the run() method. The primary purpose of multithreading is to provide simultaneous execution of program parts to make full use of CPU time. Advantages include not blocking the user, performing multiple operations simultaneously to save time, and exceptions in one thread not affecting others.

Uploaded by

prasadgr931
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Multithreading

Multithreading allows concurrent execution of parts of a program to maximize CPU utilization. There are two main ways to create threads in Java: 1) by extending the Thread class and overriding the run() method, or 2) by implementing the Runnable interface and overriding the run() method. The primary purpose of multithreading is to provide simultaneous execution of program parts to make full use of CPU time. Advantages include not blocking the user, performing multiple operations simultaneously to save time, and exceptions in one thread not affecting others.

Uploaded by

prasadgr931
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Powered by AI

Bing

Multithreading is a feature that allows concurrent execution of two or more parts of a program for maximum
utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within
a process.

There are two main ways to create a thread in Java:

1. Extend the Thread class: We create a class that extends the java.lang.Thread class. This class
overrides the run() method available in the Thread class. A thread begins its life inside run() method.
We create an object of our new class and call start() method to start the execution of a thread.
2. Implement the Runnable Interface: We create a new class which implements java.lang.Runnable
interface and override run() method. Then we instantiate a Thread object and call start() method on
this object.

Here’s an example of creating a thread by extending the Thread class:

class MultithreadingDemo extends Thread {


public void run() {
try {
System.out.println("Thread " + Thread.currentThread().getId() + " is running");
} catch (Exception e) {
System.out.println("Exception is caught");
}
}
}

public class Multithread {


public static void main(String[] args) {
int n = 8;
for (int i = 0; i < n; i++) {
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}

And here’s an example of creating a thread by implementing the Runnable interface:

class MultithreadingDemo implements Runnable {


public void run() {
try {
System.out.println("Thread " + Thread.currentThread().getId() + " is running");
} catch (Exception e) {
System.out.println("Exception is caught");
}
}
}

class Multithread {
public static void main(String[] args) {
int n = 8;
for (int i = 0; i < n; i++) {
Thread object = new Thread(new MultithreadingDemo());
object.start();
}
}
}

The primary purpose of multithreading is to provide simultaneous execution of two or more parts of a program
to make maximum use of CPU time. A multithreaded program contains two or more parts that can run
concurrently. Multithreading is mostly used in games, animation, etc.

Advantages of Multithreading:
1. It doesn’t block the user because threads are independent and you can perform multiple operations at the
same time.
2. You can perform many operations together, so it saves time.
3. Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.

Please note that every class used as a thread must implement the Runnable interface and override its run()
method. Also, the Thread class is part of the java.lang package.

You might also like