4.1 Thread CreationHomepage  « Java5 Certification « 4.1 Thread Creation

We start this section by looking at thread definition, instantiation and execution using the java.lang.Thread and java.lang.Runnable classes.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 4: Concurrency

    • Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable.

Thread Basicsgo to top of page Top

When we talk about concurrency in this section we are referring to two or more threads of execution running simultaneously, although the same terminology can be applied to concurrent processes. You can think of each thread as separate stacks that operate independently of each other. Whether this is on a single processor, where the underlying operating system schedules the threads to run, so they appear to be running in tandem, or on multiple processors where the threads are actually running in tandem, as far as we are concerned the threads are running concurrently.

Whenever we start up the JVM a thread is created, that usually calls the main() method of the invoking class. This initial thread is a non-daemon thread, also known as a user thread. When we create our own threads, these can be user or daemon threads. So what's the difference? Well daemon threads are background threads that are subordinate to the thread that created them. When the creating thread dies, so do any daemon threads created by it. Every thread we create takes on the characteristics of the thread used to create it, so user threads beget user threads and daemon threads beget daemon threads. Created threads are also given a priority, which is initially the same as the thread used in its creation. We can of course override the default characteristics of thread creation using methods of the Thread class, which are discussed in the Concurrency - Thread Basics lesson, and we can also alter thread priorities as shown in the Concurrency - Thread Priorities lesson.

The JVM will continue executing threads until either of the following happens:

  • All user threads have died in one of the following ways:
    • By returning from the call to the run() method.
    • By throwing an exception that has propagated beyond the run() method.
  • The exit() method of the Runtime class has been called and the security manager has allowed the exit operation to occur.

There are two ways of creating a thread in Java:

  1. By declaring a subclass of the Thread class where the subclass we create should override the run() method of the Thread class.
    1. See Creating A Single Thread
    2. See Creating Multiple Threads
    for examples of how to subclass the Thread class.
  2. By declaring a class that implements the Runnable interface.
    1. See Creating A Single Thread
    2. See Creating Multiple Threads
    for examples of how to implement the Runnable interface.

So which type of thread instantiation should we use? When we subclass the Thread class our threads have to inherit from Thread and therefore we are not only limited to that classes functionality but can't extend anything else. Fortunately for us we can also execute independent threads by implementing the Runnable interface which declares a single run() method that gets executed when the thread is started. Using this approach we are free to extend another class if we need to for added functionality.

This is possible because several of the Thread classes overloaded constructors accept a Runnable object as a parameter. So as long as we implement this interface and use our implementing class in the threads construction, then the thread knows to use the implementing classes overridden run() method when the thread is started.

Related Java5 Tutorials

Concurrency - Thread Basics
Concurrency - The Runnable Interface

<<  Concurrency                    4.2 Thread States  >>

go to home page Java5 Tutor Homepage go to home page Top

All the Java5 Certification Concurrency lessons are listed below. Click a link to go to that lesson.

4: Concurrency

4.1 Thread Creation

Thread Basics

4.2 Thread States

4.3 Synchronization