What is Java Thread?

In the context of Java, a thread is a path followed while executing a program which means more than one task can run at a time. Before the introduction of the thread concept, it was not possible to run multiple tasks parallelly.

It is a light weighted process, or in other words, it is the smallest part of the process that allows a program to operate more efficiently by running multiple tasks at the same time in parallel.

Why is it used?

Threading is used in Java mainly to perform complicated tasks in the background where all the tasks have their own separate path for execution and each and every task is executed without affecting the main program. Each and every process in this thread is independent.

Here, all the threads shares a common memory, has their own stack, program counter and local variables. This process is also called as Multi-threading, since multiple threads are executed in parallel at the same time.

Advantages of using thread:

  • If a thread gets an error or an exception during the time of its execution, it wil not affect the execution of other threads.

  • We can perform multiple operations at the same time which saves an ample amount of time.

  • Since threads are independent and can perform multiple operations at the same time, it doesn’t block the user, which is another advantage of using Multi-threading in Java.

  • Usage of threads provides concurrency within the process.

  • Threads allow efficient use of multiprocessor architectures.

States in thread

The following are the states in thread model.

  1. New(Ready to run) - A thread is in New state when it gets CPU time.

  2. Running - A thread is in running state when it is under execution.

  3. Suspended - A thread is said to be in a suspended state when it is temporarily inactive or under execution.

  4. Blocked - A thread is said to be in blocked state when it is waiting for resources.

  5. Terminated - A thread is said to be terminated when it halts its execution completely.

Thread States

Creation of thread

There are two ways to create thread. One way is to create by extending the Thread class and overriding its run() method.

Thread Code

Another way in creation of thread is to implement the Runnable interface.

Runnable Code

Thread class

Thread class has many constructors and methods which allow us to perform various operations on a thread. The Object class is extended by the Thread class, and it implements the Runnable Interface.

Some of the constructors which are commonly used in performing various operations are mentioned below:

• Thread()
• Thread(Runnable, String name)
• Thread(Runnable target)
• Thread(ThreadGroup group, Runnable target, String name)
• Thread(ThreadGroup group, Runnable target)
• Thread(ThreadGroup group, String name)
• Thread(ThreadGroup, Runnable target, String name, long stack Size)

Some commonly used methods of the Thread class are as follows:

• public void run()
• public void start()
• public void sleep(long miliseconds)
• public void join(long miliseconds)
• public int getPriority()
• public int setPriority(int priority)
• public String getName()
• public void setName(String name)
• public Thread currentThread()
• public int getId()
• public Thread.State getState()
• public boolean isAlive()
• public void yield()
• public void suspend()
• public void resume()
• public void stop()
• public boolean isDaemon()
• public void setDaemon(boolean b)
• public void interrupt()
• public boolean isInterrupted()
• public static boolean interrupted()

Types of thread

Thread implementation can be defined in two ways:

  • One is User Level Threads which is known as User Managed Threads.

  • Another is Kernel Level Threads which are the Operating System Managed threads acting on the Kernel,which is the operating system core.

User-Level threads

In User Level Threads, the thread management kernel is not aware of the existence of threads. So,it handles them as if they are single-threaded processes.

In general, User Level Threads are smaller and faster than Kernel Level Threads and also they are represented by stack, registers program counter and a small process control block.

Thread Types

Advantages of User-Level threads

  • User-Level Threads will be able to run on any operating system.
  • User-Level Threads are faster to create and easier to manage.
  • There is no kernel mode privilege in required for thread switching in user-level threads.
  • In User-Level Threads, scheduling can be user specific.

Disadvantages of User-Level threads

  • In User-Level Threads, Multi-threaded applications cannot use multi-processing to their advantage.
  • If one user-level threads performs blocking operation, then the entire process is blocked.

Kernel Level threads

In Kernel Level Threads, thread management is done by the Kernel but the threads are handled by the operating system. Since the context information for the process as well as the process threads are all managed by Kernel which is why Kernel Level threads are slower than the User-Level Threads.

Advantages of Kernel-Level threads

  • In Kernel-Level Threads, multiple threads on same process can be scheduled on different processors.
  • The routines of the kernel can be multithreaded.
  • Unlike user-level threads, if a kernel-level thread is blocked, another thread of the same process can be scheduled by the kernel.

Disadvantages of Kernel-Level threads

  • Kernel-level threads are slower to be created and managed when compared to user-level threads.
  • A mode switch is required to transfer control from one thread to another in a process.

Why Java threads should be monitored?

In the production environment, Java applications can have hundreds or even thousands of threads running in parallel. Issues may arise like JVM(Java virtual machine) taking up very high CPU resources. Here multiple threads in a JVM can be performing similar tasks like updating a file while another request is in process. It is important to synchronize the threads, so that only on thread can update a file at a time.

There are JMX(Java Management Extensions) interfaces provided by JVM that allow monitoring tools to track the thread activities in JVM.

We should be able to find how many threads are currently running in the JVM, the state of the threads like runnable, running, terminated, blocked or waiting, threads taking up excessive CPU resources, any blocked threads exist? If so, which class and variable they are in, etc.

These are some basic questions we should be able to get the solution from while trying to use Java application monitoring tools.


Further Reading

  1. What is an API Monitoring?
  2. What are the most common web application issues that you might face day-to-day in your application?
  3. What is Atatus APM and its Benefits?

Monitor your software stack for free with Atatus.

Start your free trial
OR
Request a Demo

Free 14-day trial. No credit card required. Cancel anytime.

Ready to see actionable data?

Avail Atatus features for 14 days free-trial. No credit card required. Instant set-up.