Java
  Home arrow Java arrow The Basics of Multiple Threads in Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The Basics of Multiple Threads in Java
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2009-02-25


    Table of Contents:
  • The Basics of Multiple Threads in Java
  • The Basics and the Thread class
  • The Runnable Interface
  • Taking a Break!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Basics of Multiple Threads in Java
    ( Page 1 of 4 )

    In this day and age programmers must implement multi-threading solutions into their code. Every somewhat modern programming language that respects itself offers opportunities to work with threads. Today we’re going to learn how to do this in Java. This article requires at least a little knowledge of Java such as classes, objects, inheritance and interfaces. We’ll try to keep it simple. Untitled

    Before we jump into the middle of everything, we must learn the basics so that we won't get lost during the coding process later on. First of all, a thread is a single sequential flow of control-each thread within a program becomes its own thread of execution. This is how an application can execute multiple threads concurrently. The most critical advantage of doing so is maximizing CPU power and throughput. 

    The simplest way to imagine two threads is to consider the following mathematical equation: 2 + 5 + 9 + 3. It's simple, right? We promised to keep it simple. Jokes aside, the entire equation is composed of three addition operations. The sequential solution involves one solo thread calculating 2 + 5 = 7, then adding 9 to this, which gives 16, and ultimately adding 3. The end result is 19. 

    Now please bear with me but let's imagine that one addition takes one second (I know, I know!). This is not a computer at work; let's imagine that it's a six-year old kid practicing math. The entire process took three whole seconds until the kid shouted out the final answer (hence 2 + 5 = 7; 7 + 9 = 16; 16 + 3 = 19). Now what would happen if we brought in another pupil and tried to make them cooperate? 

    While the first kid calculates 2 + 5, the second kid calculates 9+3. Assuming that both of them require equally one second to calculate, this means both results are calculated in the first second. Now the first kid finally adds those two results and finds out that 7 + 12 is 19. This addition once again took another second. But hey! The entire calculation process took a total of two seconds. It ran 33 percent faster than the original process!  

    The above analogy might seem silly and long-winded, but that's actually one of the simplest ways to imagine multi-threading. Of course, computers can work with bigger numbers and under shorter time periods, but the bottom line is still the same. Now that we know what this is about, let's fire up our favorite Java IDE and begin coding. 



     
     
    >>> More Java Articles          >>> More By Barzan "Tony" Antal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT