An introduction about Thread in Java

  • By
  • October 30, 2021
  • JAVA

Thread in java is a mechanism provided to the developer to have a golden touch of a multitasking  environment. To define a thread one can say that small part of executing program. Each Thread gives a separate path of execution. Multithreading is a form of Multitasking. 

Now in the Modern Operating system everybody is much worse with the multitasking environment. Multitasking can be achieved in two ways. One by Process based and another by Thread  based . So now understand the difference between the two from Java Classes In Pune

A process is a program in working mode. So multiple programs can run on the operating system multiple time. For example you can run video while pasting data to from one folder to another , at the same time you can type something on your text editor. So a process based multitasking can be said to be a smallest unit of code that can be dispatched by the scheduler at the same time.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

A thread a smallest unit of code that is dispatched. A single program can  perform two or more tasks simultaneously. You can consider the example of a text editor with the help of  Java Course In Pune that can format text at the same time that it is printing , if  these two things are being performed by two separate threads.

So you can say that process multitasking gives the idea of the big picture and thread based multitasking handles the details of execution. Thread Based Multitasking requires less overhead than process based multitasking. 

Multitasking threads require less overhead than multitasking processes. Processes make the task heavyweight because  that requires its  own separate address spaces. That’s why  Interposes communication becomes expensive and limited. Therefore  Context switching from one process to another is also costly. 

Threads are lightweight because they share the same address space. Also  cooperatively share the same heavyweight process. So thread based multitasking  is not very expensive , and context switching from one thread to the next is low cost.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Java controls Thread-based multitasking – 

JRE depends on threads. All the class libraries are designed with multithreading . Asynchronous. Environment is possible by Java threads. In this way you can reduce inefficiency by preventing the waste of CPU cycles.

Multithreaded environment  works best in contrast to Single-threaded systems as it uses an approach called an event loop with polling. One thread of control runs in an infinite loop in polling. Polling a single event queue to decide what to do next. This polling mechanism gives a signal about  network file as it is available  to be read, so the event loop dispatches control to the appropriate event handler. Unless and until this event handler returns,  nothing else can happen in the system. According to the updated concepts of Java Training In Pune, in this way CPU time is wasted. Because of this in one part of a program dominating the system and preventing any other events from being processed. 

A single – Threaded environment causes blocking as it is waiting for some resource, the entire program stops running. The best part  of multithreading in Java  is that the main loop/polling mechanism is eliminated. Without stopping other parts of your program, one thread can pause. The idle time utilized while a thread reads data from a network or waits for user input can be utilized elsewhere. It allows animation loops to sleep for a second between each frame without causing the whole system to pause. Only the single thread that is blocked pauses , When a thread blocks in a Java program. All other threads continue to run. Threads exist in several states. A thread can be running. It can be ready to run as soon as it gets CPU time. So also a running thread can be suspended, which temporarily suspends its activity. Thread that is suspended can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. A thread can be terminated at any time, which halts its execution immediately. If the thread is terminated, cannot be resumed.

Thread Priorities –

Priorities are assigned to each thread , that determines how that thread should be treated with respect to the others. These are integers that specify the relative priority of one thread to another. A priority is meaningless ,as an absolute value; a higher-priority thread will not run faster than a lower-priority thread if it is the only thread running. To decide when to switch from one running thread to the next , priority is used. This is called a context switch.  Every Program in java is a thread itself. For example.

 

public class Sample {

 

public static void main(String[] args)  {

Thread t = Thread.currentThread();

System.out.println(“Current thread: ” + t);

t.setName(“My Thread”);

System.out.println(“After name change: ” + t);

try {

for(int i=10;i<=50;i+=10) {

System.out.println(i);

Thread.sleep(1000);

}

}

catch (InterruptedException e) {

System.out.println(“Main Thread Interrupted..”);

}

System.out.println(“Main Thread Terminated…”);

}

 

}

 

Creating Thread with Runnable Interface

 

class newThread implements Runnable{

Thread t;

public newThread() {

 

t=new Thread(this,”Demo Thread”);

System.out.println(“Child Thread : “+t);

t.start();

}

@Override

public void run() {

try {

for(int i=2;i<=10;i+=2) {

System.out.println(“child Thread “+i);

Thread.sleep(500);

}

}catch (InterruptedException e) {

System.out.println(“Child Thread Interrupted..”);

}

 

System.out.println(“Child Thread Terminated…”);

}

}

public class Sample {

 

public static void main(String[] args)  {

new newThread();

try {

for(int i=1;i<=5;i++) {

System.out.println(“Main Thread : “+i);

Thread.sleep(1000);

}

}

catch (InterruptedException e) {

System.out.println(“Main Thread Interrupted..”);

}

System.out.println(“Main Thread Terminated…”);

}

 

}

 

Creating Thread with extending Thread Class

 

class newThread extends Thread{

public newThread() {

super(“Child Thread”);

System.out.println(“Child thread “);

start();

}

@Override

public void run() {

try {

for(int i=2;i<=10;i+=2) {

System.out.println(“child Thread “+i);

Thread.sleep(500);

}

}catch (InterruptedException e) {

System.out.println(“Child Thread Interrupted..”);

}

 

System.out.println(“Child Thread Terminated…”);

}

}

public class Sample {

 

public static void main(String[] args)  {

new newThread();

try {

 

for(int i=1;i<=5;i++) {

System.out.println(“Main Thread : “+i);

Thread.sleep(1000);

}

}

catch (InterruptedException e) {

System.out.println(“Main Thread Interrupted..”);

}

System.out.println(“Main Thread Terminated…”);

}

 

}

Author:-

Jyotsna Binjwe

Call the Trainer and Book your free demo Class  Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

 

Submit Comment

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

*
*