Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Modern threading: A Java concurrency primer

Understanding Java threads in a post-JDK 1.4 world

  • Print
  • Feedback

Much of what there is to learn about programming with Java threads hasn't changed dramatically over the evolution of the Java platform, but it has changed incrementally. In this Java threads primer, Cameron Laird hits some of the high (and low) points of threads as a concurrent programming technique. Get an overview of what's perennially challenging about multithreaded programming and find out how the Java platform has evolved to meet some of the challenges.

Concurrency is among the greatest worries for newcomers to Java programming but there's no reason to let it daunt you. Not only is excellent documentation available (we'll explore several sources in this article) but 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'll start with these:

This article is a beginner's survey of Java threading techniques, including links to some of JavaWorld's most frequently read introductory articles about multithreaded programming. Start your engines and follow the links above if you're ready to start learning about Java threading today.

 

A simple threaded program

Consider the following Java source.

Listing 1. FirstThreadingExample

class FirstThreadingExample {
   public static void main (String [] args) {
    // The second argument is a delay between
    //   successive 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.
         }
      }
   }
}

Now compile and run this source as you would any other Java command-line application. You'll see output that looks something like this:

Listing 2. Output of a threaded program

Line #1 from thread 'A'
Line #1 from thread 'C'
Line #1 from thread 'B'
Line #2 from thread 'C'
Line #3 from thread 'C'
Line #2 from thread 'B'
Line #4 from thread 'C'
   ...
Line #17 from thread 'B'
Line #14 from thread 'A'
Line #18 from thread 'B'
Line #15 from thread 'A'
Line #19 from thread 'B'
Line #16 from thread 'A'
Line #17 from thread 'A'
Line #18 from thread 'A'
Line #19 from thread 'A'

That's it -- you're a Java Thread programmer!

Well, okay, maybe not so fast. As small as the program in Listing 1 is, it contains some subtleties that merit our attention.

  • Print
  • Feedback

Resources

Threads programming

Thread synchronization

Locks and monitors

Testing and debugging threads

Threads & alternatives