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
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
private int cuenta = 0;
private long pausa;
private boolean puedeImprimir = true;
Hilo
(long milisegundos,
JTextArea cuadroTexto
){ pausa = milisegundos;
areaTexto = cuadroTexto;
}
public void ocurrioBotonazo(){
if(puedeImprimir)
this.puedeImprimir = false;
else
{
this.puedeImprimir = true;
reanudar(this);
}
}
public synchronized void reanudar(Hilo hilo)
{
if(hilo.puedeImprimir)
hilo.notify();
}
public void run()
{
while(this.puedeImprimir)
{
try
{
this.imprimirContador();
this.cuenta++;
}
{
e.printStackTrace();
}
}
}
public void imprimirContador(){
areaTexto.setText(tiempo);
}
}
private Hilo hilo,hiloEvento;
Interfaz()
{
hilo = new Hilo(2000,areaTexto);
hiloEvento = new Hilo(1000,areaTexto2);
boton
= new JButton("Pausar/Reanudar");
hilo.start();
hiloEvento.start();
boton.addActionListener(this);
}
{
hiloEvento.ocurrioBotonazo();
}
}
public class MensajesHilos {
public static void main
(String[] args
){ Interfaz i = new Interfaz();
i.setTitle("Hilos de Control");
i.setBounds(200, 200, 300, 240);
i.setVisible(true);
}
}
gracias de antemano