elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  duda con wait() y notify()
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: duda con wait() y notify()  (Leído 1,709 veces)
m@o_614


Desconectado Desconectado

Mensajes: 389


Ver Perfil
duda con wait() y notify()
« 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


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Notify Icon
.NET (C#, VB.NET, ASP)
T_O_N_N_Y 2 3,768 Último mensaje 24 Junio 2006, 21:34 pm
por T_O_N_N_Y
[Threads] wait() y notify() « 1 2 »
Java
klaine 13 9,247 Último mensaje 23 Junio 2011, 02:47 am
por Valkyr
wait() en c++ (windows)
Programación C/C++
soyloqbuskas 9 12,343 Último mensaje 22 Diciembre 2011, 12:41 pm
por BlackZeroX
Wait for new IP J.Downloader
Software
Caster 8 3,090 Último mensaje 7 Enero 2012, 19:50 pm
por Caster
notify-send y init.d
GNU/Linux
patilanz 3 2,377 Último mensaje 22 Junio 2015, 14:03 pm
por daryo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines