Mutithreading- Thread class

Posted on Updated on

The multithreading system of java is built on thread class, its methods and runnable interface.

The execution of threads are encapsulated under Thread. In order to create a new thread, one can either extend the existing thread or implement the runnable interface.

The Thread class implements several class that should be used when working with thread. The methods are listed below

Method Description
GetCurrentThread This will return the current thread
getName This method is used to get thread's name
setName This method is used to set thread's name
getPriority This will get the priority of Thread
isAlive This will determine if the thread is alive i.e. running. Returns true if alive or else false.
join This tells to wait for thread to terminate
run This is entry point of thread
sleep To suspend the thread for specified period of time
start This starts a thread by calling its running method

We shall see implementation of above methods.

Example-

getCurrentThread

Output

In the example above we see that it displays current thread output in a certain ways.

The element at first index location “main” is name of the thread, element at second location “5” is the default priority, and the element at last index location “main” is the name of group to which the thread belongs. A group thread is a data structure which controls the state of collection of threads as a whole.

Let us see implementation of few more methods to understand better.

Output

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.