Bonjour je suis en train de faire une application J2EE JSF sous glassfish.
J'ai une boutton pour lancer une thread qui vas boucler a l'infinie et une autre qui vas essayer de stopper cette thread.
Le probléme est que je veux pas toucher a mon code métier mais juste interrompre mon thread par n'importe quel moyen méme dangereuse.voici mon code
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 package com.example.beans; import java.util.Random; import java.util.TimerTask; import org.apache.commons.lang.RandomStringUtils; import org.apache.log4j.Logger; import com.example.core.RandomUtils; public class MySimpleRunnableTask implements Runnable { private Logger logger = Logger.getLogger(MySimpleRunnableTask.class); @Override public void run() { /* i do not want to touch my code here*/ int i = 1; while (i == 1) { String random = RandomStringUtils.random(3); logger.info(random); } } }j'ai partout recherché dans le net mais en vain et je sais bien qu'il existe une solution car je l'ai déja vue dans une application proprietaire.
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 @ManagedBean(name = "MainView") @SessionScoped public class MainView { private static Thread myThread; @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) public String startSimpleThread() throws SecurityException, NoSuchMethodException, InterruptedException { MySimpleRunnableTask mySimpleRunnableTask = new MySimpleRunnableTask(); myThread = new Thread(mySimpleRunnableTask); myThread.start(); return null; } @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) public String stopSimpleThread() throws SecurityException, NoSuchMethodException, InterruptedException { myThread.interrupt(); return null; }
Merci
Partager