Getting started with basics of multithreading(java)
Some basics about multithreading......
- Java provides inbuilt support for multithreading.
- we can say that thread is a part of program.
- Each thread has its seperate path for execution.
What does multitasking means ..?
By simple meaning one can say that multitasking means more than one task at a particular instant of time that is not in sequence or not in chain.
Here in java we have two sides of multitasking one is multitasking based on process and other is multitasking in terms of thread .
so multitasking
1) process based
2) thread based
So what is the difference between these two ?
- when we say that it is process based multitasking then we have to think that more than one program are executing concurrently.
for example a java compiler is working currently with the text editor working parallel to it.
- one thing we should keep in mind that the program is the smallest unit of code to be dispatched by the schedular .(to dispatch means to send on getting something which it needs).
now lets talk about the thread based multitasking....
- In thread based multitasking smallest unit of code is the thread to be dispatched by the schedular.
- here for example we can say that in a single program , two or more tasks are performed simultaneously.
- we can give a text editor followed by two other functions spell check and print or any other ....
Now by this we can say that process deals with the big picture and thread handles the details of it.
Difference between the thread and process:-
first difference is multitasking threads require less overhead than multitasking process.
now the most confusing topic that what is overhead.?
- overhead is the most repeated word in the multithreading topic so overhread meams we can say creation+dispatching+running time and other cost are added and also the concept of mutex lock and unlock time for getting the shared resource in the critical region as wellas set process and thread to run.
now diicussing further about multithread again back in the topic of difference between the thread and process .
- Thread is a part of procress suppose threads are three say that p1,p2,p3 say p1 has adress of 100- 108, p2 process has address space of 600-800 and p3 has address space of 1000 -1004.
- now if the threads work in it they share the common address space say if for process 1 if they have 5 threads then they all share the same address space of 100-108
- for process 2 again if we take different threads then they all share the same adress space 600-800 so they all have the same address space within the process.
- so the context switching of threads are more faster than the process as they share two or more threads have the same address space
- so one can say that threads are light weight and process are heavy weight.
- multithreading enables you to write program and make maximum use of cpu as process didn't because in threads if one dosent work then other is ready but before working with other thread stores the status of the first thread and goes beyond working with the other thread.
- thread also has the priorities set for it generally priority is in the integer term which has nothing to do that if the priority is more this thread is importance but this concepts says that which thread has to be executed next so as to make the execution in adifferent way and has a default thread the main thread that all priority portion we will take in the next post.
- Normally if in a single thread , if one task is going on another cant go in execution as it has to wait for the previous one to complete even though the cpu is idle .
now talking about the java thread model
- primarily there are two parts for any thread to work event loop + polling keep in mind these two things that primarily these two process are in picture polling and event loop
- event loop goes in the event handler but first of all both are in the infinite loop.
what is polling first of all.?
what i understood polling means to check continuously whether the program is in or not (checks the status).It is a single event queue which decides what to do next.
for example it returns that file is ready to be stored so by this event loop dispatches the control to eventhandler and until the event handler returns no else work can be done so if the cpu is idle then also no work could be proceeded so ultimately this is the waste of time .
and this polling is removed in multithreading ,will discuss it in the next post.
for example it returns that file is ready to be stored so by this event loop dispatches the control to eventhandler and until the event handler returns no else work can be done so if the cpu is idle then also no work could be proceeded so ultimately this is the waste of time .
and this polling is removed in multithreading ,will discuss it in the next post.
will proceed in the multithread in the next post about thread block, implementation, thread class etc ......
Comments
Post a Comment