18 Jan Java Threading
Threading in Java means working on more than two things at the same time. For achieving Threading, Java has the Thread class. The Thread class has constructors and methods to work in Threads in Java.
Thread Constructors
The following are the constructors in Java Threading:
Thread Methods
The following are the methods in Java Threading:
Create a Thread in Java
To create a Thread in Java:
- Create a Thread by extending the Thread class
- Create a Thread by implementing the Runnable interface
Create a Thread by extending the Thread class
We can create a Thread by extending and running the Thread class it using the start() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Studyopedia extends Thread { public void run(){ System.out.println("The thread is running"); } public static void main(String args[]){ Studyopedia th = new Studyopedia(); // The start() method starts the execution of the thread th.start(); } } |
Output
1 2 3 |
The thread is running |
Create a Thread by implementing the Runnable interface
We can create a Thread by implementing the Runnable interface and run it using the start() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Studyopedia implements Runnable{ public void run(){ System.out.println("The thread is running"); } public static void main(String args[]){ Studyopedia s = new Studyopedia(); Thread th = new Thread(s); // The start() method starts the execution of the thread th.start(); } } |
Output
1 2 3 |
The thread is running |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments