Home arrow Java & J2EE arrow The Basics of Multiple Threads in Java

The Basics of Multiple Threads in Java

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.

TABLE OF CONTENTS:
  1. The Basics of Multiple Threads in Java
  2. The Basics and the Thread class
  3. The Runnable Interface
  4. Taking a Break!
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 49
February 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 & J2EE Articles          >>> More By Barzan "Tony" Antal
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: