Executorservice to implement multi-thread Thread Pool

Source: Internet
Author: User

The Java 5 executorservice is used to implement multi-threading in the thread pool mode, simulating that multiple users on the client send requests to the same server.

1. Server

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;
Private executorservice; // Thread Pool
Private Final int pool_size = 10; // the size of a single CPU Thread Pool

Public multithreadserver () throws ioexception {
Serversocket = new serversocket (port );
// The availableprocessor () method of runtime returns the number of CPUs of the current system.
Executorservice = executors. newfixedthreadpool (runtime. getruntime (). availableprocessors () * pool_size );
System. Out. println ("server startup ");
}

Public void Service (){
While (true ){
Socket socket = NULL;
Try {
// Receive the customer connection. Once the customer connects, accept () is triggered to establish the connection.
Socket = serversocket. Accept ();
Executorservice.exe cute (new handler (socket ));

} Catch (exception e ){
E. printstacktrace ();
}
}
}

Public static void main (string [] ARGs) throws ioexception {
New multithreadserver (). 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 ();
}
}
}
}
2. Client
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;

Executorservice exec = executors. newcachedthreadpool ();

For (INT I = 0; I <numtasks; I ++ ){
Exec.exe cute (createtask (I ));
}

}

// Define a simple task
Private Static runnable createtask (final int taskid ){
Return new runnable (){
Private Socket socket = NULL;
Private int Port = 8821;

Public void run (){
System. Out. println ("task" + taskid + ": Start ");
Try {
Socket = new socket ("localhost", Port );
// Send the close command
Outputstream socketout = socket. getoutputstream ();
Socketout. Write ("shutdown/R/N". getbytes ());

// Receive server feedback
Bufferedreader BR = new bufferedreader (
New inputstreamreader (socket. getinputstream ()));
String MSG = NULL;
While (MSG = Br. Readline ())! = NULL)
System. Out. println (MSG );
} Catch (ioexception e ){
E. printstacktrace ();
}
}

};
}
}

This allows multiple clients to send requests to the server. The server uses multiple threads to process requests.

 

From: http://ecjtu05.blog.sohu.com/71741710.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.