Thread Class in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
Thread Class
Thread class is the main class on which Java's Multithreading system is based. Thread class, along with its companion interface Runnable will be used to create and run threads for utilizing Multithreading feature of Java.
Constructors of Thread class
- Thread ( )
- Thread ( String str )
- Thread ( Runnable r )
- Thread ( Runnable r, String str)
You can create a new thread, either by extending Thread class or by implementing Runnable interface. Thread class also defines many methods for managing threads. Some of them are,
MethodDescriptionsetName()to give thread a namegetName()return thread's namegetPriority()return thread's priorityisAlive()checks if a thread is still running or notjoin()Wait for a thread to endrun()Entry point for a threadsleep()suspend the thread for a specified timestart()start a thread by calling run() method some important points to remember when extending thread class- , we cannot override setName() and getName() functions, because they are declared final in Thread class.
- While using sleep(), always handle the exception it throws.
static void sleep(long milliseconds) throws InterruptedException
Creating a thread
Java defines two ways by which a thread can be created.
- By implementing the Runnable interface.
- By extending the Thread class.
Implementing the Runnable Interface
The easiest way to create a thread is to create a class that implements the runnable interface. After implementing the Runnable interface, the class needs to implement the run() method, which is of form,
public void run()
- run() method introduces a concurrent thread into your program. This thread will end when run() returns.
- You must specify the code for your thread inside run() method.
- run() method can call other methods, can use other classes and declare variables just like any other normal method.
class MyThread implements Runnable { public void run() { System.out.println("concurrent thread started running.."); } } class MyThreadDemo { public static void main( String args[] ) { MyThread mt = new MyThread(); Thread t = new Thread(mt); t.start(); } }
Output :
concurrent thread started running..
To call the run() method, start() method is used. On calling start(), a new stack is provided to the thread and run() method is called to introduce the new thread into the program.
Extending Thread class
This is another way to create a thread by a new class that extends Thread class and creates an instance of that class. The extending class must override run() method which is the entry point of the new thread.
class MyThread extends Thread { public void run() { System.out.println("Concurrent thread started running.."); } } classMyThreadDemo { public static void main( String args[] ) { MyThread mt = new MyThread(); mt.start(); } }
Output :
concurrent thread started running..
In this case also, as we must override the run() and then use the start() method to start and run the thread. Also, when you create a MyThread class object, Thread class constructor will also be invoked, as it is the superclass, hence MyThread class object acts as Thread class object.
What if we call run() method directly without using the start() method?
In the above program, if we directly call run() method, without using the start() method,
public static void main( String args[] ) { MyThread mt = new MyThread(); mt.run(); }
Doing so, the thread won't be allocated a new call stack, and it will start running in the current call stack, that is the call stack of the main thread. Hence Multithreading won't be there.
Can we Start a thread twice?
No, a thread cannot be started twice. If you try to do so, IllegalThreadStateException will be thrown.
public static void main( String args[] )
{
MyThread mt = new MyThread();
mt.start();
mt.start(); //Exception thrown
}
When a thread is in running state, and you try to start it again, or any method try to invoke that thread again using start()method, the exception is thrown.
Comments
Post a Comment