Lesson 1
Java Threading Model 1.1
Here's a breakdown of the purpose of the Java Threading Model in Java 1.1:
- Simultaneous Tasks:
- The primary purpose was to enable applications to perform multiple tasks concurrently, even on single-processor systems.
- This allowed for better responsiveness, improved resource utilization, and the ability to handle tasks that would otherwise block the main execution thread.
- Building Responsive Applications:
- It facilitated the creation of GUI applications that could remain responsive to user input while performing background tasks.
- Server-side applications could handle multiple requests simultaneously, enhancing throughput and efficiency.
- Exploiting Multi-processor Systems:
- It positioned Java to take advantage of the growing availability of multi-processor systems, enabling true parallel execution of threads on different processors.
- Key Features of the Model:
- Thread Class: The `Thread` class provided the core functionality for creating and managing threads.
- Runnable Interface: The `Runnable` interface allowed for code to be executed as a separate thread without extending the `Thread` class.
- Thread Scheduler: The JVM's thread scheduler managed the allocation of CPU time to threads, ensuring fair and efficient execution.
- Synchronization Mechanisms: Synchronization methods and blocks were introduced to coordinate access to shared resources among multiple threads, preventing data corruption and race conditions.
- Impact and Evolution:
- The Java Threading Model in Java 1.1 represented a significant step forward in enabling concurrent programming within the language.
- It has since evolved with enhancements in subsequent versions, including:
- Improved thread management capabilities
- Advanced synchronization constructs
- Introduction of the `java.util.concurrent` package for more sophisticated concurrency patterns
Although Java 1.1 was a foundation, its threading model laid the groundwork for Java's robust and versatile approach to concurrent programming, which continues to be refined and expanded in modern Java versions.**
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".