Lesson 1
Introduction to the Java Threading Model
Threads are a very important part of the Java Programming Language. They allow you to create applications that maximize the use of processing resources by interweaving the execution of multiple code sequences. Learning to program using threads is an essential Java programming skill.
As such, threads are an important topic on the certification exam. You can expect to see several questions that will test your knowledge of how threads are
- created,
- started, and
- executed.
You will also have to know how threads move from one execution state to another and how threads access shared resources.
Some exam questions will ask you to examine examples of program code and identify specific points within the code where threads are created and change state.
Module objectives
This module will introduce you to threads, cover the ins and outs of threads programming, and help you to satisfy the following exam objectives:
- Write code to define, instantiate, and start new threads using both
java.lang.Thread
class and the java.lang.Runnable
interface.
- Recognize conditions that might prevent a thread from executing.
- Write code using synchronized
wait()
, notify()
, and notifyAll()
methods to protect against concurrent problems and to communicate between threads.
- Define the interaction between threads, and between threads and object locks when executing synchronized
wait()
, notify()
, or notifyAll()
methods.
In addition to the above, you will cover the topics of thread execution states and thread scheduling.
In Java, "thread" means two different things:
- An instance of class
java.lang.Thread
- A thread of execution.
An instance of Thread is just an object. Like any other object in Java, it has variables and methods, and lives and dies on the heap.
But a
thread of execution is an individual process (a "lightweight" process) that has its own call stack. In Java, there is one thread per call stack, or to think of it in reverse,
one call stack per thread.
Even if you do not create any new threads in your program, threads are running in the background .
Nomenclature and Terms
In this module the terms
Ready and
Runnable mean the same thing.
Most of the diagrams in this module use the term
Runnable and in some of the diagrams you will see the word "Ready"
This is the nomenclature that is used by Oracle.
Java SE 8 - Runnable
RUNNABLE: public static final Thread.State RUNNABLE
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
On the following page in this module
Thread States
the term "created" is used in the diagram. The term "created" is the same as the term "New".