Home arrow Java & J2EE arrow Page 2 - The Basics of Multiple Threads in Java

The Basics and the Thread class - 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.

  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



A traditional single-threaded application runs into a point where it "takes time" to accomplish a task, and then the entire program halts until that operation is completed, at which time the thread is able to move further. With multi-threading execution, if one thread "takes time," it won't block the flow of execution of other threads.  

We should also note that if one of the threads modifies a thread-specific variable, then all of the threads will end up "noticing" and working with the new value. Threads are always in one of the following states: running, resumed, ready to run, suspended, or blocked

The first is self-explanatory. The second is a kind of "ready to run" state after being suspended or blocked. A thread in the third state is obviously ready but has not started yet. The fourth state means the thread is temporarily suspended, while a thread in the final state is waiting for something.

Each thread also has a given priority. These are numeric values ranging from 1 to 10. The default priority level is 5 (NORM_PRIORITY constant). A higher priority thread during execution has priority over a lower valued one. The highest priority level, 10, is referred to by the constant MAX_PRIORITY, while the lowest 1 is akin to MIN_PRIORITY. These values are final static int's (meaning "constants" in everyday coding language). 

Under Java there are two possible ways to create threads. The first route involves doing a simple inheritance from the Thread class. The Thread class can be found inside the java.lang package. Extending this thread with a sub-class means inheriting the parent's attributes and methods. We are going to cover this a bit later, as well as the other solution, which is implementing the Runnable interface. Moreover, we'll explain the differences. 

For now let's stick to the first route. What we're going to do once the inheritance is done is override the run() method within the sub-class. This is the place where we should actually write the part that tells it "what to do" once launched. Furthermore, we can create an object instance of the sub-class, and right after this we can call the start() method of the aforementioned object that kick-starts the thread. 

class MyThread extends Thread{

private int a;


public MyThread(int a){

this.a = a;


public void run(){

for (int i = 1; i <= a; ++i){

System.out.println(getName() + " is " + i);




catch(InterruptedException e){}





class MainMyThread{

public static void main(String args[]){

MyThread thr1, thr2;

thr1 = new MyThread(5);

thr2 = new MyThread(10);





The output of the above code snippet is the following. As you can see, the threads are being run simultaneously, so the lines are printed one after another at the same time until the count of five is reached. After that, only the second thread continues. The first thread dies since it has done its work. 

Thread-0 is 1

Thread-1 is 1

Thread-0 is 2

Thread-1 is 2

Thread-0 is 3

Thread-1 is 3

Thread-0 is 4

Thread-1 is 4

Thread-0 is 5

Thread-1 is 5

Thread-1 is 6

Thread-1 is 7

Thread-1 is 8

Thread-1 is 9

Thread-1 is 10

In the code snippet above we have explicitly called the start() method of the two threads. This is one way of doing it. However, some people prefer launching the start() method instantaneously as soon as the object instance is created (basically, a new thread is initialized). You can do this by calling the start() method from the constructor. 

public MyThread(int a){

this.a = a;

start(); // this is the extra line, it starts the threads!


On the next page we're going to tackle the other solution for creating new threads. 

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: