Mostrando entradas con la etiqueta java. Mostrar todas las entradas
Mostrando entradas con la etiqueta java. Mostrar todas las entradas

viernes, 30 de abril de 2010

Java Readers and Writer. Mutual exclusion.

Think about this situation, you have a buffer and into the bufer has one data. We have several elements in this situation: One Writer, that provide tha data into the buffer, and many Readers, thtat need the data into the buffer; and one restriction, only one persona can read or write into the buffer at the same time.

In order to resolve this problem we can use a Monitor. A monitor is a Class that provide a mutual exclusion in this methods. To write this in Java, we can introduce the keyword synchronized in the method. For ejample, in Buffer.java:




We can see complete execution over Main class



Output:

run:
Reader 0 is waiting to get data (total buffer=0)
Reader 9 is waiting to get data (total buffer=0)
Reader 8 is waiting to get data (total buffer=0)
Reader 7 is waiting to get data (total buffer=0)
Reader 3 is waiting to get data (total buffer=0)
Reader 6 is waiting to get data (total buffer=0)
Reader 2 is waiting to get data (total buffer=0)
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 0 unlock
Reader 0 has read a data (buff=0)
........reader 9 unlock
Reader 9 is waiting to get data (total buffer=0)
Reader 4 is waiting to get data (total buffer=0)
Reader 5 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 8 unlock
Reader 8 has read a data (buff=0)
........reader 7 unlock
Reader 7 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 3 unlock
Reader 3 has read a data (buff=0)
........reader 6 unlock
Reader 6 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 2 unlock
Reader 2 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
Reader 28 is waiting to get data (total buffer=0)
Reader 29 is waiting to get data (total buffer=0)
Reader 20 is waiting to get data (total buffer=0)
Reader 11 is waiting to get data (total buffer=0)
Reader 13 is waiting to get data (total buffer=0)
Reader 16 is waiting to get data (total buffer=0)
Reader 27 is waiting to get data (total buffer=0)
Reader 10 is waiting to get data (total buffer=0)
Reader 18 is waiting to get data (total buffer=0)
Reader 24 is waiting to get data (total buffer=0)
Reader 23 is waiting to get data (total buffer=0)
Reader 17 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 9 unlock
Reader 9 has read a data (buff=0)
........reader 4 unlock
Reader 4 is waiting to get data (total buffer=0)
Reader 21 is waiting to get data (total buffer=0)
Reader 22 is waiting to get data (total buffer=0)
Reader 19 is waiting to get data (total buffer=0)
Reader 12 is waiting to get data (total buffer=0)
Reader 25 is waiting to get data (total buffer=0)
Reader 26 is waiting to get data (total buffer=0)
Reader 14 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 5 unlock
Reader 5 has read a data (buff=0)
........reader 7 unlock
Reader 7 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 6 unlock
Reader 6 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
Reader 15 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 28 unlock
Reader 28 has read a data (buff=0)
........reader 29 unlock
Reader 29 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 20 unlock
Reader 20 has read a data (buff=0)
........reader 11 unlock
Reader 11 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 13 unlock
Reader 13 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 27 unlock
Reader 27 has read a data (buff=0)
........reader 10 unlock
Reader 10 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 18 unlock
Reader 18 has read a data (buff=0)
........reader 24 unlock
Reader 24 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 23 unlock
Reader 23 has read a data (buff=0)
........reader 17 unlock
Reader 17 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 4 unlock
Reader 4 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 22 unlock
Reader 22 has read a data (buff=0)
........reader 19 unlock
Reader 19 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 12 unlock
Reader 12 has read a data (buff=0)
........reader 25 unlock
Reader 25 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 26 unlock
Reader 26 has read a data (buff=0)
........reader 14 unlock
Reader 14 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 7 unlock
Reader 7 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 15 unlock
Reader 15 has read a data (buff=0)
........reader 29 unlock
Reader 29 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 11 unlock
Reader 11 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 10 unlock
Reader 10 has read a data (buff=0)
........reader 24 unlock
Reader 24 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 17 unlock
Reader 17 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 19 unlock
Reader 19 has read a data (buff=0)
........reader 25 unlock
Reader 25 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 14 unlock
Reader 14 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 29 unlock
Reader 29 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 24 unlock
Reader 24 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 25 unlock
Reader 25 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 16 unlock
Reader 16 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 1 unlock
Reader 1 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 21 unlock
Reader 21 has read a data (buff=0)
The writer is writing data (buffer:1)
The writer is writing data (buffer:2)
The writer is writing data (buffer:3)
The writer is writing data (buffer:4)
The writer is waiting to write data. The buffer is full

viernes, 10 de abril de 2009

Java Random Double in Range Generation

Estos días he estado pensando sobre como generar números aleatorios en Java dentro de un rango, pues bien, aquí esta mi solución y su análisis:



Así, analizando los valores límite, observamos:

max(0.0)+min(1-(0.0)) => min
max(0.5)+min(1-(0.5)) => (max+min)/2
max(1.0)+min(1-(1.0)) => max

De una manera gráfica, observamos como los valores están siempre encerrados dentro de los límites:


Un análisis matemático de ajuste.

Aquí un histograma con 100000 valores generados aleatoriamente, se puede observar como sigue una distribución uniforme.

Con un rango en la escala negativa.

viernes, 28 de diciembre de 2007

Java sincronizacion concurrente

El problema


En un reino medieval conviven 5 caballeros que deben compartir bajo exclusión mutua un único salón para festejar sus victorias en sus guerras. Para evitar solapar las fiestas de unos caballeros con las de otros el rey ha impuesto la siguiente política:

Cuando un caballero llega a su castillo tras una guerra desea festejarlo. Cada caballero dispone de un vasallo al que avisa para que consiga el salón y lo prepare a su gusto. Después, el caballero debe esperar a que su vasallo haya conseguido el salón y lo haya terminado de preparar, momento en el cual puede comenzar su fiesta. Entonces, el vasallo debe esperar a que el caballero le avise de que ha terminado la fiesta para proceder a limpiar el salón, dejando posteriormente el salón libre.
Output de la solucion :


Caballero 0 guerreando
Caballero 3 guerreando
Caballero 6 guerreando
Caballero 2 guerreando
Caballero 5 guerreando
Caballero 8 guerreando
Caballero 1 guerreando
Vasayo 2 esperando a guerrero
Caballero 4 guerreando
Vasayo 0 esperando a guerrero
Caballero 7 guerreando
Vasayo 3 esperando a guerrero
Vasayo 7 esperando a guerrero
Vasayo 8 esperando a guerrero
Caballero 9 guerreando
Vasayo 4 esperando a guerrero
Vasayo 1 esperando a guerrero
Vasayo 9 esperando a guerrero
Vasayo 6 esperando a guerrero
Vasayo 5 esperando a guerrero
Caballero 2 llamando a vasayo
Vasayo 2 preparando sala
Caballero 2 festejando
Caballero 4 llamando a vasayo
Vasayo 4 esperando para adquirir la sala (esperando 0)
Vasayo 2 esperando terminacion de la fiesta para limpiar
Vasayo 4 preparando sala
Vasayo 2 termino de limpiar la sala
Caballero 0 llamando a vasayo
Vasayo 0 esperando para adquirir la sala (esperando 0)
Caballero 4 festejando
Caballero 5 llamando a vasayo
Vasayo 5 esperando para adquirir la sala (esperando 1)
Vasayo 4 esperando terminacion de la fiesta para limpiar
Vasayo 4 termino de limpiar la sala
Vasayo 0 preparando sala
Caballero 0 festejando
Vasayo 0 esperando terminacion de la fiesta para limpiar
Vasayo 0 termino de limpiar la sala
Vasayo 5 preparando sala
Caballero 5 festejando
Vasayo 5 esperando terminacion de la fiesta para limpiar
Vasayo 5 termino de limpiar la sala
Caballero 8 llamando a vasayo
Vasayo 8 preparando sala
Caballero 8 festejando
Vasayo 8 esperando terminacion de la fiesta para limpiar
Vasayo 8 termino de limpiar la sala
Caballero 7 llamando a vasayo
Vasayo 7 preparando sala
Caballero 7 festejando
Vasayo 7 esperando terminacion de la fiesta para limpiar
Vasayo 7 termino de limpiar la sala
Caballero 6 llamando a vasayo
Vasayo 6 preparando sala
Caballero 6 festejando
Vasayo 6 esperando terminacion de la fiesta para limpiar
Vasayo 6 termino de limpiar la sala
Caballero 3 llamando a vasayo
Vasayo 3 preparando sala
Caballero 9 llamando a vasayo
Vasayo 9 esperando para adquirir la sala (esperando 0)
Vasayo 9 preparando sala
Caballero 3 festejando
Caballero 9 festejando
Vasayo 9 esperando terminacion de la fiesta para limpiar
Vasayo 9 termino de limpiar la sala
Vasayo 3 esperando terminacion de la fiesta para limpiar
Vasayo 3 termino de limpiar la sala
Caballero 1 llamando a vasayo
Vasayo 1 preparando sala
Caballero 1 festejando
Vasayo 1 esperando terminacion de la fiesta para limpiar
Vasayo 1 termino de limpiar la sala