Introduction
What is a Java Thread
The Java Thread Model
The Java run-time system depends on threads for many things. Threads reduce inefficiency by preventing the waste of CPU cycles.
Threads exist in several states. Following are those states:
- New – When we create an instance of Thread class, a thread is in a new state.
- Runnable – The Java thread is in running state.
- Suspended – A running thread can be suspended, which temporarily suspends its activity. A suspended thread can then be resumed, allowing it to pick up where it left off.
- Blocked – A java thread can be blocked when waiting for a resource.
- Terminated – A thread can be terminated, which halts its execution immediately at any given time. Once a thread is terminated, it cannot be resumed.
Multithreading in Java : Thread Class and Runnable Interface
Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable. To create a new thread, your program will either extend Thread or implement the Runnable interface.
The Thread class defines several methods that help manage threads.The table below displays the same:
Main Java Thread
Now let us see how to use Thread and Runnable interface to create and manage threads, beginning with the main java thread, that all Java programs have. So, let us discuss the main thread.
Why is Main Thread so important?
- Because this thread effects the other ‘child’ threads
- Because it performs various shutdown actions
- It is created automatically when your program is started.
How to Create a Java Thread
- By implementing the Runnable interface.
- By extending the Thread
Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface.
To implement Runnable interface, a class need only implement a single method called run( ), which is declared like this:
Inside run( ), we will define the code that constitutes the new thread
Example:
Here is how that is done:
The second way to create a thread is to create a new class that extends Thread, then override the run() method and then to create an instance of that class. The run() method is what is executed by the thread after you call start(). Here is an example of creating a Java Thread subclass:
To implement Runnable interface, a class need only implement a single method called run( ), which is declared like this:
public void run( )
Inside run( ), we will define the code that constitutes the new thread
Example:
public class MyClass implements Runnable {
public void run(){
System.out.println("MyClass running");
}
}
To execute the run() method by a thread, pass an instance of MyClass to a Thread in its constructor.Here is how that is done:
Thread t1 = new Thread(new MyClass ());
t1.start();
When the thread is started it will call the run() method of the MyClass instance instead of executing its own run() method. The above example would print out the text “MyClass running“.Extending Java Thread
public class MyClass extends Thread {
public void run(){
System.out.println("MyClass running");
}
}
To create and start the above thread you can do like this:
MyClass t1 = new MyClass ();
T1.start();
When the run() method executes it will print out the text “MyClass runningAuthors of the blog
Tanay Soni
Sarvesh Thorve
Siddesh Patil
Tanish Kogta
No comments:
Post a Comment