Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javatutorial Multithreading Java Tutorial for Beginners

Related Tutorials


 
 

Share on Google+Share on Google+

Multithreading Java Tutorial for Beginners

Advertisement
Multithreading in Java is used to run two or more threads simultaneously inside a process so that the time of processor is saved. A thread never exists on its own.

Multithreading in Java means two or more parts of program run simultaneously. Every single part is called thread. Running them simultaneously saves time. Different threads run in simultaneous mode.

Multithreading Java tutorials for beginners provides you examples and codes with explanations that simplifies the multithreaded programming.

A thread runs inside the process, which consists of memory space allocated by operating system. A thread never exists on its own and always is a part of process. A process can have many threads that run simultaneously.

Using multithreaded programming, programmer can make efficient programs that make the maximum use of CPU and since many threads runs simultaneously and execution time is less.

A thread inside the multithreaded programming exists at different stages from being new to being dead.

First Stage (New):

At first stage, thread is created but is not initialized

Second Stage (Ready-to-Run)

At this stage, a thread is available to be run at any time CPU gives it a time slot. Hence this stage is called as ready to run stage.

Third Stage (Running):

Here the thread is running as CPU has allotted it the time slot

At this stage a thread can enter into further stages that are:

  • Sleeping:
  • Here the thread is at a sleeping position because it is not being processes at the time. By moving threads to sleeping position for a certain time we save processor time.

  • Blocked for Join Completion:
  • Here the thread has completed its task but is waiting for another thread, which will complete its task and then both the threads will be executed together.

  • Blocked for I/O:
  • Here the thread is waiting for the completion of blocking operation.

  • Waiting for notification:
  • Here the thread is waiting for a notification from another thread. The time it gets the notification it returns to Ready-to-Run stage.

  • Blocked for lock acquisition:

Here a thread is waiting to acquire the lock of an object.

Last Stage (Dead):

The final stage the thread after the completion of task has been terminated.

Threads have priority, which ranges from minimum to normal to maximum. By default normal priority is given to every thread. Maximum priority thread is allocated more processor time; normal priority thread is allocated less processor time while the minimum priority thread has the least processor time. Execution of the thread does not depend on the priority rather is platform dependent.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 20, 2013

Related Tutorials

Discuss: Multithreading Java Tutorial for Beginners  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com