Descargar la presentación
La descarga está en progreso. Por favor, espere
Publicada porAlfredo Cedillo Modificado hace 9 años
1
Capítulo 5 - b: Hilos
2
4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Ejemplo de hilos: un applet Un applet es un hilo que se ejecuta dentro de un navegador web Los navegadores web invocan los siguientes métodos para la administración de los applets: start( ) – inicia la actividad del applet, lo crea si no existe, lo reinicia si estaba suspendido por ejemplo si el applet vuelve al área de visualización de la página web stop( ) – suspende la ejecución del applet. Por ejemplo si el applet sale del área de visualización de la página web destroy( ) – elimina al applet No se deben confundir estos métodos con los métodos de los hilos.
3
4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Ejemplo de hilos: un applet A continuación se presenta un applet que muestra la hora y la fecha en un navegador web. import java.applet.*; import java.awt.*; public class AppletReloj extends Applet implements Runnable{ public void run( ){ while( true ){ try{ HiloReloj.sleep(1000); } catch( InterruptedException e){} repaint( ); } public void start( ){ if( HiloReloj == null ){ HiloReloj = new Thread(this); HiloReloj.start( ); } else{ HiloReloj.resume( ); }
4
4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Ejemplo de hilos: un applet public void stop( ){ if( HiloReloj != null ){ HiloReloj.suspend( ); } public void destroy( ){ if( HiloReloj != null ){ HiloReloj.stop( ); HiloReloj = null; } public void paint(Graphics g){ g.drawString(new java.util.Date( ).toString( ),10,30); } private Thread HiloReloj; }
5
4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Productor – consumidor con buzón ilimitado El buzón con capacidad ilimitada implementado en el capítulo 4 utilizando vectores, se muestra a continuación import java.util.*; public class ColaMensajes{ cola = new Vector( ); } public void enviar( Object mensaje ){ cola.addElement( mensaje ); } public Object recibir( ){ Object mensaje; if( cola.isEmpty( ) == true ) return null; else{ mensaje = cola.firstElement( ); cola.removeElementAt( 0 ); return mensaje; } private Vector cola; }
6
4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Productor – consumidor con buzón ilimitado class Productor extends Thread{ public Productor( ColaMensajes c ){ buzon = c ; } public void run( ){ while( true ){ int dormirtiempo = ( int )Servidor.DORMIR*Math.random( )); System.out.println( “El Productor duerme por "+dormirtiempo+" segundos"); try{ Productor.sleep(dormirtiempo); } catch( InterruptedException e ){}; mensaje = new Date( ); buzon.enviar( mensaje ); } Private ColaMensajes buzon; }
7
4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Productor – consumidor buzón ilimitado class Consumidor extends Thread{ public Consumidor( ColaMensajes c ){ buzon = c; } public void run( ){ while( true ){ int dormirtiempo = (int)Servidor.DORMIR*Math.random( )); System.out.println("El consumidor duerme por "+dormirtiempo+" segundos"); try{ Consumidor.sleep(dormirtiempo*1000); } catch(InterruptedException e ){ }; mensaje = (Date)buzon.recibir( ); if( mensaje != null )System.out.println("El consumidor retiro "+mensaje); } private ColaMensajes buzon; }
8
4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Productor – consumidor con buzón ilimitado public class Servidor{ public Servidor( ){ ColaMensajes buzon = new ColaMensajes( ); Productor HiloProductor = new Productor( buzon ); Consumidor HiloConsumidor = new Consumidor( buzon ); HiloProductor.start( ); HiloConsumidor.start( ); } public static void main( String args[ ]){ Servidor ejecuta = new Servidor( ); } public static final int DORMIR = 5; }
9
Final del Capítulo 5 - b
Presentaciones similares
© 2024 SlidePlayer.es Inc.
All rights reserved.