Threads in Java
Know Creating Threads and Multithreading in Java
Soresa
Last Update 4 года назад
- Unlike many other computer languages, Java provides built-in support for multithreading. Multithreading in Java contains two or more parts that can run concurrently. A Java thread is actually a lightweight process.
- This blog will introduce you to all the Java Thread concepts which many people find tricky to use and understand. So let us get started then, shall we?
- What are Threads in Java?
- Multithreading in Java
- Life Cycle of Thread.
- Creating Thread using Thread Class.
- Synchronization
What are Threads in Java?
- A thread is actually a lightweight process. Unlike many other computer languages, Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called thread and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking.
What is Multithreading?
- Multithreading is a Java feature that allows concurrent(simultaneously) execution of two or more parts of a program(thread) for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. … Start () invokes the run () method on the Thread object.
What are the advantages of multithreading?
- The primary function of multithreading is to simultaneously run or execute multiple tasks. These tasks are represented as threads in a Java program and have a separate execution path. Also, handling of multithreaded Java programs is easy because you can decide the sequence in which execution of Java threads take place
Why do we use threads?
- Thread is a light weight process which helps in running the tasks in parallel. The threads work independently and provides the maximum utilization of the CPU, thus enhancing the CPU performance. Threads to make Java application faster by doing multiple things at same time.
How does multithreading works?
- Java multithreading allows you to do multiple tasks at the same time. This is possible because modern day computers have multiple CPUs (CPUs are the brain of your computer, and it has a bunch!). One CPU can work on one Thread at a time (unless your CPUs have hyper-threading, in which case it can handle two at a time).
Advantages of multithreading:
- Enhanced performance by decreased development time
- Simplified and streamlined program coding
- Improvised GUI responsiveness
- Simultaneous and parallelized occurrence of tasks
- Better use of cache storage by utilization of resources
- Decreased cost of maintenance
- Better use of CPU resource
Disadvantages of multithreading:
- Complex debugging and testing processes
- Overhead switching of context
- Increased potential for deadlock occurrence
- Increased difficulty level in writing a program
- Unpredictable results
Life cycle of a Thread

- New – A thread that has not yet started is in this state.
- Runnable – A thread executing in the Java virtual machine is in this state.
- Blocked – A thread that is blocked waiting for a monitor lock is in this state.
- Waiting/Suspended – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
- Dead/Terminated – A thread that has exited is in this state.
A thread can be in only one state at a given point in time.
- We can create threads in java by extending the java.lang.Thread class or by implementing the java.lang.Runnable interface. Both the implementation overrides the run() method to do so. Both the implementations can be used according to the use case of the class.
Creating a thread in Java
- By extending Thread class.
- By implementing Runnable interface.
- Before we begin with the programs (code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below.
- getName(): It is used for Obtaining a thread’s name
- getPriority(): Obtain a thread’s priority
- isAlive(): Determine if a thread is still running
- join(): Wait for a thread to terminate
- run(): Entry point for the thread
- sleep(): suspend a thread for a period of time
- start(): start a thread by calling its run() method
Method 1: Thread creation by extending Thread class
Example 1:
output:
My thread is in running state.
Example 2:
output:
my thread created Thread[my extending thread,5,main]
Main thread will be alive till the child thread is live
Printing the count 0
Printing the count 1
Main thread will be alive till the child thread is live
Printing the count 2
Main thread will be alive till the child thread is live
Printing the count 3
Printing the count 4
Main thread will be alive till the child thread is live
Printing the count 5
Main thread will be alive till the child thread is live
Printing the count 6
Printing the count 7
Main thread will be alive till the child thread is live
Printing the count 8
Main thread will be alive till the child thread is live
Printing the count 9
My thread run is over
Main thread's run is over
Synchronization
- Multithreading introduces asynchronous behavior to the programs. If a thread is writing some data another thread may be reading the same data at that time. This may bring inconsistency.
- When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization.
- To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.
- When we want to synchronize access to objects of a class which was not designed for the multithreaded access and the code of the method which needs to be accessed synchronously is not available with us, in this case we cannot add the synchronized to the appropriate methods. In java we have the solution for this, put the calls to the methods (which needs to be synchronized) defined by this class inside a synchronized block in following manner.
