#Multithreading in Java

Multithreading

Multithreading is a Java 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 …


This content originally appeared on DEV Community and was authored by ritwin59

Multithreading

Multithreading is a Java 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.

Threads can be created by using two mechanisms :

1.Extending the Thread class 
2.Implementing the Runnable Interface




Extending the Thread class

Thread creation by extending 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. Start() invokes the run() method on the Thread object.

Example:

class MultithreadingDemo extends Thread {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}

Output

Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running

Implementing the Runnable Interface

Thread creation by implementing 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.

// Java code for thread creation by implementing
// the Runnable Interface
class MultithreadingDemo implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}

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

Thread 13 is running
Thread 11 is running
Thread 12 is running
Thread 15 is running
Thread 14 is running
Thread 18 is running
Thread 17 is running
Thread 16 is running

Thread Class vs Runnable Interface

  1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.
  2. We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.
  3. Using runnable will give you an object that can be shared amongst multiple threads.


This content originally appeared on DEV Community and was authored by ritwin59


Print Share Comment Cite Upload Translate Updates
APA

ritwin59 | Sciencx (2021-11-25T13:25:14+00:00) #Multithreading in Java. Retrieved from https://www.scien.cx/2021/11/25/multithreading-in-java/

MLA
" » #Multithreading in Java." ritwin59 | Sciencx - Thursday November 25, 2021, https://www.scien.cx/2021/11/25/multithreading-in-java/
HARVARD
ritwin59 | Sciencx Thursday November 25, 2021 » #Multithreading in Java., viewed ,<https://www.scien.cx/2021/11/25/multithreading-in-java/>
VANCOUVER
ritwin59 | Sciencx - » #Multithreading in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/25/multithreading-in-java/
CHICAGO
" » #Multithreading in Java." ritwin59 | Sciencx - Accessed . https://www.scien.cx/2021/11/25/multithreading-in-java/
IEEE
" » #Multithreading in Java." ritwin59 | Sciencx [Online]. Available: https://www.scien.cx/2021/11/25/multithreading-in-java/. [Accessed: ]
rf:citation
» #Multithreading in Java | ritwin59 | Sciencx | https://www.scien.cx/2021/11/25/multithreading-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.