
|
|
Magercises: Threads
Introduction
Welcome to the Thread Magercises. The initial Magercises cover
thread creation and basic operations, synchronization and communcation between threads,
and how to avoid race conditions and deadlocks.
So
that you may concentrate on the topic of threads, we provide a code framework
rather than asking you to work from scratch.
Two basic approaches to thread use are explored: (i) a single thread
manages all objects and (ii) a thread is used to manage every object,
resulting in so-called actors. The first two Magercises use the former
technique and the second two use the latter technique.
Overall, there are seven thread Magercises to complete:
- Using the Runnable Interface
This Magercise shows how to introduce concurrency into an object using
the Runnable interface.
Educational goal(s):
- Use the Runnable interface.
- Create a run method for a class.
- Using Synchronized Methods
This Magercise shows how to protect objects in a concurrent
context using synchronized methods. This Magercise also shows
how to join threads together.
Educational goal(s):
- Create and use synchronized methods
- Create a new subclass of the Thread class.
- Use the join method.
- Synchronized Statements
In this Magercise, a synchronized statement is used to coordinate
access to an object.
Educational goal(s):
- Demonstrate contention for a resource by multiple threads.
- Use synchronized statements.
- Using Wait and Notify in a Producer/Consumer Scenario
This Magercise shows how to synchronize
communication between threads.
Educational goal(s):
- Use the methods wait and notifyAll to synchronize
the passing of data between threads.
- Using Wait and Notify in a Barrier Scenario
Here the wait and notify methods are used to
coordinate the actions of
a group of threads.
Educational goal(s):
- Use the methods wait and notifyAll to
create a thread barrier.
- Thread Priority and Scheduling
This Magercise explores the nature of thread scheduling using the
sleep and yield methods, as well as the
effects of thread priority.
Educational goal(s):
- Demonstrate aspects of thread scheduling.
- Use the yield method to improve application liveness.
- Demonstrate the effects of thread priority.
- Using a Facade to avoid Deadlocks
Here a facade object is used to manage changes to a group of objects.
This Magercise shows how such a structure can be used to avoid
potential deadlocks.
Educational goal(s):
- Demonstrate scenarios that can result in deadlocks.
- Organize sychronized object access through a facade object.
- Using Threads to Maintain Application Liveness
This Magercise shows how threads can be used to improve the reponsiveness
of a user interface.
Educational goal(s):
- Spawn new threads from GUI actions.
- Coordinate a thread's action with the user interface.
- Using the Threads in Applets
This Magercise shows how to manage a thread's lifecycle in the context
of an Applet.
Educational goal(s):
- Use the suspend and resume methods.
- Stop a thread when an applet is destroyed.
- Using Threads as Actors
In this Magercise, a group of active objects are linked together
to cooperate on a computation.
Educational goal(s):
- Create an actor class.
- Link actor objects together to achieve a computation.
|