1.服务端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public
class MultiThreadServer
{
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;
//线程池
private final int POOL_SIZE=10;
//单个CPU线程池大小
public MultiThreadServer()
throws IOException{
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
serverSocket=
new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
System.out.println("服务器启动");
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
public void service(){
while(
true){
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
Socket socket=
null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
socket=serverSocket.accept();
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
executorService.execute(
new Handler(socket));
![](http://static.oschina.net/uploads/img/201305/10113016_5nz8.gif)
}
catch (Exception e) {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
e.printStackTrace();
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
public static void main(String[] args)
throws IOException {
new MultiThreadServer().service();
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
![](http://static.oschina.net/uploads/img/201305/10113016_z8YP.gif)
}
class Handler
implements Runnable
{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
private PrintWriter getWriter(Socket socket)
throws IOException{
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,
true);
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
private BufferedReader getReader(Socket socket)
throws IOException{
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
InputStream socketIn=socket.getInputStream();
return new BufferedReader(
new InputStreamReader(socketIn));
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
public String echo(String msg){
return "echo:"+msg;
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
public void run(){
try {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
BufferedReader br=getReader(socket);
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
PrintWriter pw=getWriter(socket);
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
String msg=
null;
while((msg=br.readLine())!=
null){
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
System.out.println(msg);
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
pw.println(echo(msg));
if(msg.equals("bye"))
break;
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_5nz8.gif)
}
catch (IOException e) {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
e.printStackTrace();
![](http://static.oschina.net/uploads/img/201305/10113016_5nz8.gif)
}
finally{
try {
if(socket!=
null)
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
socket.close();
![](http://static.oschina.net/uploads/img/201305/10113016_5nz8.gif)
}
catch (IOException e) {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
e.printStackTrace();
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_z8YP.gif)
}
2.客户端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public
class MultiThreadClient
{
public static void main(String[] args) {
int numTasks = 10;
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
ExecutorService exec = Executors.newCachedThreadPool();
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
for (
int i = 0; i < numTasks; i++) {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
exec.execute(createTask(i));
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
// 定义一个简单的任务
private static Runnable createTask(
final int taskID) {
return new Runnable() {
private Socket socket =
null;
private int port=8821;
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
public void run() {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
System.out.println("Task " + taskID + ":start");
try {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
socket =
new Socket("localhost", port);
// 发送关闭命令
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
OutputStream socketOut = socket.getOutputStream();
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
socketOut.write("shutdown\r\n".getBytes());
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
// 接收服务器的反馈
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
BufferedReader br =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
String msg =
null;
while ((msg = br.readLine()) !=
null)
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
System.out.println(msg);
![](http://static.oschina.net/uploads/img/201305/10113016_5nz8.gif)
}
catch (IOException e) {
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
e.printStackTrace();
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_6YlP.gif)
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
};
![](http://static.oschina.net/uploads/img/201305/10113016_hAfj.gif)
}
![](http://static.oschina.net/uploads/img/201305/10113016_z8YP.gif)
}