Wednesday, July 30, 2014

Introduction to Threads in java

Introduction to Threads

sMultithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.

Comparative to other languages, multithreading is very easy and simple in Java. The built-in support for threads is simple and straight so that any programmer can do with multithreading operations without confusion and special attention. Multithreading uses very effectively the microprocessor time by reducing its idle time. When the processor's idle time is reduced, the output of the programs comes earlier.

Concept to understand :-

Multitasking, Multiprocessing and Multithreading
A program under execution is called a process. : "performing multiple tasks at the same time is called multitasking ". This is very wrong and is the result of misunderstanding the concept.

Every computer person knows well that microprocessor can execute only one instruction (task) at a time. That is, the earlier definition is going wrong somewhere else. This is the point of confusion about multithreading for everyone.
When a process is being executed, there may be an interruption, sometimes, for execution; for example, a keyboard input (scanf) may not be fed immediately. When not fed, the execution is halted (observe, the cursor will be blinking even for the whole day when the input is not given).
 Now the processor is free and idle. We can ask the microprocessor to do another process meantime. The processor obliges (as underlying OS supports multitasking). Now, two programs are under execution – the first one half the way completed and kept apart and the other being executed. When the second task (process) is over, if the input is ready, the control will be shifted to the first process and now the first process is executed completely. The result is output of both the programs is obtained earlier. All this is transparent (not known) to the user as context switching occurs at very fast speed.

1.   Multitasking
"Shifting the microprocessor to execute another process when the first process is stopped its execution, is known as multitasking". Shifting between the processes is known as context switching. Do not shift unnecessarily when the first program is going smooth with its execution as context switching takes more time and performance looses. Shift only when a running process is stopped for some reason.
2.     Multiprocessing
Putting multiple processors (say two) and feeding multiple processes simultaneously is known as multiprocessing and is a very rare phenomenon as multiple processors on desktop PCs is uncommon (only super computers have).
3.   Multithreading
Multithreading is the Java's style of achieving multitasking. A single Java program can be divided into a number of threads and executed simultaneously while shifting between the threads when one stops its execution. The current tutorial discusses how to divide a Java program into multiple threads and how to execute them.
Simultaneously, concurrently, parallel

"At the same time" is a confusing idiom. Many processes may exist whose execution is stopped in the middle for some reason. Remember, only one can be active at any time. Which ever process is ready is called and executed. "Many" must be interpreted this way. It looks all at a time as context switching happens very fast (like cinema where a number of frames of images are run is such a speed, our eye cannot catch).

Another term to understand :

Preemptive multitasking and Time-sharing

1.     Preemptive multitasking
At a given time, a number of processes may be getting executed on the CPU. CPU allocates a certain period of time, known as time-slice, for each process to execute. Each process is guaranteed of a time-slice. In preemptive multitasking ,

2.   Time-sharing
one process empties (forcibly evacuating) the other process for the time-slice period and thus every process definitely gets the CPU time. Thus, CPU time is shared by all the processes and is known as time sharing. Time sharing is a CPU architecture where CPU distributes its time equally to all the processes waiting for execution.

Preemptive and Cooperative Multitasking

In multitasking two types of algorithms exists
– preemptive multitasking and cooperative multitasking.

Each process is allotted a certain period of time known as time slice. With time slices, the waiting processes get evenly all the CPU time and one process cannot hold the CPU time as long it wants.
one process can hold the microprocessor time as long as it would like. But the CPU is empowered to remove the process if the process does not utilize the time after holding it. Preemptive multitasking is supported by OS/2, UNIX, Windows NT etc. Cooperative multitasking is supported by Windows 3.1, UNIX, Windows NT etc.

When an immediate action or attention is required, like keyboard input or a button click, preemptive is preferred. The disadvantage of cooperative multitasking is, if designed poorly, it may hang the system.

Preemptive Scheduling and Non-preemptive Scheduling
For forcing the thread to vacate the microprocessor, two properties can be considered – priority and waiting time.

 In preemptive scheduling, a thread with more priority vacates the executing thread of low priority. That is, low-priority thread yields to the high-priority thread.
In non-preemptive scheduling, waiting time is considered and not priority. That is, the high-priority thread cannot relinquish a low-priority thread having more waiting time.

No comments:

Post a Comment