Java Thread
In Java, threads can be created by using two mechanisms:
- Extending the Thread class
- Implementing the Runnable interface
Extending the Thread Class
- We create a class that extends the java.lang.Thread class.
- The java.lang package is imported automatically in out program.
- This class overrides the run() method available in the Thread class.
- A thread begins its life inside run() method.
- We create an object of our class and call start() method to start the execution of a thread.
- start() invokes the run() method on the Thread object.
Output :
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
I am listening song...
This is very nice video..
I am watching coding video.
This is very nice video..
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
Implementing the Runnable Interface
- We create a new class which implements java.lang.Runnable interface.
- The java.lang package is imported automatically in out program.
- This class overrides the run() method available in the Runnable interface.
- We instantiate a Thread class object and call start() method on this object.
Output :
I am listening song...
This is my favorite song....
I am watching coding video.
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
This is very nice video..
I am watching coding video.
This is very nice video..
I am listening song...
This is my favorite song....
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
Note: Thread class implements the Runnable interface and the run() method is declared inside the Runnable interface.
We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.
Thread Priorities
- Whenever we create a thread in Java, it always has some priority assigned to it.
- Priorities in threads is a concept where each thread is having a priority which is represented by numbers ranging from 1 to 10.
- In most cases, the thread scheduler schedules the threads according to their priority (known as primitive scheduling).
- But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
- The default priority is set to 5 as expected.
- Minimum priority is set to 1.
- Maximum priority is set to 10.
Some of the methods in Thread class are as:
- currentThread(): The method is used to return a reference to the currently executing thread object.
- setName(): This method is used to change the name of the thread.
- getName(): This method is used to return the name of the thread.
- setPriority(): This method is used to change the name of the thread.
- getPriority(): This method is used to return the priority of given thread.
- getId(): This method returns the identifier of the invoked thread. The thread ID is unique, generated when thread is created and remains unchanged during its lifetime.
- join(): The join() method permits one thread to wait until the other thread to finish its execution.
Output :
Current Thread name is: Thread-4
Current Thread name is: Thread-3
Current Thread name is: Thread-2
Current Thread name is: First
Current Thread priority is: 7
Current Thread priority is: 5
Current Thread priority is: 5
Current Thread priority is: 10
Current Thread name is: Last
Current Thread priority is: 2
Current Thread name is: Thread-5
Current Thread priority is: 1
Java Thread Constructors
Some of the main Constructors of Thread class are explained as:
- Thread(): Allocates a new Thread object.
- Thread(String name): Allocates a new Thread object and sets the name of the Thread.
- Thread(Runnable target): Allocates a new Thread object.
- Thread(Runnable target, String name): Allocates a new Thread object and sets the name of the Thread.
Output :
I am extending Thread class.
This is inside Run1 class extending Thread class..
The name is Deepak
The id is 14
The name is Gagandeep
The id is 15
I am implementing Runnable interface...
This is inside Run2 class implementing Runnable interface....
Comments
Post a Comment