当前位置: 技术问答>java相关
急,在线等待,有关多线程applet的问题
来源: 互联网 发布时间:2015-11-04
本文导语: 我编写一个applet,单开一个线程,在其中创建socket连接,进行udp通讯,一直循环receive并处理,但在网页中刷新后,再接收信息时,就把整个浏览器关掉了!!!! 是不是刷新前的线程未及时终止惹的? ...
我编写一个applet,单开一个线程,在其中创建socket连接,进行udp通讯,一直循环receive并处理,但在网页中刷新后,再接收信息时,就把整个浏览器关掉了!!!!
是不是刷新前的线程未及时终止惹的?
我把主要代码,贴在此:
// execute...
public class config extends JApplet implements Runnable
{
private final int BUFSIZE=512;
private String message="Status Request";
private InetAddress hostAddress;
private DatagramSocket socket;
private Thread t=null;
private boolean stopThread=false;
private final int INPORT=8388;
private final String hostname="192.168.201.104";
private myPanel SRPanel;
public void init()
{
initialPanel();
t=new Thread(this);
//window.eval("alert('new Thread(this)');");
t.start();
//stopThread=false;
}
public void destroy()
{
stopThread=true;
//t.setPriority(Thread.MIN_PRIORITY);
t.interrupt();
//window.eval("alert('destroy() end');");
//t.stop();
//System.out.println("Set stopThread OK! t.isInterrupt:"+t.isInterrupted());
t=null;
//System.out.println("stopThread=="+stopThread);
System.out.println("Set stopThread OK!");
}
public void run()
{
//window.eval("alert('run()');");
try{
byte[] outBuffer = new byte[BUFSIZE];
byte[] inBuffer=new byte[BUFSIZE];
socket=new DatagramSocket();
try
{
hostAddress=InetAddress.getByName(hostname);
}catch (UnknownHostException e){}
outBuffer=message.getBytes();
DatagramPacket sdpacket=new DatagramPacket(outBuffer,outBuffer.length,hostAddress,INPORT);
DatagramPacket rdpacket=new DatagramPacket(inBuffer,BUFSIZE);
try
{
socket.send(sdpacket);
System.out.println("Send request: "+message);
}catch(java.io.IOException e)
{//window.eval("alert('IOException');");
}
// window.eval("alert('end initial');");
while((!t.isInterrupted())&&(stopThread==false))
{
try
{
socket.receive(rdpacket);
t.sleep(2);
if(t.isInterrupted())
{/**/System.out.println("if(t.isInterrupted())"+t.isInterrupted());
return;
}
/**/System.out.println("else(t.isInterrupted())"+t.isInterrupted());
message=new String(rdpacket.getData(),0,rdpacket.getLength());
if((message.length()==12)&&(stopThread==false))
{
System.out.println("Receive status_changed infor: "+message);
setStatus(message);
}
else if ((message.equals("verify"))&&(stopThread==false))
{
System.out.println("Receive address_check infor: "+message);
message="verifyok";
outBuffer=message.getBytes();
sdpacket=new DatagramPacket(outBuffer,outBuffer.length,hostAddress,INPORT);
socket.send(sdpacket);
}
else if((message.length()>16)&&(stopThread==false))
{
String msg=message.substring(0,16);
if(msg.equalsIgnoreCase("A:6:0,1&B:6:0,0&"))
{
System.out.println("Receive initial infor: "+message);
initialStatus(message);
}
}
else if(stopThread==false)
System.out.println("Receive invalid infor: "+message);
}
catch(java.io.IOException e){System.out.println("Receive or send infor error!");}
}//while()
socket.close();
}
catch (Exception e)
{
//window.eval("alert('can not create socket!');");
}
}//run()
private synchronized void initialPanel()
{
......
notifyAll();
}
private synchronized void initialStatus(String str)
{
......
notifyAll();
}
}//class end
是不是刷新前的线程未及时终止惹的?
我把主要代码,贴在此:
// execute...
public class config extends JApplet implements Runnable
{
private final int BUFSIZE=512;
private String message="Status Request";
private InetAddress hostAddress;
private DatagramSocket socket;
private Thread t=null;
private boolean stopThread=false;
private final int INPORT=8388;
private final String hostname="192.168.201.104";
private myPanel SRPanel;
public void init()
{
initialPanel();
t=new Thread(this);
//window.eval("alert('new Thread(this)');");
t.start();
//stopThread=false;
}
public void destroy()
{
stopThread=true;
//t.setPriority(Thread.MIN_PRIORITY);
t.interrupt();
//window.eval("alert('destroy() end');");
//t.stop();
//System.out.println("Set stopThread OK! t.isInterrupt:"+t.isInterrupted());
t=null;
//System.out.println("stopThread=="+stopThread);
System.out.println("Set stopThread OK!");
}
public void run()
{
//window.eval("alert('run()');");
try{
byte[] outBuffer = new byte[BUFSIZE];
byte[] inBuffer=new byte[BUFSIZE];
socket=new DatagramSocket();
try
{
hostAddress=InetAddress.getByName(hostname);
}catch (UnknownHostException e){}
outBuffer=message.getBytes();
DatagramPacket sdpacket=new DatagramPacket(outBuffer,outBuffer.length,hostAddress,INPORT);
DatagramPacket rdpacket=new DatagramPacket(inBuffer,BUFSIZE);
try
{
socket.send(sdpacket);
System.out.println("Send request: "+message);
}catch(java.io.IOException e)
{//window.eval("alert('IOException');");
}
// window.eval("alert('end initial');");
while((!t.isInterrupted())&&(stopThread==false))
{
try
{
socket.receive(rdpacket);
t.sleep(2);
if(t.isInterrupted())
{/**/System.out.println("if(t.isInterrupted())"+t.isInterrupted());
return;
}
/**/System.out.println("else(t.isInterrupted())"+t.isInterrupted());
message=new String(rdpacket.getData(),0,rdpacket.getLength());
if((message.length()==12)&&(stopThread==false))
{
System.out.println("Receive status_changed infor: "+message);
setStatus(message);
}
else if ((message.equals("verify"))&&(stopThread==false))
{
System.out.println("Receive address_check infor: "+message);
message="verifyok";
outBuffer=message.getBytes();
sdpacket=new DatagramPacket(outBuffer,outBuffer.length,hostAddress,INPORT);
socket.send(sdpacket);
}
else if((message.length()>16)&&(stopThread==false))
{
String msg=message.substring(0,16);
if(msg.equalsIgnoreCase("A:6:0,1&B:6:0,0&"))
{
System.out.println("Receive initial infor: "+message);
initialStatus(message);
}
}
else if(stopThread==false)
System.out.println("Receive invalid infor: "+message);
}
catch(java.io.IOException e){System.out.println("Receive or send infor error!");}
}//while()
socket.close();
}
catch (Exception e)
{
//window.eval("alert('can not create socket!');");
}
}//run()
private synchronized void initialPanel()
{
......
notifyAll();
}
private synchronized void initialStatus(String str)
{
......
notifyAll();
}
}//class end
|
线程的新建和启动好像不应该写在init()中吧
t=new Thread(this);
t.start();
标准的applet的范例如下:
//Called by the browser or applet viewer to inform this applet that it has been loaded into the system.
public void init() {
}
//Called by the browser or applet viewer to inform this applet that it should start its execution.
public void start(){
loopMainStart();
}
//Called by the browser or applet viewer to inform this applet that it should stop its execution.
public void stop()
{
loopMainStop();
}
private void loopMainStart()
{
if(loopMainThread == null) {
loopMainThread = new Thread(this);
loopMainThread.start();
}
}
private void loopMainStop()
{
if(loopMainThread != null){
//loopMainThread.stop(); //Deprecated. This method is inherently unsafe.
loopMainThread = null;
}
}
t=new Thread(this);
t.start();
标准的applet的范例如下:
//Called by the browser or applet viewer to inform this applet that it has been loaded into the system.
public void init() {
}
//Called by the browser or applet viewer to inform this applet that it should start its execution.
public void start(){
loopMainStart();
}
//Called by the browser or applet viewer to inform this applet that it should stop its execution.
public void stop()
{
loopMainStop();
}
private void loopMainStart()
{
if(loopMainThread == null) {
loopMainThread = new Thread(this);
loopMainThread.start();
}
}
private void loopMainStop()
{
if(loopMainThread != null){
//loopMainThread.stop(); //Deprecated. This method is inherently unsafe.
loopMainThread = null;
}
}
|
You Applet
start(){
...//启动线程
}
stop(){
...//停止线程(不是调用Thread.stop())
}
start(){
...//启动线程
}
stop(){
...//停止线程(不是调用Thread.stop())
}