Multithreading

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy