Wednesday, September 9, 2009

Java Thread Basics || Learn Java Threads

Most of the functions in a computer needs various things to be done simultaneously.Like if we are playing a computer game then there are various things going on at the same time,like graphics,sound,gameplay,commands,scoring etc.Do you ever imagine how all this happens? The answer is it is done with the help of Threads.Threads are the sequence of computer instructions which carry out specific tasks.Now you will say that "wait this is the definition of a computer process isn't it".Yes you are absolutely right,but since the Threads are small unit of execution and they also have start and end and they complete a specific task so this definition also applies to Threads.A process is a larger unit of sequence of instructions.A thread is not a process on its own but it exists within a Thread.A Thread has no unique existence.Every process has atleast a Thread called primary Thread,you can create as many as you want.The Microprocessor divides the memory among the processes that we execute.Each process occupies that address space and the Threads within the process share same address space.That is why I said "Thread cannot exist on their own".It is clear from the following figure:-



Threads are very useful when you have large computations at your side.Threads divides the task into multiple threads and each on finishes its task.This way Threads accomplish a faster execution time and complete utilisation of Computer resources.

The resources like memory,devices and data of program and context of program are also available to the Threads of the program.A Thread is also known as a "Lightweight process" or "execution context".This is because there are fewer overloads on the processor when it switches from one Thread to the other,than when it switches from one Process to the other.The above diagram also shows the relationship between a Process and a Thread.

SingleThreaded and MultiThreded Application


A Process in general are of two types

1.Single Threaded
2.Multi Threaded

A process which is made up of single thread is known as single threaded process.It is like performing a task at a particular time.No other simultaneous thread can execute.The next thread can only execute on the completion of previous Threads completion.You can understand more from figure see below.


Single Thread within a process which runs serially and perform tasks



A Process having more than one thread is known as MultiThreaded Process.In MultiThreaded process multiple threads can run and perform various tasks at the same time for Example:-In a computer game we have a lot of things going on at the same time like Gameplay,Scoring,Sound,Controlling and so on.These all are handled by individual threads which works with each other or execute at the same time to produce the results.More understanding from figure see below.




Thread Input(two thread running simultaneously within a process independently)


Are you Intelligent Compete with world to prove

Suggested Reading:-

Thread Life cycle
Ways to create Threads

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP