Leave a Message To Me

Monday, April 19, 2010

Thread in Object Oriented Language

Threads in Programming Languages

All human senses sight, touch, smell, taste and hearing can be employed at once. Computers, too, can perform several operations concurrently. It is common for computers to compile a program, send a file to a printer and receive electronic mail message over a network concurrently. Computer can do that by applying a technical concept called threads. Technically, a thread is defined as an independent stream of instructions that can be scheduled to run two or more tasks in overlapping duration within the same operating system [1,p.155]. This means that threads provides a single flow of control with a process that runs independently to perform a great variety of operations in parallel. A process is active execution of one or more programs. The concept of thread was first developed by the United States Department of Defence that made concurrency widely available to defence contractors building military command and control system [2,p.1045].

Each thread has a life-cycle during which it goes through a number of states. A thread can be in only one state at a given point in time. Figure 1 explains that a new thread begins its life cycle in the new state. It remains in this state until the program starts the thread, which places the thread in the runnable state. A thread in the running state is executing its task [3,p.179]. Sometime a runnable thread transitions to the suspend-blocked state while it waits for another thread to perform a task. Also, according to figure1, the suspend-blocked thread transitions back to the running state only when another thread notifies it to continue executing. The runnable thread transitions to the blocked state when it attempts to perform a task that cannot be completed immediately and the thread must temporarily wait until that task completes. At this point, the blocked thread transitions to the runnable state, so it resumes execution [4,p.189]. A blocked thread cannot use a processor even if one is available. Finally, runnable thread enters terminated state when it successfully completes its task or due to an error. These states are virtual machine states which do not reflect any operating system thread states [1,p.188].


In most operating system, each thread is given a small amount of processor time called a quantum or timeslice by which to perform a task. When the thread's quantum expires, the thread returns to the ready state and the operating system assigns another thread to the processor [2, 3].
There are single and multithreading executions in threads. Single thread is going through “the computation of the entire task from the beginning to the end without being interrupted [3,p.198]”. Multithreading, on the other hand, each application contains separate threads of execution, where each thread has its own “stack” and problem counter. This allows all applications to execute concurrently with other threads while sharing memory. The problem with single threaded applications that can lead to poor responsiveness is that “lengthy activities must complete before others can begin [4.p,192]”. In multithreaded applications, threads can be distributed across multiple processor so that multiple tasks execute concurrently and the application can operate more efficiently. Multithreading can also increase performance on single-processor system that simulates concurrency. This happens when one thread cannot proceed, another can use the processor.

In conclusion Threads are a way for program(s) to split into two or more simultaneously running tasks. They represents single sequence of instructions executed in parallel with other sequence by time slicing.



Reference:
[1] S. Prasad, Multithreading Programming Techniques, New York, McGraw-Hill, Inc, 1997.
[2] H. Deitel, Paul J. Deitel , Java how to program, New York, 7th Edition, Prentice Hall, 2007.
[3] Gent, Operating Systems: A Modern Perspective, Third Edition, University of Colorado, 2004
[4] A. Fourouzan, F. Gilberg, A Structured Programming Approach Using C, (3rd Edition), Brooks/Cole publishers, 2003.

0 comments: