Before going into details of " multithreading" in java. Let us go through few terminology related to thread and how they are implemented in different fields of computer science.
Listening music on our desktop is a very good example to start with, We all love to listen music. Everyone have there own choice, and they love to play it as many times as possible. Well to play a music
file of .mkv format (video) we need to have a music player , which is nothing but a software. This software is basically a block of code which simplifies a complex process. This program is designed to do many task simultaneously like playing the visual part , audio part and makes the interface of the media player interactive
with the user. User can change the aspect ratio and do many more things while the song is being played.This is an example of multitasking.
Formally multitasking (or time sharing) means that the cpu executes more than one job by switching among them,but the switching is so frequently done that we can interact with each program while it is running.The term switching used above is nothing but context switching. Many of us are familiar with this term.We have studied it in operating system.
Switching the CPU to another process requires saving the state of the current process and loading the saved state for the new process.This task is known as context switching.(process is nothing but a block of code which is under execution in CPU)
This defination can be simplified in this way. Whenever we are creating a process (say process A) ,a copy of process control block (PCB) is also generated, where various ingredients of a process is kept as a references. A PCB has many section like stack pointer (pointing towards a memory space) , program counter, process state, process id, block of code, cpu schedulling information,
memory management information and many more useful informations are stored. Now whenever a process wants to enter into cpu for its execution. Various informations mentioned above are stored in PCB . Now if context switching takes place while the process is executing, the cpu saves the current details of the process in the PCB and then the CPU updates its content with the ingredients of new process (say process B), After sometimes if the process A comes into existence then
it can start from the begining or start from where it was stopped. It is done by putting the saved contains of the PCB (of process A) into the CPU. And , the current information of process B is saved into its PCB copy , as it was done for process A.
So we come to a conclussion that a big task can be divided into smaller ones and they can be executed concurrently in the CPU by context switching, which increases the efficiency of CPU and helps us a lot.
Multitasking is of two types - (i) process based and, (ii) thread based.
Process based : in process based multitasking more than one programs runs concurrently in our computer. for example :
while doing facebook , we can play music in our media player.
Thread based : in thread based , a single program can perform more than one task . for example : we can write a mail at the same time we can change the format of the text. this is known as thread based multitasking.
How threads are better than process???
Threads are basically smallest unit of dispatchable codes. they are light weight task that share same address space whereas each process takes its own address space. so, there is more utilization of memory in thread based and communication in between them are easier than processes.
Let us go back to the topic "threads in java". In java threads are very much useful , it makes the whole environment asynchronous and helps to increase the cpu efficiency. Now-a-days much emphasis is given on multithreading. It helps to remove the drawbacks of single thread environment like main loop/polling mechanism. In polling mechanism, other process are not allowed to get cpu cycle,
till it gets over.Threads can work concurretly without damaging others execution.
Threads exist in several states. A thread can be running. it can be ready to run as soon as it gets cpu. A running thread can be suspended, which temporarily suspends its activity. A suspended thread can be resumed.allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At anytime , a thread can be terminated, which halts its execution immediately.Once terminataed it cannot be resumed.
Implementation of multithreading needs priority of threads , synchronization and messaging facilities to communicate with the other threads and provide a asynchronous environment.
Priority of thread means , java assigns priority to the threads according to which they are executed.Thread priorities helps us to determine which thread will get the cpu cycle. Suppose a thread of lower priority occupies cpu , and another thread of higher priority request for cpu
cycle then context switching takes place and the higher priority takes the control over cpu cycle. There are two cases when this type of context switching takes place. (i) when the thread is fully executed and , (ii) when preemptation takes place i.e forcefully control over cpu cycles
are taken.
Java multi-threading creates a asynchronous environment where more than one thread can work properly without conflicting with other threads execution. To avoid such kind of situation where conflict occurs , we need a control mechanism which can take care of it. Monitor helps us
to overcome this problem. Monitor is basically a box kind off thing in which only one thread can exist at a time. So other threads are not allowed enter into this box till the thread which is inside gets completely executed, thus there execution is stopped for a while. In this way , execution
of more than one thread is not affected and they are synchronized properly.
We know that Java threads are light weighted dispatchable codes , and they are mainly a part of big process. So these small block of codes need to communicate with each other. This is done with the help of message .Message is something which helps to communicate between the threads.
Java's messaging system allows a thread to enter a synchronized method on an object , and then wait there until some other thread explicitly notifies it to come out.
Listening music on our desktop is a very good example to start with, We all love to listen music. Everyone have there own choice, and they love to play it as many times as possible. Well to play a music
file of .mkv format (video) we need to have a music player , which is nothing but a software. This software is basically a block of code which simplifies a complex process. This program is designed to do many task simultaneously like playing the visual part , audio part and makes the interface of the media player interactive
with the user. User can change the aspect ratio and do many more things while the song is being played.This is an example of multitasking.
Formally multitasking (or time sharing) means that the cpu executes more than one job by switching among them,but the switching is so frequently done that we can interact with each program while it is running.The term switching used above is nothing but context switching. Many of us are familiar with this term.We have studied it in operating system.
Switching the CPU to another process requires saving the state of the current process and loading the saved state for the new process.This task is known as context switching.(process is nothing but a block of code which is under execution in CPU)
This defination can be simplified in this way. Whenever we are creating a process (say process A) ,a copy of process control block (PCB) is also generated, where various ingredients of a process is kept as a references. A PCB has many section like stack pointer (pointing towards a memory space) , program counter, process state, process id, block of code, cpu schedulling information,
memory management information and many more useful informations are stored. Now whenever a process wants to enter into cpu for its execution. Various informations mentioned above are stored in PCB . Now if context switching takes place while the process is executing, the cpu saves the current details of the process in the PCB and then the CPU updates its content with the ingredients of new process (say process B), After sometimes if the process A comes into existence then
it can start from the begining or start from where it was stopped. It is done by putting the saved contains of the PCB (of process A) into the CPU. And , the current information of process B is saved into its PCB copy , as it was done for process A.
So we come to a conclussion that a big task can be divided into smaller ones and they can be executed concurrently in the CPU by context switching, which increases the efficiency of CPU and helps us a lot.
Multitasking is of two types - (i) process based and, (ii) thread based.
Process based : in process based multitasking more than one programs runs concurrently in our computer. for example :
while doing facebook , we can play music in our media player.
Thread based : in thread based , a single program can perform more than one task . for example : we can write a mail at the same time we can change the format of the text. this is known as thread based multitasking.
How threads are better than process???
Threads are basically smallest unit of dispatchable codes. they are light weight task that share same address space whereas each process takes its own address space. so, there is more utilization of memory in thread based and communication in between them are easier than processes.
Let us go back to the topic "threads in java". In java threads are very much useful , it makes the whole environment asynchronous and helps to increase the cpu efficiency. Now-a-days much emphasis is given on multithreading. It helps to remove the drawbacks of single thread environment like main loop/polling mechanism. In polling mechanism, other process are not allowed to get cpu cycle,
till it gets over.Threads can work concurretly without damaging others execution.
Threads exist in several states. A thread can be running. it can be ready to run as soon as it gets cpu. A running thread can be suspended, which temporarily suspends its activity. A suspended thread can be resumed.allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At anytime , a thread can be terminated, which halts its execution immediately.Once terminataed it cannot be resumed.
Implementation of multithreading needs priority of threads , synchronization and messaging facilities to communicate with the other threads and provide a asynchronous environment.
Priority of thread means , java assigns priority to the threads according to which they are executed.Thread priorities helps us to determine which thread will get the cpu cycle. Suppose a thread of lower priority occupies cpu , and another thread of higher priority request for cpu
cycle then context switching takes place and the higher priority takes the control over cpu cycle. There are two cases when this type of context switching takes place. (i) when the thread is fully executed and , (ii) when preemptation takes place i.e forcefully control over cpu cycles
are taken.
Java multi-threading creates a asynchronous environment where more than one thread can work properly without conflicting with other threads execution. To avoid such kind of situation where conflict occurs , we need a control mechanism which can take care of it. Monitor helps us
to overcome this problem. Monitor is basically a box kind off thing in which only one thread can exist at a time. So other threads are not allowed enter into this box till the thread which is inside gets completely executed, thus there execution is stopped for a while. In this way , execution
of more than one thread is not affected and they are synchronized properly.
We know that Java threads are light weighted dispatchable codes , and they are mainly a part of big process. So these small block of codes need to communicate with each other. This is done with the help of message .Message is something which helps to communicate between the threads.
Java's messaging system allows a thread to enter a synchronized method on an object , and then wait there until some other thread explicitly notifies it to come out.