私は2つのボタンを持つglassfishの下にJSF Webアプリケーションをデプロイしました。最初は無限のスレッドを開始し、2番目のスレッドは停止します。私の問題は、 thread.Iがネット上の解決策で検索しましたが、場合vain.it作品に私はここにJ2EEアプリケーションとJ2SEアプリケーションではなく、を持って実行すると、私のコードは、あなたができるので、私は自分のコードを変更したアプリケーションサーバーで無限のスレッドを停止する方法
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