Ver Mensaje Individual
  #3 (permalink)  
Antiguo 22/11/2015, 12:24
shikamarucb
 
Fecha de Ingreso: febrero-2014
Mensajes: 107
Antigüedad: 10 años, 2 meses
Puntos: 0
Respuesta: Suspender un hilo por algun tiempo

Hola de nuevo, tengo el siguiente codigo que se supone que al presionar el boton de la clase Menu, me suspende el hilo donde se ejecuta el objeto de la clase Graficos, pero se me congela toda la interfaz grafica del Menu tambien; he usado wait y notify pero algo estoy haciendo mal y no se que es

Clase Menu:
Código Java:
Ver original
  1. package invoke;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import javax.swing.SwingUtilities;
  6.  
  7.  
  8. public class Menu extends javax.swing.JFrame {
  9.  
  10.     Graficos obj;
  11.     public Menu() {
  12.         initComponents();
  13.         obj=new Graficos();
  14.         obj.setVisible(true);
  15.     }
  16.  
  17.    
  18.     @SuppressWarnings("unchecked")
  19.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  20.     private void initComponents() {
  21.  
  22.         jButton1 = new javax.swing.JButton();
  23.  
  24.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  25.  
  26.         jButton1.setText("jButton1");
  27.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  28.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  29.                 jButton1ActionPerformed(evt);
  30.             }
  31.         });
  32.  
  33.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  34.         getContentPane().setLayout(layout);
  35.         layout.setHorizontalGroup(
  36.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  37.             .addGroup(layout.createSequentialGroup()
  38.                 .addGap(158, 158, 158)
  39.                 .addComponent(jButton1)
  40.                 .addContainerGap(165, Short.MAX_VALUE))
  41.         );
  42.         layout.setVerticalGroup(
  43.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  44.             .addGroup(layout.createSequentialGroup()
  45.                 .addGap(57, 57, 57)
  46.                 .addComponent(jButton1)
  47.                 .addContainerGap(41, Short.MAX_VALUE))
  48.         );
  49.  
  50.         pack();
  51.     }// </editor-fold>                        
  52.  
  53.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  54.         Runnable actualizar = new Runnable(){
  55.             public void run() {
  56.                 synchronized(obj){
  57.                     try {
  58.                         if(1==obj.pausar(obj)){
  59.                             notify();
  60.                         }
  61.                     } catch (InterruptedException ex) {
  62.                         Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
  63.                     }
  64.                 }
  65.             };
  66.            
  67.         };
  68.         //new Thread(actualizar).start();
  69.         SwingUtilities.invokeLater(actualizar);
  70.     }                                        
  71.  
  72.     /**
  73.      * @param args the command line arguments
  74.      */
  75.     public static void main(String args[]) {
  76.         /* Set the Nimbus look and feel */
  77.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  78.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  79.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  80.          */
  81.         try {
  82.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  83.                 if ("Nimbus".equals(info.getName())) {
  84.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  85.                     break;
  86.                 }
  87.             }
  88.         } catch (ClassNotFoundException ex) {
  89.             java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  90.         } catch (InstantiationException ex) {
  91.             java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  92.         } catch (IllegalAccessException ex) {
  93.             java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  94.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  95.             java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  96.         }
  97.         //</editor-fold>
  98.  
  99.         /* Create and display the form */
  100.         java.awt.EventQueue.invokeLater(new Runnable() {
  101.             public void run() {
  102.                 new Menu().setVisible(true);
  103.             }
  104.         });
  105.     }
  106.  
  107.     // Variables declaration - do not modify                    
  108.     private javax.swing.JButton jButton1;
  109.     // End of variables declaration                  
  110. }

Clase Graficos:
Código Java:
Ver original
  1. package invoke;
  2.  
  3.  
  4. public class Graficos extends javax.swing.JFrame implements Runnable{
  5.  
  6.     public Graficos() {
  7.         initComponents();
  8.     }
  9.  
  10.  
  11.     @SuppressWarnings("unchecked")
  12.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  13.     private void initComponents() {
  14.  
  15.         jButton1 = new javax.swing.JButton();
  16.         jButton2 = new javax.swing.JButton();
  17.         jScrollPane1 = new javax.swing.JScrollPane();
  18.         jTextArea1 = new javax.swing.JTextArea();
  19.  
  20.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  21.  
  22.         jButton1.setText("jButton1");
  23.  
  24.         jButton2.setText("jButton2");
  25.  
  26.         jTextArea1.setColumns(20);
  27.         jTextArea1.setRows(5);
  28.         jScrollPane1.setViewportView(jTextArea1);
  29.  
  30.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  31.         getContentPane().setLayout(layout);
  32.         layout.setHorizontalGroup(
  33.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  34.             .addGroup(layout.createSequentialGroup()
  35.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  36.                     .addGroup(layout.createSequentialGroup()
  37.                         .addGap(102, 102, 102)
  38.                         .addComponent(jButton1)
  39.                         .addGap(63, 63, 63)
  40.                         .addComponent(jButton2))
  41.                     .addGroup(layout.createSequentialGroup()
  42.                         .addGap(114, 114, 114)
  43.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  44.                 .addContainerGap(89, Short.MAX_VALUE))
  45.         );
  46.         layout.setVerticalGroup(
  47.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  48.             .addGroup(layout.createSequentialGroup()
  49.                 .addGap(51, 51, 51)
  50.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  51.                     .addComponent(jButton1)
  52.                     .addComponent(jButton2))
  53.                 .addGap(31, 31, 31)
  54.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  55.                 .addContainerGap(99, Short.MAX_VALUE))
  56.         );
  57.  
  58.         pack();
  59.     }// </editor-fold>                        
  60.  
  61.     /**
  62.      * @param args the command line arguments
  63.      */
  64.    
  65.     public int pausar(Graficos obj) throws InterruptedException{
  66.         wait(10000);
  67.         /*synchronized(obj){
  68.             obj.wait(10000);
  69.         }*/
  70.         return(1);      
  71.     }
  72.    
  73.     public static void main(String args[]) {
  74.         /* Set the Nimbus look and feel */
  75.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  76.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  77.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  78.          */
  79.         try {
  80.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  81.                 if ("Nimbus".equals(info.getName())) {
  82.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  83.                     break;
  84.                 }
  85.             }
  86.         } catch (ClassNotFoundException ex) {
  87.             java.util.logging.Logger.getLogger(Graficos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  88.         } catch (InstantiationException ex) {
  89.             java.util.logging.Logger.getLogger(Graficos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  90.         } catch (IllegalAccessException ex) {
  91.             java.util.logging.Logger.getLogger(Graficos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  92.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  93.             java.util.logging.Logger.getLogger(Graficos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  94.         }
  95.         //</editor-fold>
  96.  
  97.         /* Create and display the form */
  98.         java.awt.EventQueue.invokeLater(new Runnable() {
  99.             public void run() {
  100.                 new Graficos().setVisible(true);
  101.             }
  102.         });
  103.     }
  104.  
  105.     // Variables declaration - do not modify                    
  106.     private javax.swing.JButton jButton1;
  107.     private javax.swing.JButton jButton2;
  108.     private javax.swing.JScrollPane jScrollPane1;
  109.     private javax.swing.JTextArea jTextArea1;
  110.     // End of variables declaration                  
  111.  
  112.     @Override
  113.     public void run() {
  114.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  115.     }
  116. }

Gracias por su atencion.