Java multi-threaded threadpoolexecutor receive and process data __java
Source: Internet
Author: User
1. General Internet projects, are involved in the processing of multiple data, this is more common but things, if it is but the thread to do the data processing, obviously performance is slow a lot, then there is no good way na.
Of course, this is the Java itself multithreaded mechanism corresponding to Java multithreading problem, there are a lot of demo to do the reference, in the JDK's java.util.concurrent package, provides a lot of available APIs, no longer class described ...
2. The main use is threadpoolexecutor: The common construction method is:
Threadpoolexecutor (int corepoolsize, int maximumpoolsize,
Long KeepAliveTime, Timeunit unit,
Blockingqueue<runnable> Workqueue,
Rejectedexecutionhandler handler)
Corepoolsize: Thread pool maintains minimum number of threads
Maximumpoolsize: Thread pool maintains maximum number of threads
KeepAliveTime: Thread pool maintains idle time allowed by threads
Unit: The units in which the thread pool maintains the idle time allowed by threads
Workqueue: Buffer queues used by the thread pool
Handler: thread pool processing policy for reject tasks
A task is added to the thread pool through the Execute (Runnable) method, and the task is a Runnable type object, and the task is performed by the Runnable type object's Run () method.
When a task is added to the thread pool through the Execute (Runnable) method:
If the number of threads in the thread pool is less than corepoolsize, a new thread is created to handle the added task even if the threads in the thread pool are idle.
If the number in the thread pool is equal to corepoolsize, but the buffer queue Workqueue is not full, the task is put into the buffer queue.
If the number of threads in the thread pool is greater than corepoolsize, the buffer queue Workqueue full, and the number of thread pools is less than maximumpoolsize, a new thread is built to handle the added task.
If the number of threads in the thread pool is greater than corepoolsize, the buffer queue is workqueue full, and the number in the thread pool equals maximumpoolsize, the task is handled through the policy specified by handler.
That is: the priority of the processing task is:
Core thread corepoolsize, Task queue workqueue, maximum thread maximumpoolsize, if all three are full, use handler to process the rejected task.
When the number of threads in the thread pool is greater than corepoolsize, the thread is terminated if a thread is idle longer than KeepAliveTime. This allows the thread pool to dynamically adjust the number of threads in the pool.
The optional parameters of the unit are several static properties in Java.util.concurrent.TimeUnit:
nanoseconds, microseconds, milliseconds, SECONDS.
Workqueue I used to be: java.util.concurrent.ArrayBlockingQueue
Handler has four options:
Threadpoolexecutor.abortpolicy ()
Throw Java.util.concurrent.RejectedExecutionException exception
Threadpoolexecutor.callerrunspolicy ()
Retry adding the current task, he will automatically repeatedly invoke the Execute () method
Threadpoolexecutor.discardoldestpolicy ()
Throw away the old task
Threadpoolexecutor.discardpolicy ()
Discard the current task
3. A recent project, the use of this note, to facilitate the discovery of problems and later to make changes. Others do not say, mainly look at the code:
public class Acceptserver implements Runnable {
Public final Logger Logger = Logger.getlogger (Acceptserver.class);
Service side, used to receive bytecode data sent from the C program.
Private ServerSocket server = null;
Thread pool
public static Threadpoolexecutor executor = null;
/**
* Initialization method, called at the start of the project
*/
public void Initrevc () {
try {
Executor = new Threadpoolexecutor (200,300,1,timeunit.seconds,new arrayblockingqueue<runnable> (300),
New Threadpoolexecutor.callerrunspolicy ()
);
Logger.debug ("Personaeserver start to Run");
Server = new ServerSocket (Integer.parseint (Getoperateport ("ServerPort", "25105"));
Acceptserver recv = new Acceptserver ();
Recv.setserver (server);
Thread thread = new Thread (recv);
Thread.Start ();
catch (IOException e) {
Logger.error (E.getmessage ());
catch (Exception ex) {
Logger.error (Ex.getmessage ());
}
}
public void Run () {
Socket client = null;
while (true) {
try {
Logger.debug ("Personaeserver befor server.accept ()");
Client = This.getserver (). Accept ();
Client.setsotimeout (60000*10);
Logger.debug ("Personaeserver after Server.accept ()");
The way in which the convection file is handled
Queryreader.parsebytereader (New Bufferedinputstream (Client.getinputstream ()), client);
Through this framework code, it can be used to handle the requests sent by the client on the server side and make corresponding corresponding.
Queryreader.parsebytereader (New Bufferedinputstream (Client.getinputstream ()), client); It mainly deals with the data and returns the corresponding success code.
4. Deployment Note points:
1. This method uses dynamically read IP and port to obtain the stream information of the client, so the IP and port in the Config.properties file should be dynamically configured;
2. Use spring's Ben project to initialize the thread into the beanfactory:
<bean class= "com. Acceptserver "init-method=" INITREVC ">
5. In the use of multithreading problems, you can according to their own habits, using the API provided under the Java.util.concurrent package to do the data, the business processing work.
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