我有一個JSF Web應用程序部署在Glassfish中,其中有兩個按鈕。第一個啓動一個無限線程,第二個停止它。我的問題是,我無法停止一個運行thread.I搜索了在網上一個解決方案,但在vain.it工作的情況下,我有一個J2SE應用程序,但不與這裏的J2EE應用程序是我的代碼如何停止應用程序服務器中的無限線程
package com.example.beans;
import org.apache.commons.lang.RandomStringUtils;
public class MyBusinessClass {
public static void myBusinessMethod() {
/* this method takes a lot of time */
int i = 1;
while (i == 1) {
String random = RandomStringUtils.random(3);
System.out.println(random);
}
}
}
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() {
MyBusinessClass.myBusinessMethod();
}
}
@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;
}
}
我已經改變了我的代碼,這樣你就可以真正瞭解我的問題
是否允許JSF代碼創建線程? – Raedwald