import java.io.* ;
import java.net.* ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.RejectedExecutionException ;
class EchoServer
{
private int port = 8000 ;
private ServerSocket serverSocket;
private ExecutorService executorService ;
private final int POOL_SIZE = 4;
private int portForShutdown = 8001 ;
private ServerSocket serverSocketForShutdown ;
private boolean isShutdown = false ;
private Thread shutdownThread = new Thread()
{
public void start()
{
this.setDaemon(true) ;
//这个super.start是什么意思?
super.start() ;
System.out.println(super.getClass()) ;
}
public void run()
{
Socket socketForShutdown = null ;
try
{
socketForShutdown = serverSocketForShutdown.accept() ;
BufferedReader br = new BufferedReader(new InputStreamReader(socketForShutdown.getInputStream())) ;
String command = br.readLine() ;
if (command.equals("shutdown"))
{
long beginTime = System.currentTimeMillis() ;
socketForShutdown.getOutputStream().write("服务器正在关闭\r\n".getBytes()) ;
isShutdown = true ;
executorService.shutdown() ;
while(!executorService.isTerminated())
{
executorService.awaitTermination( 30 , TimeUnit.SECONDS ) ;
}
serverSocket.close() ;
long endTime = System.currentTimeMillis() ;
socketForShutdown.getOutputStream().write(("服务器已经关闭, " + "关闭服务器用时: " + (endTime-beginTime) + "毫秒\r\n").getBytes() );
socketForShutdown.close() ;
serverSocketForShutdown.close() ;
}
else
{
socketForShutdown.getOutputStream().write("错误的命令\r\n".getBytes()) ;
socketForShutdown.close() ;
}
}
catch (Exception e)
{
e.printStackTrace() ;
}
}
} ;
public EchoServer() throws IOException
{
serverSocket = new ServerSocket(port) ;
serverSocket.setSoTimeout(60000) ;
serverSocketForShutdown = new ServerSocket(portForShutdown) ;
executorService = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors()*POOL_SIZE) ;
shutdownThread.start() ;
System.out.println("服务器启动") ;
}
public void service()
{
while(!isShutdown)
{
Socket socket = null ;
try
{
socket = serverSocket.accept() ;
socket.setSoTimeout(60000) ;
executorService.execute(new Handler(socket)) ;
}
catch (SocketTimeoutException e)
{
//不做任何处理e.printStackTrace() ;
}
catch(RejectedExecutionException e)
{
try
{
if(socket != null)
socket.close() ;
}
catch (IOException ex)
{
ex.printStackTrace() ;
}
return ;
}
catch(SocketException e)
{
if(e.getMessage().indexOf("socket closed") != -1)
return ;
}
catch(IOException e)
{
e.printStackTrace() ;
}
}
}
}
public class Server
{
public static void main(String args[])throws IOException
{
new EchoServer().service() ;
}
}
class Handler implements Runnable
{
private Socket socket ;
public Handler(Socket socket)
{
this.socket = socket ;
}
private PrintWriter getWriter(Socket socket)throws IOException
{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
private BufferedReader getReader(Socket socket)throws IOException
{
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg)
{
return "echo:" + msg;
}
public void run()
{
try
{
System.out.println("New connection accepted " +
socket.getInetAddress() + ":" + socket.getPort());
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
String msg = null;
while ((msg = br.readLine()) != null)
{
System.out.println(msg);
pw.println(echo(msg));
if (msg.equals("bye"))
break;
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(socket != null)socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
这个是书上的例子, 但是这个super.start()
是什么意思? 运行之后发现super.getClass()
返回的是class EchoServer$1
!
谢谢大家, 我知道我哪里糊涂了,
EchoServer$1
是匿名内部类class文件的命名规则 ,主类名+$+(1,2,3...)
, super.start()是调用Thread类的start()函数, 而非调用自身.