How to create Threads in Java?
Threads can be created by using two ways
1)
Extending the Thread class
2)
Implementing the Runnable Interface
Thread
Class
Thread
class provides methods and constructors to create and perform operations on a
thread.
Thread
class extends Object class and implements Runnable interface.
Steps to create a thread using Thread class
1) Create
a class and extend class with Thread
2) Override
run() method, provide body/ write logic inside run() method.
3)
Invoke start() method.
What is run() & start() method detailed information available in thread class method explanation part.
Example Program:
Output:Thread Class methods:
run(), start(), sleep(), join(), yield(), getId() etc.,
start()
& run() methods
as we
know we can create in two ways i.e., extending thread class and implementing
runnable interface in both ways we need to provide body/ write logic inside
run() method.
Actually
run() method is body of thread.
But we
can’t call run method directly if we call run() method directly it will executes
as a normal program and it is not considered as a separate thread.
run() method doesn’t create separate stack for the new thread.
start()
method is responsible for the execution of run() method.
so instead
of calling run() method directly we should call start() method
start()
method has a capability to create a new stack.
After
invoking start() method, JVM create runtime stack for the particular thread.
That method
body/run() of thread will be executed.
After executing the method that corresponding method will be removed from the stack.
sleep()
method
The sleep() & sleep(Long milliseconds)method is used to pause the execution of a thread for a specific time.
join()
method
The
join() method in java is used to pause the execution of a current thread until
unless the specified thread is dead.
Suppose
we have two threads thread1 and thread2. We want the thread2 should be executed
after the thread1.
We can
achieve it by calling the thread1.join() while thread2 is executing.
The join() method is used for adding sequencing between multiple threads e.g. one thread starts execution after first thread finishes its execution.
yield()
method
The yield() method in Java is used to give the hint to thread scheduler and gives the chance to other threads
A
real-life example of the yield() method
Let’s
say we have two threads, which are thread1, thread2, and both threads have the
same priority.
The
thread1 using the processor time and it is in running state. The thread2 is in
a RUNNABLE state.
The
completion time of thread1 is 1 hour and the completion time of thread2 is 5
minutes.
So
thread2 must wait for 1 hour to just finish the 5-minute job.
In such
type of scenarios where one thread is taking too much time to complete its
execution and other important threads are waiting.
Here can
use the yield() method and give the chance to execute the other threads.
The
yield) method gives the hint to thread scheduler, The thread scheduler can take
the action, or it can ignore the hint.
Thread1.yield()
-> here Thread1 give chance to other
threads so after completion of others then only Thread1 execute
Runnable Interface
The Runnable
Interface should be executed by any class, that class instances are
should be executed by Thread.
Runnable
Interface have only one method i.e., run().
Steps to
create a thread using Runnable Interface
1) Create
a class and implements runnable interface
2) Override
run() method, provide body/ write logic inside run() method.
3) Create
an implementation class instance.
4) Create
a Thread class object and pass implementation class instance inside Thread
class constructor.
5)
Invoke start() method.
Example Program:
Output:
No comments:
Post a Comment