用SocketChannel, client.configureBlocking(true);然后这个socketchannel就是阻塞模式的,可以在其他线程中close它,阻塞的线程会收到一个exception.
package closesocket;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.lang.*;
import javax.swing.*;
import java.awt.event.*;
public class Client extends JFrame
{
  public SocketChannel client = null;
  public InetSocketAddress isa = null;
  public RecvThread rt = null;
  JButton jb=new JButton("close socket");
  public Client()
  {
    this.getContentPane().add(jb);
    jb.addActionListener(new ActionListener()
                         {
                           public void actionPerformed(ActionEvent e)
                               {
                                 try
                                 {
                                   client.close();
                                 }
                                 catch(Exception ioe)
                                 {
                                   ioe.printStackTrace();
                                 }
                               }
                         });
                         setSize(600,400);
setVisible(true);  }  public void makeConnection() {
    int result = 0;
    try {      client = SocketChannel.open();
      isa = new InetSocketAddress("127.0.0.1", 5555);
      client.connect(isa);
      client.configureBlocking(true);
      receiveMessage();
    }
    catch (UnknownHostException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    while ( (result = sendMessage()) != -1) {
    }    try {
      client.close();
      System.exit(0);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }  public int sendMessage() {
    System.out.println("Inside SendMessage");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String msg = null;
    ByteBuffer bytebuf = ByteBuffer.allocate(1024);
    int nBytes = 0;
    try {
      msg = in.readLine();
      System.out.println("msg is " + msg);
      bytebuf = ByteBuffer.wrap(msg.getBytes());
      nBytes = client.write(bytebuf);
      System.out.println("nBytes is " + nBytes);
      if (msg.equals("quit") || msg.equals("shutdown")) {
        System.out.println("time to stop the client");
        interruptThread();
        try {
          Thread.sleep(5000);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        client.close();
        return -1;
      }    }
    catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("Wrote " + nBytes + " bytes to the server");
    return nBytes;
  }  public void receiveMessage() {
    rt = new RecvThread("Receive THread", client);
    rt.start();
  }  public void interruptThread() {
    rt.val = false;
  }  public static void main(String args[]) {
    Client cl = new Client();
    cl.makeConnection();
  }  public class RecvThread
      extends Thread {
    public SocketChannel sc = null;
    public boolean val = true;    public RecvThread(String str, SocketChannel client) {
      super(str);
      sc = client;
    }    public void run() {      System.out.println("Inside receivemsg");
      int nBytes = 0;
      ByteBuffer buf = ByteBuffer.allocate(2048);
      try {
        while (val) {
          while ( (nBytes = nBytes = client.read(buf)) > 0) {
            buf.flip();
            Charset charset = Charset.forName("us-ascii");
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer charBuffer = decoder.decode(buf);
            String result = charBuffer.toString();
            System.out.println(result);
            buf.flip();
          }
        }
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      System.out.println(this.getClass().getName()+" stopped");
    }
  }
}

解决方案 »

  1.   

    package closesocket;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    import java.io.*;
    import java.net.*;public class SockServerExam
    {
            int portnum = 5555;
            public static void main(String args[])
            {
                    new SockServerExam().work();
            }
            void work()
            {
                    try
                    {
                            ServerSocket server_socket=new ServerSocket(portnum);
                            System.out.println("listen on "+portnum);
                            while(true)
                            {
                                    Socket socket = server_socket.accept();
                                    System.out.println("New connection accepted " +
                                               socket.getInetAddress() +
                                               ":" + socket.getPort());
                                            RequestHandler rh = new RequestHandler(socket);
                                            Thread th = new Thread(rh);
                                            th.start();                        }
                    }
                    catch(IOException eio)
                    {
                            eio.printStackTrace();
                    }        }}
    class RequestHandler implements Runnable
    {
            Socket conn;
            InputStream input;
        OutputStream output;
        BufferedReader br;
        byte[] buf;
            public RequestHandler(Socket s)
            {
                    try
                    {
                            conn=s;
                            this.input = conn.getInputStream();
                            this.output = conn.getOutputStream();
                            buf = new byte[256];
                    }
                    catch(Exception e)
                    {
                            e.printStackTrace();
                    }
            }
            public void run()
            {
                    int receive_count=0;
                    int readcount = 1;
                    try
                    {
                            while(readcount>0)
                            {
                                    readcount=input.read(buf);
                                    System.out.println("received num:"+readcount);
                                    if(readcount>0)
                                            receive_count+=readcount;
                            }
                            System.out.println("connection accepted " +
                                               conn.getInetAddress() +
                                               ":" + conn.getPort()+ " received bytes:" + receive_count);
                            input.close();
                            output.close();
                            conn.close();
                    }
                    catch(Exception e)
                    {
                            e.printStackTrace();
                    }        }
    }
      

  2.   

    第一次见到五星级人物,羡慕。
    学习ing....
      

  3.   

    羡慕+学习,我只知道把一个THREAD赋值为NULL来杀线程,倒没想到这么深,学习!!!
      

  4.   

    JDK1.4支持异步IO在javax.nio包中,专用于网络IO
      

  5.   

    我也是第一次看到五星级的人物啊。
    学习ing
      

  6.   

    除了saucer,第二个五星楼主有什么密级请到高手,多教教我们小弟呵呵
      

  7.   

    谢谢五星兄的回答,我正在学nio。Socket socket = server_socket.accept();
        我的目的是:暂停、睡眠它所在线程。
        为了从accept()继续运行下去,我不得不新建一个客户连接,然后再关闭,才能执行后面的wait(),有没有好的方法?请指教。
      

  8.   

    学习啊~~~>_<~~~
    羡慕!!!!
      

  9.   

    这篇写的比较清楚。      
            java高级多线程编程(一)-关于线程的停止问题
                             出自matrix.org.cn
       多线程是java的一个优势,java使得程序员可以很方便的进行多线程程序开发。获得更好的性能。
       关于多线程的概念以及一般的多线程编程,比如如何以及为何实现runnable接口,为何stop()会被Deprecated掉等等,这个请看matrix之前的多线程编程基础或者sun的java文档。
       关于多线程编程,有几点这里要提到的:
    1。既然stop()是不被推荐的,那么我们如何停止一个线程呢?直接kill吗?在这里,笔者总结一种比较通用也比较稳定的方法:class threadtest extent Thread{
    //skip some code..
     boolean runflag=true; 
     
     public synchronized void stopthread()
          {
              runflag=false;         
          } public synchronized boolean getrunflag()
     {
              return runflag; 
     }  public void run() {
           runflag=true;
            try {
            while (getrunflag()) {
                    code1;
                    code2;  
                   //put your code here
                  }
            }
          }
          catch (IOException e) {
            e.printStackTrace();
          }
          System.out.println(this.getClass().getName()+" stopped");
        }//skip some code..
    }
         这样,每当需要停止该线程时,只需调用stopthread()即可。这里有两点需要注意:1)我们用了一个同步方法getrunflag()来得到当前的状态,为什么用这个方法而不是直接使用while(runflag)呢?
       这个是因为在java的多线程模型中,有一个公共的对象存储区,但是每个对象都有自己的私有备份,当一个线程改变了状态,jvm并不能保证这个线程改变过的变量即时更新公共对象存储区的状态,可能(可能性不大)造成问题。
    所以建议有好的设计习惯,采用同步方法来获得当前的runflag值。2)还有一点,特别是涉及网络的多线程,如果发生了网络阻塞(在while循环里面发生),那么,即使runflag状态比如改变成false,由于程序被阻塞,线程用这种方法是永远都不会被停止的。
       举个例子:比如上面的程序,如果code1是一段网络程式,如果在code1发生了阻塞,阻塞的意义就是得不到请求的
    资源,在无限期等待,这个时候,runflag状态的变化对while循环是起不了作用的,线程不会被停止。
       笔者曾经参与多个涉及到获取网络资源的java程式,经常遇到因为网络的阻塞引起的线程问题。
       如果你的程式可能涉及到网络阻塞,或者有可能发生某种消息接受的阻塞。那么,请不要用这种方法来停止线程。具体方法请看笔者另外一片文章:高级多线程编程(二)-多线程中的监控与超时问题。
                                    (待续)
                  【matrix原创】-转载请注明出自matrix.org.cn