【问题标题】:How to stop() Thread when it created on server side for client in javajava - 在服务器端为客户端创建java时如何停止()线程
【发布时间】:2015-02-13 07:01:29
【问题描述】:

当客户端离开服务器连接时,我如何停止为客户端和服务器之间的通信创建的服务器线程,请告诉我如何向与该服务器连接的所有客户端发送单个客户端消息,谢谢提前:)。

ChatServer.java

import java.net.*;

import java.io.*;

   public class ChatServer implements Runnable

   { 
 private ServerSocket     server = null;

   private Thread           thread = null;

   private ChatServerThread client = null;

   public ChatServer(int port)
   { 
 try
      {  System.out.println("Binding to port " + port + ", please wait     ...");
         server = new ServerSocket(port);  
         System.out.println("Server started: " + server);
         start();
      }
      catch(IOException ioe)
      {  System.out.println(ioe); }
   }
   public void run()
   {  
   while (thread != null)
      { 
   try
         {  System.out.println("Waiting for a client ..."); 
            addThread(server.accept());
         }
         catch(IOException ie)
         {  System.out.println("Acceptance Error: " + ie); }
      }
   }
      public void addThread(Socket socket)
   {  System.out.println("Client accepted: " + socket);
      client = new ChatServerThread(this, socket);
      try
      {  client.open();
         client.start();
      }
      catch(IOException ioe)
      {  System.out.println("Error opening thread: " + ioe); }
   }
  public void start()
   {  if (thread == null)
      {  thread = new Thread(this); 
         thread.start();
      }
   }
      public void stop()
   {  if (thread != null)
      {  thread.stop(); 
         thread = null;
      }
   }
   public static void main(String args[])
   {  ChatServer server = null;
      int x=2111;
      if (x<1)
         System.out.println("Usage: java ChatServer port");
      else
         server = new ChatServer(x);
   }
   }

聊天服务器线程

import java.net.*;
import java.io.*;

public class ChatServerThread extends Thread
{  private Socket          socket   = null;
   private ChatServer      server   = null;
   private int             ID       = -1;
   private DataInputStream streamIn =  null;

   public ChatServerThread(ChatServer _server, Socket _socket)
   {  server = _server;  socket = _socket;  ID = socket.getPort();
   }
   public void run()
   {  System.out.println("Server Thread " + ID + " running."+Thread.activeCount());
      while (true)
      {  try
         {  System.out.println(streamIn.readUTF());
         }
         catch(IOException ioe) {  }
      }
   }
   public void open() throws IOException
   {  streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
   }
   public void close() throws IOException
   { 
       if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
   }
}

【问题讨论】:

  • 您永远无法停止()线程。尽管看起来你可以。
  • 但是,如果我不能将与之连接的客户端捆绑在一起,则会造成处理器繁忙
  • 我没有看到你在任何地方都在调用 'client.stop()'!

标签: java multithreading


【解决方案1】:

ChatServerThread:

public void run()
    try {
        // thread code
    } catch(InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        // cleanup code, if required
    }
}

ChatServer:

public void stop() {
    if (thread != null) {
        thread.interrupt(); 
        thread = null;
    }
}

这不会停止线程,但它会向线程发出信号以停止它正在执行的操作 - 它可能会或可能不会立即停止,甚至根本不会停止,具体取决于线程当前正在执行的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多