Tip:
Highlight text to annotate it
X
CRISTIAN >> Hi, I'm Christian. Welcome to the laboratory number eleven of the module
"Development of Computer System Functions."
In this lab you will know Java multithreaded architectures and you will learn one of several
mechanisms of synchronization between threads. In particular, the control of critical section.
Open Eclipse. In this lab, you need to create
three classes. As it is usual, in the first step you have to create a new project. To
do this, choose Fichero -> Nuevo -> Proyecto Java. In the wizard that it will be appeared
put "Lab11" in the "Nombre de proyecto" textbox and click on Finalizar.
Now create the first class into the project,
that is called contador. Display the project by double clicking on its name and click the
right mouse button on the src icon. Select Nueva -> Clase. In the wizard that it will
be appeared, put "contador" in the "Nombre" textbox and click on Finalizar.
Within the class, create an attribute called
contador of int primitive type and initialise to 1.
You also need to create the getter and setter
for the class. To do this, please use the following automation tool that provides Eclipse:
place the cursor within the code of the class, click the right mouse button and choose "Código
fuente" -> "Generar métodos de obtención y establecimiento" . Check the box where it
is contador text and click on Aceptar. You can watch how the signatures of both methods
are created automatically, with its source code.
In the second step you have to create a HiloContador
class, which it is a class that works with Contador class by using threads: click the
right mouse button on the src icon. Select Nueva -> Clase. In the wizard that it will
be appeared, put "HiloContador" in the "Nombre:", textbox, replace java.lang.Object for java.lang.Thread
in the "Superclase" textbox and click on Finalizar.
Within the class, you have to create an attribute called contador that belongs to the Contador
class, created in the previous step.
You have to create the constructor of the HiloContador class. To do this, use another
automation tool that provides Eclipse: place the cursor within class code, click on the
right mouse button and choose "Código fuente" -> "Generar constructor utilizando campos".
Select Thread(String) in the "Seleccione el constructor super a invocar" textbox and click on "Aceptar". You can see how the
signature is created automatically within their source code.
Then, you have to create the run() method
which is the main method of a thread. Write its signature, namely:
And inside the method, type the following
code:
With this code every iteration of the loop will increase the contador value by one unit
and prints the results on screen. The iteration is performed ten times and includes a waiting
time for what the thread running between the getter and setter shows error that can occur
with the execution of multiple threads concurrently.
Third and finally, create a TestHiloContador class, which it will be the one that runs
class code of the HiloContador simultaneously by multiple threads: click the right mouse
button on the src icon. Select Nueva -> Clase. In the wizard that it will be appeared, put
"TestHiloContador" in the "Nombre:" textbox and click on Finalizar, but not before check
the line public static void main (String args []).
Declare a cont object of Contador type and
reserve its corresponding memory space. That is:
Declare two objects: hc1 and hc2, of HiloContador type and reserve its corresponding memory
space. Call its constructor with Strings "HiloUno" and "HiloDos" respectively, next to the cont
object. That is:
Call start() method of the two newly created objects, that is, hc1 and hc2.
Now you can run the program.
It is possible the results will not be what it's expected. This is because the implementation
of the two threads is not controlled in its critical section. For a detailed explanation
of this problem, check out the theoretical part of this unit.
To resolve this problem, we must prevent access
to the execution of the critical section by more than one thread at the same time. This
is achieved by using the synchronized method (variable) that sorrounds the critical section
code. Run the code
and notice that with this change is working properly.
And so ended the laboratory number 11. Thank
you very much for your attention and I wait for you in the lab number 12.