How to create threads and run in java?
A thread represents the separate path of execution of a group of statements. In a java program if we write a group of a statement then these statements are executed by Java Virtual Machine(JVM) one by one. This execution call threads because the Java virtual machine uses a thread.
Note: Thread is a lightweight process because they utilize minimum resources of the system, this means they take less memory and less processes(minimum execution time).
In every program, at least one thread is executed that is main thread. It always creates and executes in the program. In a single program, there may be many threads and it is executed one by one in the memory by the Java virtual machine.
A task means doing some calculation. A task is a small part of a program that is given to interpreter to perform some calculation. A thread represents an execution of statements, the way the statements are executed as of two types-
In a single tasking environment, only one task is given to processor at a time of execution. In this tasking, only one separate memory is provided by the processor in which the task is performed.
The second tasking is the multitasking, multitasking means executing multiple jobs at a time. To use the processor time in an optimum way we can give it several jobs at a time this is called multitasking.
Note: Executing only one job at a time call single tasking while executing several jobs at a time is called multitasking. In single-tasking, the processor time is wasted but in multitasking, we can utilize the processor time as well as available resources in an optimum way.
Multitasking is the process of executing several jobs or multiple jobs at the same time. Multitasking can be categorized as-
- Processed based multitasking(multiprocessing): Each process has its own memory address in the memory. In Another word, we can say that every process has its separate path.
- Thread-based multitasking (Multi-threading): In multithreading, number of threads are executed at the same time. the communication between threads is very low. In multithreading, every thread shares the same address space or memory.
Every Java Program uses threads
Threading is the important concept in which we can execute multiple threads at a time with the help of Java Virtual Machine. If we write any java program that has at least one thread that is the main thread. When we try to execute the program, Java Virtual Machine creates the main thread and calls the program’s main in which the main thread is available. Java virtual machine also creates other threads but it is invisible to the user because it is not so important to them for the user.
Why use threads?
Threads are used in every program of java. Threads are used because number of reasons why the threads create and use some of them are
- Make the User interface(UI) more responsive, means more dynamically.
- The main advantage of using threading concepts is multiprocessing. We can execute multiple jobs at the same time with the help of the processor.
- It is more valuable in those places where the execution of the program performs as Asynchronous way.
We can create a thread in the program and execute the threads one by one. There are following steps for creating a thread which shows below-
Steps for creating a thread
- Create a class that extends thread class or implements run-able to interface.(both are found in Java.lang package).
- Write or provide the body of Run () method.
- Create an object of that class which contains the run () method.
- Create a thread and attach to the object of a class containing a Run () method.
- Run the thread (for this purpose) use the Start () method of the thread class.
- Example to show (how to run and create a thread).
Thread t= new Thread(); //here Thread is the class and t is the object of thread class.
Thread t=new (object,”thread_name”); //here we pass object and thread name.
Program to show creation of thread
public class sample
public static void main(Stringargs)
Thread t= Thread.currentThread();
System.out.println(“name of the current Thread=”+t.getName());
name of the current Thread main.
Note: By default Run () method recognize and executes by a thread.
Explanation of the program
In above program currentThread() is a static method in Thread class, so we called it as Thread.currentThread(). After this method gives an object t of Thread class. When we displayed object t we displayed the following contained-
Here Thread indicates Thread ()method, First main indicate the name of the thread running on the current code.
5-It representing the priority of the current threads(IE 1 to 10).
The next main indicates the thread group name to which thread belongs. A thread group represents a group of the thread in a single unit.
Life cycle of a Thread
A thread is creating using new Thread() statement and executes by the start() method. The thread inter runnable() states and when sleep(), suspend(), wait() method use or when the thread block on input/output, it then goes into not runnable state(), from not runnable states the thread, comes back to the runnable states and continue running the statements.
The thread dies when it comes out from run() method, these state transition of a thread call a life cycle of a thread.
Yield() method may pause a thread briefly, but the thread will still in a runnable () states only.
Program to show how start() method works
Here is the simple example that show the execution flow of the program with start()method.
class sample extends Thread
public void run()
public class main class
public static void main(Stringargs)
sample ob=new sample();
Thread t1=new Thread(ob);
Thread t2=new Thread(ob);
t.start(); //here the start() method call(execution start from from start method).
When two or more threads need access to shares resources, they need some way to insure that the resources will uses by only one thread at a time. Process by which achieve call thread synchronisation.
Here in this article, I have provided simple and easy content. Any learner can easily read and understand the concepts of threading, Multi-threading, multitasking etc. In this post, I also defined how we can understand the life cycle of the thread in simple words with the help of the images. I hope this article is much more beneficial for the learner.