IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Thread, run et stop


Sujet :

avec Java

  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 389
    Points : 227
    Points
    227
    Par défaut Thread, run et stop
    bonjour a tous
    voila, j'ai un léger soucis.
    mon programme crée des Thread, les thread, vont bien dans la méthode Run(), par exemple exécute un System.out.print(), mais, impossible de leur faire faire une boucle ou autre chose (dans mon exemple, je ne peut pas faire tourné un chrono ?????)
    aussi, si je stop met Thread avec la méthode stop(), je ne peut pas re-créer d'autre thread (exception).
    Donc, mes questions sont:
    -comment stopé une thread proprement (je doit pouvoir regré d'autre thread aprés)
    - pourquoi mes thread n'exécute telle pas mes boucles???????

    le code va surement vous premettre de mieux comprendre mon probleme.

    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
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    //import javax.swing.event.*;
     
    @SuppressWarnings("serial")
    public class Ihmprocoswing extends JFrame implements ActionListener, Runnable {
     
    	/**
             * @param args
             */
     
    	private int seconde, minute, heure;
     
    	public Thread th1;
    	public Thread th2;
    	public Thread th3;
    	public Thread th4;
    	public Thread th5;
    	public Thread th6;
    	public Thread th7;
    	public Thread th8;
    	public Thread chrono;
     
    	private JButton lancer;
    	private JButton stop;
     
    	private JLabel lab1;
    	private JLabel lab2;
    	private JLabel lab3;
    	private JLabel lab4;
    	private JLabel lab5;
    	private JLabel lab6;
    	private boolean passage = false;
     
    	private JComboBox cb;
     
    	int nbProc = Runtime.getRuntime().availableProcessors();// permet de savoir
    	// le nombre de
    	// processeur
    	String name = System.getProperty("os.name"); // donne le nom de l'os
    	String version = System.getProperty("os.version"); // donne la version de
     
    	// l'os
     
    	public Ihmprocoswing() {
    		setTitle("Test Processeur by Hannibal For hardware-pc");
    		setSize(650, 200);
    		setLocation(500, 500);
    		setLayout(new BorderLayout());
     
    		Color col = new Color(15, 212, 214);
     
    		JPanel pan1 = new JPanel(new GridLayout(1, 4, 3, 3));
    		JPanel pan2 = new JPanel(new GridLayout(1, 4, 3, 3));
     
    		// ===================NORTH========================
     
    		// lancer = new JButton(new ImageIcon("image/lancer2.png"));
    		lancer = new JButton("lancer");
    		lancer.addActionListener(this);
    		pan1.add(lancer);
     
    		stop = new JButton("stop"); // a mettre image
    		stop.addActionListener(this);
    		pan1.add(stop);
     
    		String combo[] = { "30 min", "1 Heure", "2 Heures", "4 Heures",
    				"6 Heures", "Infini" }; // contenu de la comboBox
    		cb = new JComboBox(combo);
    		cb.addActionListener(this);
    		pan1.add(cb);
     
    		lab1 = new JLabel("OS: " + name + " | " + version);
    		pan1.add(lab1);
     
    		// ===================SOUTH========================
    		lab2 = new JLabel("Temps écoulé: ");
    		pan2.add(lab2);
     
    		lab3 = new JLabel("00h00min00sec");
    		pan2.add(lab3);
     
    		lab4 = new JLabel("Temps Choisi: ");
    		pan2.add(lab4);
     
    		lab5 = new JLabel("00h30min00sec ");
    		pan2.add(lab5);
     
    		// ===================CENTER========================
     
    		lab6 = new JLabel("CENTER", JLabel.CENTER);
    		add(lab6, BorderLayout.CENTER);
     
    		// ================================================
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// fermeture fenetre
     
    		chrono = new Thread(this);
    		chrono.setName("chrono");
    		pan1.setBackground(col);
    		pan2.setBackground(col);
    		add(pan1, BorderLayout.NORTH);
    		add(pan2, BorderLayout.SOUTH);
     
    		setVisible(true);
    	}
     
    	@SuppressWarnings("deprecation")
    	public void actionPerformed(ActionEvent e) {
     
    		if (e.getSource() == lancer) {
    			// verif pour ne pas essayé de lancé 2
    			// fois les même Thread
    			if (passage == false) {
    				passage = true;
    				try {
    					// lancement des Thread
    					switch (nbProc) {
    					case 1:
    						th1 = new Thread(this);
    						th1.setName("th1"); //donne le nom th1 a la thread
    						th1.start();
    						break;// lancement d'un thread
     
    					case 2:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th1.start();
    						th2.start();
    						break;// lancement de 2 thread
     
    					case 3:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th1.start();
    						th2.start();
    						th3.start();
    						break;
     
    					case 4:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th4 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th4.setName("th4");
    						th1.start();
    						th2.start();
    						th3.start();
    						th4.start();
    						break;
     
    					case 5:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th4 = new Thread(this);
    						th5 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th4.setName("th4");
    						th5.setName("th5");
    						th1.start();
    						th2.start();
    						th3.start();
    						th4.start();
    						th5.start();
    						break;
     
    					case 6:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th4 = new Thread(this);
    						th5 = new Thread(this);
    						th6 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th4.setName("th4");
    						th5.setName("th5");
    						th6.setName("th6");
    						th1.start();
    						th2.start();
    						th3.start();
    						th4.start();
    						th5.start();
    						th6.start();
    						break;
     
    					case 7:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th4 = new Thread(this);
    						th5 = new Thread(this);
    						th6 = new Thread(this);
    						th7 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th4.setName("th4");
    						th5.setName("th5");
    						th6.setName("th6");
    						th7.setName("th7");
    						th1.start();
    						th2.start();
    						th3.start();
    						th4.start();
    						th5.start();
    						th6.start();
    						th7.start();
    						break;
     
    					case 8:
    						th1 = new Thread(this);
    						th2 = new Thread(this);
    						th3 = new Thread(this);
    						th4 = new Thread(this);
    						th5 = new Thread(this);
    						th6 = new Thread(this);
    						th7 = new Thread(this);
    						th8 = new Thread(this);
    						th1.setName("th1");
    						th2.setName("th2");
    						th3.setName("th3");
    						th4.setName("th4");
    						th5.setName("th5");
    						th6.setName("th6");
    						th7.setName("th7");
    						th8.setName("th8");
    						th1.start();
    						th2.start();
    						th3.start();
    						th4.start();
    						th5.start();
    						th6.start();
    						th7.start();
    						th8.start();
    						break;
    					}
    				} catch (Exception exc) {
    					System.out
    							.println("probleme dans le lancements des thread");
    					System.exit(0);
    				}
     
    			}
    			lab6.setText("En preparation");
     
    			switch (cb.getSelectedIndex()) {
    			case 0:
    				lab5.setText("00h30min00sec");
    				break;
     
    			case 1:
    				lab5.setText("01h00min00sec");
    				break;
     
    			case 2:
    				lab5.setText("02h00min00sec");
    				break;
     
    			case 3:
    				lab5.setText("04h00min00sec");
    				break;
     
    			case 4:
    				lab5.setText("06h00min00sec");
    				break;
     
    			case 5:
    				lab5.setText("INFINI");
    				break;
    			}
     
    			chrono.start();
    			passage = false;
    		} else {
    			if (e.getSource() == stop) {
     
    				try {
     
    					switch (nbProc) {
    					case 1:
    						th1.stop();
    						break;// stop d'un thread
     
    					case 2:
    						th1.stop();
    						th2.stop();
    						break;
     
    					case 3:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						break;
     
    					case 4:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						th4.stop();
    						break;
     
    					case 5:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						th4.stop();
    						th5.stop();
    						break;
     
    					case 6:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						th4.stop();
    						th5.stop();
    						th6.stop();
    						break;
     
    					case 7:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						th4.stop();
    						th5.stop();
    						th6.stop();
    						th7.stop();
    						break;
     
    					case 8:
    						th1.stop();
    						th2.stop();
    						th3.stop();
    						th4.stop();
    						th5.stop();
    						th6.stop();
    						th7.stop();
    						th8.stop();
    						break;
    					}
    				} catch (Exception exc) {
    					System.out.println("probleme dans les arret de thread");
    					System.exit(0);
    				}
    				lab5.setText("00h30min00sec");
    				cb.setSelectedIndex(0);
    				lab6.setText("Fin de la procedure d'arret");
     
    			}
    		}
    	}
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		String strj = Thread.currentThread().getName();// donne nom thread
    		// apellante
    		System.out.println(strj);
    		System.out.println("ici");
    		if (strj.equals("chrono")) {
     
    			while (lab3.getText().equals(lab5.getText())) {
     
    				try {
    					Thread.sleep(1000);
    				} catch (InterruptedException e) {
    					System.out.println("Probleme dans le temps du chrono");
    				}
    				seconde++;
    				if (seconde == 60) {
    					seconde = 00;
    					minute++;
    				}
    				if (minute == 60) {
    					minute = 00;
    					heure++;
    				}
    				lab3.setText(heure + "h" + minute + "min" + seconde + "sec");/*
    																			 * rafraichir
    																			 * le
    																			 * label
    																			 */
    			}
    		}
    		Algotest algo = new Algotest();
     
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new Ihmprocoswing();
     
    	}
     
    }
    voila, merci d'avance

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    121
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 121
    Points : 136
    Points
    136
    Par défaut
    De préférence tu laisses chaque Thread mourir de lui même... C'est à dire que tu laisse le run() se terminer.

    Si tu veux lancer une tache à interval régulier...Il existe deux classes en Java java.util.TimerTask pour définir tes taches et java.util.Timer pour leur lancement.

  3. #3
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    je rajouterais, mais tes classes runnable à part de la frame, ca rendra le code plus lisible. Là tu crée un classe foure-tout qui fait runable, frame et actionlistener en meme temps, c'est pas très lisible.

  4. #4
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 389
    Points : 227
    Points
    227
    Par défaut
    je veut bien, mais, j'ai eu des probleme lors du deplacement de run or de cette classe.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Classe gestion de thread - Capter evenement stop
    Par sone47 dans le forum Threads & Processus
    Réponses: 3
    Dernier message: 14/12/2012, 12h05
  2. run.jar stop.jar fournis par Jboss 4
    Par vain_mich dans le forum Wildfly/JBoss
    Réponses: 2
    Dernier message: 08/08/2007, 13h50
  3. [Thread] Stop dépréciée, que faire?
    Par the_ugly dans le forum Concurrence et multi-thread
    Réponses: 13
    Dernier message: 27/10/2005, 11h39
  4. [Thread]run une fois et plus apres
    Par maxvador dans le forum Concurrence et multi-thread
    Réponses: 4
    Dernier message: 29/12/2004, 15h31
  5. [Thread]Faut-il boucler la méthode run ?
    Par Pill_S dans le forum Concurrence et multi-thread
    Réponses: 6
    Dernier message: 17/06/2004, 22h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo