Foro de elhacker.net

Programación => Java => Mensaje iniciado por: m@o_614 en 12 Septiembre 2015, 01:13 am



Título: duda con wait() y notify()
Publicado por: m@o_614 en 12 Septiembre 2015, 01:13 am
Saludos

Estoy haciendo el siguiente código en el que tengo dos hilos, cada uno de los cuales
va a imprimir un contador en un área de texto. En el código tengo un botón, el cual
se va a bloquear(deja de imprimir) si hago clic en el botón y se desbloquea una vez que haga clic otra
vez en el botón. Para esto traté de usar un wait() y un notify, pero creo que no los
estoy usando correctamente porque la primera vez que oprimo el botón si se bloquea
pero a la segunda no se desbloquea

Código
  1. import java.awt.*;
  2. import java.util.Random;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. class Hilo extends Thread{
  7. private int cuenta = 0;
  8. private long pausa;
  9. private boolean puedeImprimir = true;
  10. private JTextArea areaTexto;
  11.  
  12. Hilo(long milisegundos,JTextArea cuadroTexto){
  13. pausa = milisegundos;
  14.    areaTexto = cuadroTexto;
  15. }
  16.  
  17. public void ocurrioBotonazo(){
  18.    if(puedeImprimir)
  19.       this.puedeImprimir = false;
  20.    else
  21.    {
  22.     this.puedeImprimir = true;
  23.     reanudar(this);
  24.    }
  25. }
  26.  
  27.  
  28. public synchronized void reanudar(Hilo hilo)
  29. {
  30. if(hilo.puedeImprimir)
  31.   hilo.notify();
  32. }
  33.  
  34. public void run()
  35. {
  36. while(this.puedeImprimir)
  37. {
  38. try
  39. {
  40. this.imprimirContador();
  41. Thread.sleep(pausa);
  42. this.cuenta++;
  43. }
  44. {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. public void imprimirContador(){
  51. String tiempo;
  52.    tiempo = Integer.toString(cuenta);
  53.    areaTexto.setText(tiempo);
  54. }
  55. }
  56.  
  57. class Interfaz extends JFrame implements ActionListener{
  58. private JTextArea areaTexto,areaTexto2;
  59. private JButton boton;
  60. private Hilo hilo,hiloEvento;
  61.  
  62. Interfaz()
  63. {
  64. areaTexto = new JTextArea(10,7);
  65. areaTexto2 = new JTextArea(10,7);
  66. hilo = new Hilo(2000,areaTexto);
  67. hiloEvento = new Hilo(1000,areaTexto2);
  68. boton = new JButton("Pausar/Reanudar");
  69. this.getContentPane().add(boton,BorderLayout.SOUTH);
  70. this.getContentPane().add(areaTexto,BorderLayout.WEST);
  71. this.getContentPane().add(areaTexto2,BorderLayout.EAST);
  72.  
  73. hilo.start();
  74.        hiloEvento.start();
  75.  
  76. boton.addActionListener(this);
  77. }
  78.  
  79. public void actionPerformed(ActionEvent event)
  80. {
  81.     hiloEvento.ocurrioBotonazo();
  82. }
  83. }
  84.  
  85.  
  86. public class MensajesHilos {
  87.  
  88. public static void main(String[] args){
  89. Interfaz i = new Interfaz();
  90. i.setTitle("Hilos de Control");
  91. i.setBounds(200, 200, 300, 240);
  92. i.setVisible(true);
  93. }
  94. }
  95.  


gracias de antemano