Thread in Java language
In this tutorial, we will discuss Thread and Multithreading programming in the Java programming language.
What's Thread in Java?
Java provides support for multithreaded programming. A multithreaded java program contains two or more parts. Each part is called a thread. So, Multithreading is a specialized form of multitasking.
Methods that help manage threads:
- start() - Calling its run method.
- run() - Entry point for the thread.
Creating a Thread:
There are two types of methods to create a thread.
- Extends Thread class.
- Implements Runnable interface.
Extends Thread Class:
import java.io.*;
public class thread extends Thread{
public static void main(String[] args){
thread T = new thread();
T.start();
System.out.println("In Main Method");
}
public void run(){
System.out.println("In Run Method");
}
}
Output:
In Main Method
In Run Method
Implements Runnable Interface:
import java.io.*;
public class thread implements Thread{
public static void main(String[] args){
thread T = new thread();
Thread t = new Thread(T);
t.start();
System.out.println("In Main Method");
}
public void run(){
System.out.println("In Run Method");
}
}
Output:
In Main Method
In Run Method
0 Comments