Thread is a flow of execution and a program in itself. Each thread has its own job to perform. We can create a thread by using two things:
1) Extending
Thread class &
2) Implementing
Runnable interface.
Here is one more class i.e. ThreadGroup. This class is
use to group number of threads as per their behavior. This class is helpful, if
we want to change behavior of multiple threads of same group at once.
Thread class has multiple methods to implement required job.
But, in case of Runnable interface, it has only run() method.
We should implement runnable interface because by
extending thread class we will never extends other classes if required.
Each thread has some property, which are
1)
Name: By
setting name, we can identify the thread by its name. By default name is
Thread0, Thread1…
2)
Priority: By
setting name, we can set the execution chance priority. We can set from 1 to
10. By default priority is normal i.e. 5.
3)
Group:
We cannot set group of threads. It is automatically set by JVM. We
can only get group name of thread.
Thread class implements
Runnable interface.
No comments:
Post a Comment
Thank you for your comments.