What is multithreading??? (Java)

When two or more parts of a program run simultaneously it is called multithreading and each such program is called a thread. We achieve maximum utilization of CPU through multithreading.

Threads can be created using two methods.

  1. Extending the thread class

  2. Implementing runnable interface

Implementing multithreading by extending thread class:

package com.company;
class Mythread1 extends thread{
@override
public void run(){ // this function (public void run) is used to perform actions for a thread
system.out.print("this is thread");
}
}
public class main {
public static void main(string[] args){
Mythread t1 = new Mythread();
t1.start(); // ( .start() ) function is used
}
}

This code will give output "this is thread".

Implementing multithreading by using runnable interface

package com.company
class Mythread implements Runnable{
public void run(){
system.out.print("this thread is running");
}
}
public class main {
public static void main(string[] args){
Mythread t1 = new Mythread();
t1.start(); // ( .start() ) function is used
}
}