Java Multitasking  «Prev  Next»

Lesson 12

Java Thread Evolution


Java Multitasking
Much of what there was to learn about programming with Java threads did not chang dramatically between the release of Java 1.0 and Java 1.6. From the beginning, Java supported concurrency in the form of low-level threads management, locks, synchronization, and APIs for concurrency. The elements of the Java 1.0 model consisted of the Thread class, Runnable interface [1], and synchronized keyword.
Since 5.0, Java also supports high-level concurrency APIs in its java.util.concurrent package. The APIs for concurrent programming involve high-level APIs to exploit today's multi-core hardware, in which a single processor has multiple cores. These APIs are also useful for exploiting concurrency in machines that support multiple processors.
Most of the Java concurrency utilities are provided in the java.util.concurrent[2] package. Classes to efficiently update shared variables without using locks are provided in the java.util.concurrent.atomic subpackage. The Lock interface and the classes deriving from it are provided in the java.util.concurrent.locks subpackage.
In this Java threads primer, I will discuss some of the high (and low) points of threads as a concurrent programming [3] technique. Get an overview of what is challenging about multithreaded programming and find out how the Java platform has evolved to meet some of the challenges. Concurrency is among the greatest challenges for newcomers to Java programming. Java threads have become easier to work with as the Java platform has evolved. In order to learn how to do multithreaded programming in Java 6 and 7, you really just need some building blocks. We will start with these: This lesson contains a survey of Java threading techniques.

Simple threaded program

class ThreadingExample {
 public static void main (String [] args) {
/* The second argument is a delay betweensuccessive outputs.  
The delay is measured in milliseconds. "10", for instance, means, 
"print a line every  hundredth of a second". */
   ExampleThread mt = new ExampleThread("A", 31);
   ExampleThread mt2 = new ExampleThread("B", 25);
   ExampleThread mt3 = new ExampleThread("C", 10);
   mt.start();
   mt2.start();
   mt3.start();
 }
}
class ExampleThread extends Thread {
 private int delay;
 public ExampleThread(String label, int d) {
 // Give this particular thread a  name: "thread 'LABEL'".
  super("thread '" + label + "'");
  delay = d;
 }
 public void run () {
  for (int count = 1, row = 1; row < 20; row++, count++) {
   try {
    System.out.format("Line #%d from %s\n", count, getName());
    Thread.currentThread().sleep(delay);
   }
   catch (InterruptedException ie) {
   // This would be a surprise.
   }
  }
 }
}

In the simplest Java programs, there is a guarantee of order-of-execution: the first line in main() will be executed first, then the next, and so on, with appropriate tracing in and out of other methods.
[1]Runnable interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
[2]java.util.concurrent: The java.util.concurrent package contains utility classes commonly useful in concurrent programming.
[3]Concurrent programming: 1. The writing programs which are divided into independent tasks. 2. Tasks may be executed in parallel or on multiprocessors.