Introduction to Java Multithreading - An Example



Before we start coding our example, we should talk about the 2 different ways of creating threads in Java. The first way is extending the Thread class and the other way is implementing the Runnable interface. Second method is technically more efficient, since you can extend only one class in Java but you can implement as many interfaces as you want. By implementing Runnable interface, you still have room to extend another class. 

Now let's see these two different methods in our example:

Extending Thread Class


/**
 * FindEven -- Finds as many even numbers as it can in 2 milliseconds
 */
public class FindEven extends Thread {
 public static final int MAX = 1000;
 public static final int TIME = 2;
 public volatile boolean finished = false;

 public void run() {
  int[] numbers = new int[MAX];
  int count = 0;
  for (int i = 0; i < MAX; i++) {

   // Check to see if the timer has expired
   if (finished)
    break;

   if (i % 2 == 0) {
    numbers[count++] = i;
    System.out.println("Found even: " + i);
   }
  }
 }

 public static void main(String[] args) {
  FindEven finder = new FindEven();
  finder.start();
  try {
   Thread.sleep(TIME);
  } catch (InterruptedException e) {
   System.out.println("Exception happened!");
  }
  finder.finished = true;
 }
}

Whenever we create an object of the subclass of Thread class and call the start method of that object, run() method of that object is called. Run() method holds the program logic and the expected task of the thread. On the other hand, by invoking the sleep method of the Thread class, we tell the main thread to sleep for 2 milliseconds and then to stop the finder thread by setting finished variable to true. When the finder thread realizes that the finished variable is true, it breaks the loop and the program ends. And here is the output:

Found even: 0
Found even: 2
Found even: 4
Found even: 6
Found even: 8
Found even: 10
Found even: 12
Found even: 14
Found even: 16
Found even: 18
Found even: 20
Found even: 22
Found even: 24


Implementing Runnable Interface



/**
 * FindEven -- Finds as many even numbers as it can in 2 milliseconds
 */
public class FindEven implements Runnable {
 public static final int MAX = 1000;
 public static final int TIME = 2;
 public volatile boolean finished = false;

 public void run() {
  int[] numbers = new int[MAX];
  int count = 0;
  for (int i = 0; i < MAX; i++) {

   // Check to see if the timer has expired
   if (finished)
    break;

   if (i % 2 == 0) {
    numbers[count++] = i;
    System.out.println("Found even: " + i);
   }
  }
 }
 
 public static void main(String[] args) {
  FindEven finder = new FindEven();
  Thread t = new Thread(finder);
  t.start();
  try {
   Thread.sleep(TIME);
  } catch (InterruptedException e) {
   System.out.println("Exception happened!");
  }
  finder.finished = true;
 }
}

And here is the result:

Found even: 0
Found even: 2
Found even: 4
Found even: 6
Found even: 8
Found even: 10
Found even: 12
Found even: 14
Found even: 16
Found even: 18
Found even: 20
Found even: 22
Found even: 24
Found even: 26
Found even: 28
Found even: 30
Found even: 32
Found even: 34
Found even: 36

Continue Reading the Next Part of This Tutorial : Sharing Access to Data -->
Author:

Software Developer, Codemio Admin

Disqus Comments Loading..