JAVA les threads

Fermé
layeures Messages postés 286 Date d'inscription mercredi 13 juin 2007 Statut Membre Dernière intervention 22 juin 2018 - 17 sept. 2010 à 11:16
Bonjour,
j ai essayer de faire un programme avec un thread mes il y a un bug
le probleme est que je n'arrive pas a arreter le thread et de l'excuter une deuxieme fois je croie que le probleme vien de la.
aidez moi si vous plait.

voici le code qui je croie pose probleme

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frameun.java
 *
 * Created on 16 sept. 2010, 12:38:19
 */

package pack;

import java.io.*;
import java.util.*;
import java.lang.*;
/**
 *
 * @author sofiene
 */
public class frameun extends javax.swing.JFrame {
    Thread etatth = Thread.currentThread();
    String dd = etatth.getName();
    PseudoPing a1 = new PseudoPing();
    /** Creates new form frameun */
    public frameun() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        ipun = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        etat1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        thread = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Reseaux Ping");

        jLabel1.setBackground(new java.awt.Color(204, 255, 102));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("adresse ip n°1");
        jLabel1.setAlignmentX(0.5F);

        etat1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etat1.setText("etat du serveur");

        jButton1.setText("pinger serveur");
        jButton1.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("arret du ping");
        jButton2.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        thread.setText("etat thread");

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("nouveau");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(etat1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(ipun, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))))
                .addGap(125, 125, 125)
                .addComponent(thread, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(ipun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(etat1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(thread))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
if (a1.isAlive() == false){
    PseudoPing.a = 0;
//PseudoPing a2 = new PseudoPing();
  try {
    a1.start();
    thread.setText("Ping executer");
 //   thread.setText("Ping Executer");
} catch (Exception e){
thread.setText("erreur d'excecution");
}
}
else if (a1.isAlive() == true){
       PseudoPing.a = 1;
    try {
     //PseudoPing a3 = new PseudoPing();
  
          a1.join();
          thread.setText("");
      } catch (InterruptedException e) {}
    thread.setText("probleme 1");
}
        // TODO add your handling code here:
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       PseudoPing.a = 1;
       //PseudoPing a4 = new PseudoPing();
       try {
           a1.sleep(1000);
           a1.interrupt();
         a1.join();
         thread.setText("arret du thread");
      } catch (InterruptedException e) {}
        
        frameun.etat1.setText("etat du serveur terminer");
        // TODO add your handling code here:
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws Exception {
                new frameun().setVisible(true);
           // PseudoPing aa1 = new PseudoPing();

    
    
    }

    // Variables declaration - do not modify                     
    public static javax.swing.JLabel etat1;
    public static javax.swing.JTextField ipun;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private static javax.swing.JLabel thread;
    // End of variables declaration                   

}




A voir également: