Bonjour, dans code, je souhaiterai arrêter un thread puis le redémarrer ensuite lorsque qu'il se passe quelque chose mais je ne sais comment faire.

Voilà, ce que j'ai fait:
la classe ThreadLecteur qui gère le thread :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
public class ThreadLecteur extends Thread {
 
    public void run() {
 
        while (true) {
 
            Interface_Gardien.jTextFieldNumCodeBarre.setText(Interface_Gardien.CB);
            if (!Interface_Gardien.jTextFieldNumCodeBarre.getText().equals("")) {
                Interface_Gardien.thread.stop(); 
            }
        }
    }
}

classe Interface_Gardien (ici on s'intéresse qu'au thread qui est dans la partie de l'initialisation de la fenetre:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Interface_Gardien.java
 *
 * Created on 29 mai 2008, 10:34
 */
 
 
 
 
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
 
/**
 *
 * @author  adminlocal
 */
public class Interface_Gardien extends javax.swing.JFrame {
 
        GestionSystemeGardien ent = new GestionSystemeGardien();
        static ThreadLecteur thread = new ThreadLecteur();
        static String CB;
 
 
    /** Creates new form Interface_Gardien */
    public Interface_Gardien() {
        initComponents();
 
        setTitle("Interface du Gardien");     
 
        //centrage fenêtre
        ent.centrageFenetre(this);
 
        //Affichage caméra
        ent.affichageCamera(jPanelCam);
 
         //Connexion au lecteur code barre
        GestionLecteur l = new GestionLecteur(ent.m_Port_LCB_chaine);
        thread.start();
    }
 
 
 
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jPanelCam = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanelPhoto = new javax.swing.JPanel();
        jPanelAlarme = new javax.swing.JPanel();
        jToggleButtonArretAlarme = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelPorte = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jToggleButtonPorte = new javax.swing.JToggleButton();
        jButtonPassageBadge = new javax.swing.JButton();
        jTextFieldNumCodeBarre = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButtonTest = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItemConfiguration = new javax.swing.JMenuItem();
        jMenuItemMDP = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItemManuel = new javax.swing.JMenuItem();
        jMenuItemApropos = new javax.swing.JMenuItem();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jPanelCam.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
 
        org.jdesktop.layout.GroupLayout jPanelCamLayout = new org.jdesktop.layout.GroupLayout(jPanelCam);
        jPanelCam.setLayout(jPanelCamLayout);
        jPanelCamLayout.setHorizontalGroup(
            jPanelCamLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 454, Short.MAX_VALUE)
        );
        jPanelCamLayout.setVerticalGroup(
            jPanelCamLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 383, Short.MAX_VALUE)
        );
 
        jLabel1.setText("Caméra extérieure:");
 
        jLabel2.setText("Photo de l'employé:");
 
        jPanelPhoto.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
 
        org.jdesktop.layout.GroupLayout jPanelPhotoLayout = new org.jdesktop.layout.GroupLayout(jPanelPhoto);
        jPanelPhoto.setLayout(jPanelPhotoLayout);
        jPanelPhotoLayout.setHorizontalGroup(
            jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 454, Short.MAX_VALUE)
        );
        jPanelPhotoLayout.setVerticalGroup(
            jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 390, Short.MAX_VALUE)
        );
 
        jPanelAlarme.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
 
        org.jdesktop.layout.GroupLayout jPanelAlarmeLayout = new org.jdesktop.layout.GroupLayout(jPanelAlarme);
        jPanelAlarme.setLayout(jPanelAlarmeLayout);
        jPanelAlarmeLayout.setHorizontalGroup(
            jPanelAlarmeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanelAlarmeLayout.setVerticalGroup(
            jPanelAlarmeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
 
        jToggleButtonArretAlarme.setText("Marche/Arrêt Alarme");
        jToggleButtonArretAlarme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonArretAlarmeActionPerformed(evt);
            }
        });
 
        jLabel3.setText("Déclenchement alarme:");
 
        jLabelPorte.setText("La porte est fermée");
 
        jLabel4.setText("Simulation de la porte:");
 
        jToggleButtonPorte.setText("Ouverture Porte");
        jToggleButtonPorte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonPorteActionPerformed(evt);
            }
        });
 
        jButtonPassageBadge.setText("Passage du badge");
        jButtonPassageBadge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPassageBadgeActionPerformed(evt);
            }
        });
 
        jTextFieldNumCodeBarre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNumCodeBarreActionPerformed(evt);
            }
        });
 
        jLabel5.setText("Numéro du code barre:");
 
        jButtonTest.setText("jButton1");
        jButtonTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTestActionPerformed(evt);
            }
        });
 
        jMenu1.setText("Fichier");
 
        jMenuItem1.setText("Quitter");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
 
        jMenuBar1.add(jMenu1);
 
        jMenu2.setText("Configuration");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });
 
        jMenuItemConfiguration.setText("Paramétrage du matériel");
        jMenuItemConfiguration.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemConfigurationActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemConfiguration);
 
        jMenuItemMDP.setText("Modification du mot de passe");
        jMenuItemMDP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemMDPActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemMDP);
 
        jMenuBar1.add(jMenu2);
 
        jMenu3.setText("?");
 
        jMenuItemManuel.setText("Manuel d'utilisation");
        jMenuItemManuel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemManuelActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItemManuel);
 
        jMenuItemApropos.setText("A propos...");
        jMenuItemApropos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAproposActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItemApropos);
 
        jMenuBar1.add(jMenu3);
 
        setJMenuBar(jMenuBar1);
 
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(83, 83, 83)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanelPhoto, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanelCam, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jToggleButtonArretAlarme)
                                    .add(layout.createSequentialGroup()
                                        .add(14, 14, 14)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(jLabel3)
                                            .add(jPanelAlarme, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                                .add(160, 160, 160))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 433, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(34, 34, 34))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 191, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextFieldNumCodeBarre)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel5)
                                    .add(jButtonPassageBadge))
                                .add(18, 18, 18)
                                .add(jButtonTest))
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jLabelPorte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jToggleButtonPorte)
                                    .add(jLabel4))))
                        .add(94, 94, 94)))
                .add(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(45, 45, 45)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelCam, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(164, 164, 164)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelAlarme, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jToggleButtonArretAlarme)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelPhoto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(57, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(57, 57, 57)
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabelPorte)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jToggleButtonPorte)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 195, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonPassageBadge)
                            .add(jButtonTest))
                        .add(18, 18, 18)
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldNumCodeBarre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(61, 61, 61))))
        );
 
        pack();
    }// </editor-fold>                        
 
    private void jToggleButtonPorteActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
 
        //Ouverture/Fermeture de la porte
        GestionGache porte = new GestionGache();
        AbstractButton bouton = (AbstractButton) evt.getSource();
        boolean selected = bouton.getModel().isSelected();
        if (selected) {
            porte.PorteOuverte(jLabelPorte);
        } else {
            porte.PorteFermee(jLabelPorte);
        }
    }                                                  
 
    private void jTextFieldNumCodeBarreActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        // TODO add your handling code here:
}                                                      
 
    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
 
    }                                      
 
    private void jButtonPassageBadgeActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            String NumCodeBarre = "9204521220089";
 
 
 
            //Téléchargement de la photo
            ent.downloadFichier(NumCodeBarre, "photo_employe.jpg", ".jpg");
 
            //Affichage photo
            ent.affichagePhoto(jPanelPhoto);
 
            //Téléchargement du son
            ent.downloadFichier(NumCodeBarre, "son.wav", ".wav");
 
            //Lecture du son
            ent.lectureSon();
 
            //Requete
            ent.requete(NumCodeBarre);
 
 
            ent.majHeureEntree("9204521220089");
 
            //Connexion à la BdD
            /*GestionSystemGardien app = new GestionSystemGardien();
            app.ConnexionBdD("root", "root", "jdbc:mysql://10.104.100.57:3306/", "org.gjt.mm.mysql.Driver");*/
 
 
            /*GestionAutorisation a = new GestionAutorisation();
            a.HeureEntree();*/
 
        } catch (Exception ex) {
            Logger.getLogger(Interface_Gardien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                                   
 
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        System.exit(0);
    }                                          
 
    private void jToggleButtonArretAlarmeActionPerformed(java.awt.event.ActionEvent evt) {                                                         
        // TODO add your handling code here:
          //Marche/Arrêt de l'alarme
        GestionAlarme alarme = new GestionAlarme(Color.WHITE, Color.RED, 1000,
                1000, jPanelAlarme);
        AbstractButton bouton = (AbstractButton) evt.getSource();
        boolean selected = bouton.getModel().isSelected();
        if (selected) {
            alarme.demarrer();
        } else {
            alarme.arreter(); 
        }
    }                                                        
 
    private void jMenuItemConfigurationActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        // TODO add your handling code here:
        new Parametrage().setVisible(true);
}                                                      
 
    private void jButtonTestActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        jTextFieldNumCodeBarre.setText("");
        thread.start();      
    }                                           
 
    private void jMenuItemMDPActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        new ChangerMP().setVisible(true);
}                                            
 
    private void jMenuItemAproposActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
}                                                
 
    private void jMenuItemManuelActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Interface_Gardien().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonPassageBadge;
    private javax.swing.JButton jButtonTest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelPorte;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItemApropos;
    private javax.swing.JMenuItem jMenuItemConfiguration;
    private javax.swing.JMenuItem jMenuItemMDP;
    private javax.swing.JMenuItem jMenuItemManuel;
    private javax.swing.JPanel jPanelAlarme;
    private javax.swing.JPanel jPanelCam;
    private javax.swing.JPanel jPanelPhoto;
    private javax.swing.JSeparator jSeparator1;
    public static javax.swing.JTextField jTextFieldNumCodeBarre;
    private javax.swing.JButton jToggleButtonArretAlarme;
    private javax.swing.JToggleButton jToggleButtonPorte;
    // End of variables declaration                   
 
}
la classe GestionLecteur (qui gère le lecteur code barre):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 
 
import javax.comm.*;
import com.sun.comm.Win32Driver;
import java.io.*;
import java.util.*;
 
public class GestionLecteur extends Thread implements SerialPortEventListener {
 
        GestionSystemeGardien gest = new GestionSystemeGardien();
 
	private CommPortIdentifier portId;
	private SerialPort serialPort;
	private BufferedReader fluxLecture;
        private PrintStream serial_out = null;
	private boolean running;
        private String NumCodeBarre;
	/**
         * Constructeur qui récupère l'identifiant du port et lance l'ouverture.
        */
 
	public GestionLecteur(String port) {
		//initialisation du driver
  		Win32Driver w32Driver = new Win32Driver();
		w32Driver.initialize();
 
		//récupération de l'identifiant du port
		try {
			portId = CommPortIdentifier.getPortIdentifier(port);
		} catch (NoSuchPortException e) {
		}
 
		//ouverture du port
		try {
			serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
		} catch (PortInUseException e) {
		}
		//récupération du flux
		try {
			fluxLecture =
				new BufferedReader(
				new InputStreamReader(serialPort.getInputStream()));
		} catch (IOException e) {
		}
		//ajout du listener
		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {
		}
		//paramétrage du port
		serialPort.notifyOnDataAvailable(true);
		try {
 
			serialPort.setSerialPortParams(
                                gest.Vitesse,
				gest.Donnees,
				gest.Stop,
				gest.Parite);
		} catch (UnsupportedCommOperationException e) {
		}
		System.out.println("port ouvert, attente de lecture");
	}
    @Override
	public void run() {
		running = true;
		while (running) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
		}
		//fermeture du flux et port
		try {
			fluxLecture.close();
		} catch (IOException e) {
		}
		serialPort.close();
	}
	/**
         * Méthode de gestion des événements.
         */
	public void serialEvent(SerialPortEvent event) throws IOException {
		//gestion des événements sur le port :
		//on ne fait rien sauf quand les données sont disponibles
		switch (event.getEventType()) {
			case SerialPortEvent.DATA_AVAILABLE :
                            serial_out = new PrintStream(serialPort.getOutputStream(), true, "ISO-8859-1");
				NumCodeBarre = new String(); 
				try {
					//lecture du buffer et affichage
					NumCodeBarre = (String) fluxLecture.readLine();
                                        Interface_Gardien.CB = NumCodeBarre;
					//InterLC.Recup_Code.setText(codeBarre);
				} catch (IOException e) {
				}
				break;
		}
 
 
	}
 
        public String getNumCodeBarre()
        {
            return this.NumCodeBarre;          
        }
 
	/**
         * Permet l'arrêt du thread
         */
	public void stopThread() {
		running = false;
	}
	/**
         * Méthode principale de l'exemple.
         */
	public static void main(String[] args) {
		//Récuperation du port en argument
		String port = "COM1";
		//lancement de l'appli
		GestionLecteur modeEve=new GestionLecteur(port);
		modeEve.start();
		//"interface utilisateur"
		System.out.println("taper q pour quitter");
		//construction flux lecture
		BufferedReader clavier =
			new BufferedReader(new InputStreamReader(System.in));
		//lecture sur le flux entrée.
		try {
			String lu = clavier.readLine();
			while (!lu.equals("esc")) {
			}
		} catch (IOException e) {
		}
		modeEve.stopThread();
	}
}
En faite, je voudrais que lorqu'il y a un code barre de détecter, le thread s'arrête. Et que le thread redémarre quand je clique sur un bouton. Comment faire ?

Merci d'avance.