|
- 我写了两个类...想通过java的垃圾收集来关掉我在运行的thread...
- 但调用function2()进行关闭时...会出现
- java.net.SocketException: socket closed
- at java.net.PlainDatagramSocketImpl.receive0(Native Method)
- at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
- at java.net.DatagramSocket.receive(DatagramSocket.java:712)
- at serverRun.Server.recPacket(Server.java:33)
- at serverRun.Server.run(Server.java:22)
- class A{
- private B b;
- function1(){
- b = new B();
- b.start();
- }
- function2(){
- b.threadStop()
- }
- }
- class B extends Thread{
- private boolean threadStop = false;
- int port;
- DatagramSocket socket;
- public void run(){
- try{
- while(threadStop == false){
- //udp 接收信息
- }catch(...){
- ....
- }
- }
- public void threadStop(){
- this.threadStop = true;
- this.port = 0;
- try{
- //socket.disconnect(); 如果用disconnect 就会倒致程序死掉..
- socket.close();
- }catch(Exception ex){
-
- }finally{
- this.socket = null;
- }
- }
- }
复制代码 |
|