Thread class in java

the Thread class in java is the most important part of multithreading. We can create a thread in two ways whether to use the Thread class or Runnable interface. In this post, we will see what is Thread class in java and how it is useful in multithreading?

Thread class in java

Here is the table content of the article will we will cover this topic.
1. What is the thread class in java?
2. How Thread class is useful?
3. Constructors of Thread class
4. Methods of Thread class

What is the thread class in java?

The Thread class in java is present in java.lang package. Thread class extends Object class and implements the Runnable interface. As we know we can create threads by use of Thread class and achieve multithreading. Thread class in java provides constructors and methods to create and perform operations on a thread.

How Thread class is useful

The Thread class has various methods that are useful to handle the execution of the thread. A program can have multiple threads and each thread has some properties. Like the priority is a property of thread that is used to handle the thread by the thread scheduler. The Thread scheduler uses the priority of threads and determines which thread must run first. Java provides a Thread class that has various methods that can manage the order and behavior of threads. In the below section, we will discuss some important constructors and methods.

Constructors of Thread class

  • Thread(): It is the default constructor of Thread class. It is used to create an object of the Thread class.
  • Thread(Runnable target): It is a parameterized constructor of Thread class which takes a parameter of Runnable. It creates an object like a default constructor (Thread()). The parameter target is the object whose run() method is invoked when the start get call().
  • Thread(Runnable target, String name): It is parameterized constructor that is used to create an object of Thread class. It takes two parameters one is the Runnable type and another String type. It is similar to the above-mentioned constructor except you can set the name of the thread.
  • Thread(String name): It is a parameterized constructor that is used to create an object of Thread class. It takes only one parameter of String type. You can set the name of the thread to pass the parameter
  • Thread(ThreadGroup group, Runnable target): It is a parameterized constructor that is used to create an object of Thread class. It takes two parameters one is the ThreadGroup type and another Runnable type. Group parameter is a collection of multiple threads into a single object and target is the object whose run() method is invoked when the start get call().
  • Thread(ThreadGroup group, Runnable target, String name): It is a parameterized constructor that is used to create an object of Thread class. It takes three parameters first one is the ThreadGroup type, second is Runnable type and third is a String type. ThreadGroup group parameter is a collection of multiple threads into a single object. Runnable target parameter is the object whose run() method is invoked when the start get call(). String name parameter is used to set the name of the thread.
  • Thread(ThreadGroup group, String name): It is a parameterized constructor that is used to create an object of Thread class. It takes two parameters first one is the ThreadGroup type and the second is a String type. ThreadGroup group parameter is a collection of multiple threads into a single object. String name parameter is used to set the name of the thread.
  • Thread(ThreadGroup group, Runnable target, String name, long stackSize): It is a parametrized constructor that is used to create an object of Thread class. It takes three parameters first one is the ThreadGroup type, second is Runnable type and third is a String type. ThreadGroup group parameter is a collection of multiple threads into a single object. The runnable target parameter is the object whose run() method is invoked when the start get call().
    String name, the parameter is used to set the name of the thread.
    long stackSize, the desired stack size for the new thread, or zero to indicate that this parameter is to be ignored.

Methods of Thread class

Thread class has many methods that are used to perform some operations on the thread. We can change the order and behaviors of thread by use of methods.

  1. start(): This method is used to start the execution of the thread. It is very frequently used in multithreading. For more detail, you can visit here.
  2. run(): This method performs the action for a thread. It gets a call from start() method. For more detail, you can visit here.
  3. sleep(): It is used to sleep the current thread for a specific time. For more detail, you can visit here.
  4. yield(): The yield() method is used to give the hint to the thread scheduler. If the current thread is not doing anything important and any other threads need to be run, they should run. Otherwise, the current thread will continue to run. For more detail, you can visit here.
  5. join(): The join() method belongs to Thread class. The join() method is used when we want one thread to wait until another thread completes its execution. For more detail, you can visit here.
  6. getName(): This method returns the name of the thread. It is a public and final method. Its return type is String. For more detail, you can visit here.
  7. setName(String name): This method is used to set the name of the thread. It is a public, final and synchronized method. Its return type is void it means it doesn’t return anything. For more detail, you can visit here.
  8. isDaemon(): This method returns a boolean value either true or false. This method is used to check whether the thread is daemon thread or not. It is a public and final method. Its return type is boolean. For more detail, you can visit here.
  9. setDaemon (boolean on): This method is used to set a thread as daemon thread. You can mark any user thread as a daemon thread bypassing the value true (setDaemon(true)) in a parameter. If I have a Daemon thread and you can make it user thread bypassing the value false setDaemon(false)). For more detail, you can visit here.
  10. getPriority(): This method returns the priority of the thread. It is a public and final method. Its return type is int. For more detail, you can visit here.
  11. setPriority(int newPriority): This method is used to set the priority of a thread. Its return type is void it means it doesn’t return anything.  For more detail, you can visit here.

Leave a Comment