Java Multithreading:

Before knowing multithreading, first we have to know about the concept of multi-tasking. When multiple tasks are executing simultaneously, is known as multi-tasking.

For example, in a class room, while a faculty is teaching, the sitting students are doing multiple activities: such as listening the lecture, at the same time they are writing the notes, at the same time some students are in sleeping mood and at the same time some students are busy with their mobiles. So all these activities they are doing simultaneously. Hence this is called as multi-tasking. Similarly multitasking allows several activities to occur concurrently on the computer.

Basically there are two types of multitasking, such as

  • Process based multitasking
  • Thread based multitasking

Process based multi-tasking:

Executing multiple tasks simultaneously where each task is a separate independent process is known as process based multi-tasking. For example; while writing a java program in the editor, we can listen music through MP3 player. At the same time we can download a file from the internet. All these tasks are executing simultaneously and independent of each other. So it is called as process based multi-tasking. It is best suitable at operating system level.

Disadvantages :

  • Each process contain its own memory area.
  • That's why it is a heavy weight process.
  • Cost communication between process is high.

Thread based multi-tasking:

Thread based multi-tasking is called as multithreading. Executing multiple tasks simultaneously where each task is a separate independent part of same program is known as thread based multi-tasking. It is best suitable at programmatic level. Java provides inbuilt support for thread based multi-tasking by providing varieties of library (Thread, Runnable etc...).

The purpose of multitasking is to reduce the response time and improve performance of the system whether it is process based or thread based. There are different areas where multithreading concept is used, such as video games, animation movies, multimedia graphics, server side etc..

Here I am presenting some advantages of thread-based multitasking as compared to process-based multitasking which are as follows:

  • Threads share the same address space.
  • Context switching between threads is usually less expensive than between processes.
  • Cost of communication between threads is relatively low.

Java supports thread-based multitasking and provides high-level facilities for multi-threaded programming. Thread safety is the term used to describe the design of classes that ensure that the state of their objects is always consistent, even when the objects are used concurrently by multiple threads.

Overview of Threads:

  • A thread is an independent normal flow of execution within a program.
  • Many threads can run concurrently within a program.
  • At runtime threads in a program exist in a common memory space and can, therefore, share both data and code, that is they are lightweight compared to processes.
  • They also share the process running the program.
  • Every thread in java is created and controlled by a unique object of the java.lang.Thread class.
  • Thread make the runtime environment asynchronous, allowing different tasks to be performed concurrently.
  • It is pointed that every java program have a thread which is running internally and that thread is used by JVM to execute the statements. Let's know what is that thread!! For this let's see the following program :

Example

class ThreadDemo
{
public static void main(String args[ ])
{
Thread t=Thread.currentThread( );
System.out.println("current thread is"+t);
System.out.println("the name of thread is"+t.getName());
}
}

Output

current thread is Thread[main,5,main]
the name of thread is main

  • Here currentThread() is a static method invoking by Thread class directly and t is an object of thread class.
  • In Thread[main,5,main], Thread indicates that t is a Thread class object, and the first main is the name of the thread, and 5 is the number representing the priority of the thread. This priority numbers are ranging from 1 to 10. The minimum priority is 1 and the maximum priority is 10 and the default priority is 5. The more priority will be given more preference at the time of execution by JVM.
  • The next main indicates the thread group where this thread is belonging. A thread group represents a group of threads as a single unit.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment