问题描述
我在终止线程这里有一个问题。问题是,我使用的小程序线程访问DOM JS和操作跨度innerHTML来右边的主题名称为的innerHTML。它正常工作,直到我不断刷新页面,但如果我不给它像200毫秒的睡眠和去哪个不要有任何的applet另一个页面,它会给我在Firebug控制台的错误:
I have got a problem here in terminating the threads. The problem is that I am using applet thread to access JS DOM and manipulate innerHTML of a span to right the Thread Name as the innerHTML. It works fine until I keep refreshing the page but if I dont give it a sleep of like 200ms and go to another page which dont have any applet on it, it will give me an error in firebug console:
document.getElementById("userName") is null
我所怀疑的是,虽然我上停止发送中断,我也通过在循环中,线程执行比JVM需要卸载它,我的时间快得多设置在运行运行的标志变量试图同让我的网页浏览器中改变,但小程序仍然在试图找到ID用户名的元素。
What I am suspecting is, although I send an interrupt on stop and I have also tried the same by setting a flag variable of running in run while loop, the thread executes much faster than the time JVM takes to unload it and I get my page changed in browser but applet is still trying to find an element with id userName.
是这样吗?
是否有任何变通呢?
有什么办法知道有多少线程目前正在运行?
有没有杀死所有的命令将在destroy()方法?
Is that true?Is there any work around of it?Is there any way to know how many threads are running at the moment?Is there any kill all command to send on destroy()?
我寻觅了很多,看到Java线程和小应用程序的所有的API,无法得到这个工作。
I have searched a lot, seen all the API of java threads and applets, couldn't get this to work.
下面是code:
import java.applet.Applet;
import netscape.javascript.*;
//No need to extend JApplet, since we don't add any components;
//we just paint.
public class firstApplet extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
firstThread first;
boolean running = false;
public class firstThread extends Thread{
JSObject win;
JSObject doc;
firstThread second;
public firstThread(JSObject window){
this.win = window;
}
public void run() {
try{
System.out.println("running... ");
while (!isInterrupted()){
doc = (JSObject) win.getMember("document");
String userName = Thread.currentThread().getName();
//String userName = "John Doe";
String S = "document.getElementById(\"userName\").innerHTML = \""+userName+"\";";
doc.eval(S);
Thread.sleep(5);
}
}catch(Exception e){
System.out.println("exception in first thread... ");
e.printStackTrace();
}
}
}
public void init() {
System.out.println("initializing... ");
}
public void start() {
System.out.println("starting... ");
JSObject window = JSObject.getWindow(this);
first = new firstThread(window);
first.start();
running = true;
}
public void stop() {
first.interrupt();
first = null;
System.out.println("stopping... ");
}
public void destroy() {
if(first != null){
first = null;
}
System.out.println("preparing for unloading...");
}
}
感谢
推荐答案
这个问题解决了,用JS变量来控制Java小程序的执行,完整的解决方案是在这里:http://ciitronian.com/blog/programming/time-efficiency-controlling-java-applet-from-javascript-especially-if-you-make-round-trips-to-the-web-page-from-applet/
This problem was solved, using JS variables to control Java applet execution, the complete solution is here: http://ciitronian.com/blog/programming/time-efficiency-controlling-java-applet-from-javascript-especially-if-you-make-round-trips-to-the-web-page-from-applet/
这篇关于在页面卸载终止Java小程序的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!