oval
Centre International de Mathİmatiques Pures et Appliquİes
International Center for Pure and Applied Mathematics
A CIMPA-UNSA-INRIA-UNESCO-VIETNAM SCHOOL ON
Objects and Network
Threads Exercises
Emmanuel Chailloux
Emmanuel.Chailloux@lip6.fr
http://www-spi.lip6.fr/~emmanuel
Shared memory
All process interact on the same memory :
figure=shared.eps,height=0.4
Ş implicit communication and explicit synchronization!!!
Relations between Threads
- Unrelated Threads
- Related but Unsynchronized Threads
- Mutually Exclusive Threads
- mutual exclusion over a block of statements
- mutual exclusion over a method
- Communicating Mutually-Exclusive Threads
Threads Exercises
- Game of Life : case 1,2 and 3
- Producer-Consumer : case 3 and case 4
- World of robots : case 3 or case 4
Exercises : Threads
A simulation for cellulars automaton
The following source program is a text version for the Game of
Life (a simulation for cellulars automats).
GameOfLife.java
To follow the different tasks :
- To extend the inheritence tree to build an applet from this
application. You don't have to modify the original source. You can
display the old and the new generations.
- To write an HTML file which starts two applets of ``Game of
Life''.
To test it
with appletviewer and a browser.
- To modify the code to run four (independent) threads on the same
world (you slice the world in four parts). Each thread can display
its cells with a different color.
- To modify this program with overlap between threads. You need to
synchronize threads.
Some Sources
A Producer-Consumer
It's a classical example.
- To create basic classes to represent Product and a
Shop
(represented
by an Array) where Product instances are stored.
- To create a Producer class
which can build Product instances and store them
inside a Shop instance.
- To create a Consumer class which can take Product
instances from a Shop instance.
- What are the different problems of synchronisation?
- To integrate a solution and to run your program with a
Producer
instance and three Consumer instances, synchronized to a Shop
instance.
Some Sources
A World of robots
In a special world, represented by a plane, somes robots move.
The world contains cells where robots can stay or move. There are
different kinds of robots. Each robot will be represented by a
thread.
- To define a World abstract class.
- To define a concrete sub-class for a plane world.
- To define an abstract class for the robots.
- To define a concrete class for an ``hazardeous''robot. It'll decide
by random what will its next position.
- To integrate these different classes to build a text
application, where
robot movements will be displayed.
- To build an applet to have a graphical display. A robot
will be represented by a colored rectangle.
If you have time, to add others kinds of robots :
- a stayed robot (without movement)
- a right robot, which turns right when it can't move.
- a friendly robot, which follows the closest robot.
(in this case it needs to known positions of others robots).
- To try also a world which communicates at boards.
This document was translated from LATEX by HEVEA.