Creating Java Threads

A thread allows you to do more things at once. It’s like a family sitting at the dinner table with in the middle one big pot of spaghetti (CPU). Every member of the family would be a thread and everybody gets its turn to eat some.

For Sun’s definition, check here: What is a thread?

There are several ways to create a thread. The following are a couple of examples that will each perform the same two tasks: 1) ask for first and last name of user with System.in 2) calculate the sum of all numbers from 1 to 123456789. While the program is waiting for input from the user, it is meanwhile performing the calculation.

Method 1

Extend from Thread class, define a public void run() method (the starting point of a thread) and call start on an instance of that class:

ThreadExample1.java:

import java.io.*;
 
public class ThreadExample1
{
   public static void main(String []args) {
      // create instance of thread1 and 2
      UserInputThread thread1   = new UserInputThread();
      CalculationThread thread2 = new CalculationThread();
 
      // start up the threads, start is defined in the
      // Thread class and the VM will call the run() method
      thread1.start();
      thread2.start();      
   }
}
 
// Thread 1: ask for user input
class UserInputThread extends Thread
{
   public void run() {
      try {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("What is your first name?");
         String firstName = br.readLine();
         System.out.println("What is your last name?");
         String lastName  = br.readLine();
 
         System.out.println("Hi " + firstName + " " + lastName);
      }
      catch(IOException e) {
         e.printStackTrace();
      }
   }
}
 
// Thread 2: perform some calculation
class CalculationThread extends Thread
{
   public void run() {
      int n = 123456789;
      long total = 0;
      for (int i=1; i<n; i++) {
         total += i;
      }
      System.out.println("Total of summing up numbers from 1 to " + n + " = " + total);
   }
}

Method 2

Mark the class you want as a thread with the interface Runnable, define a public void run() method (the starting point of a thread), pass the Runnable class to a new instance of a Thread and call start on it:

ThreadExample2.java:

import java.io.*;
 
public class ThreadExample2
{
   public static void main(String []args) {
      // create an instance of each class, both implement
      // the interface Runnable 
      Runnable thread1 = new UserInputThread();
      Runnable thread2 = new CalculationThread();
 
      // create instances of Thread and pass the Runnable 
      // objects.  Starting up the threads will cause the
      // VM to call run()
      new Thread(thread1).start(); 
      new Thread(thread2).start();
   }
}
 
// Thread 1: ask for user input
class UserInputThread implements Runnable
{
   public void run() {
      try {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("What is your first name?");
         String firstName = br.readLine();
         System.out.println("What is your last name?");
         String lastName  = br.readLine();
 
         System.out.println("Hi " + firstName + " " + lastName);
      }
      catch(IOException e) {
         e.printStackTrace();
      }
   }
}
 
// Thread 2: perform some calculation
class CalculationThread implements Runnable
{
   public void run() {
      int n = 123456789;
      long total = 0;
      for (int i=1; i<n; i++) {
         total += i;
      }
      System.out.println("Total of summing up numbers from 1 to " + n + " = " + total);
   }
}

Method 3

Compact, anonymous instance:

ThreadExample3.java:

import java.io.*;
 
public class ThreadExample3
{
   public static void main(String []args) {
      // create thread 1, override run() and start it
      new Thread() {
         public void run() {
            try {
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("What is your first name?");
               String firstName = br.readLine();
               System.out.println("What is your last name?");
               String lastName  = br.readLine();
 
               System.out.println("Hi " + firstName + " " + lastName);
            }
            catch(IOException e) {
               e.printStackTrace();
            }
         }
      }.start();
 
      // create thread 2, override run() and start it
      new Thread() {
         public void run() {
            int n = 123456789;
            long total = 0;
            for (int i=1; i<n; i++) {
               total += i;
            }
            System.out.println("Total of summing up numbers from 1 to " + n + " = " + total);
         }
      }.start();
   }
}