Java 5 Multi-Threading programming practice

Source: Internet
Author: User

Java5 adds a new class library concurrency set Java.util.concurrent, which provides rich APIs for concurrent programs that are easier and more flexible in Java 5. This article through a network server model, to practice JAVA5 multithreaded programming, the model used in the JAVA5 thread pool, blocking queues, can be reset locks, but also practice callable, future and other interfaces, and the use of Java 5, another new feature generics.

Brief introduction

This article will implement a network server model, once the client is connected to the server, start a new thread for the connection service, the service content to send some character information to the client. A typical network server model is as follows:

1. Establish the listening port.

2. Found a new connection, accept the connection, start the thread, execute the service thread. 3. Service complete, close the thread.

This model works well in most cases, but it takes a lot of time to process the user's request and the service is short for each request, and the system will spend a great deal of it on the creation and destruction of the threads. The Java 5 thread pool overcomes these drawbacks. By performing multiple tasks on a reusable thread, the cost of creating and destroying frequent threads is avoided and the performance of the server is greatly improved. Therefore, the network server model for this article will be as follows:

1. Set up the listening port and create the thread pool.

2. New connections are found, using the thread pool to perform service tasks.

3. Service complete, release thread to thread pool.

The following details how to implement this server using the APIs provided by the Java 5 concurrent package.

Class

Initialization involves creating a thread pool and initializing the listening port. Creating a thread pool can be created by calling the static method Newchahedthreadpool or Newfixedthreadpool in the Java.util.concurrent.Executors class or by creating a new Java.util.concurrent.ThreadPoolExecutor the instance to perform the task. Here we use the Newfixedthreadpool method to build the thread pool.

 ExecutorService pool = Executors.newFixedThreadPool (10);

Represents a new thread pool with 10 threads in the thread pool serving task queues.

Use the ServerSocket object to initialize the listening port.

private static final int PORT = 19527;

serverListenSocket = new ServerSocket(PORT);

serverListenSocket.setReuseAddress(true);

serverListenSocket.setReuseAddress(true);

Service New Connection

When a new connection is established, the service task is submitted to the thread pool execution when accept returns.

while(true){

Socket socket = serverListenSocket.accept();

pool.execute(new ServiceThread(socket));

}

This uses thread pool objects to execute threads, reducing the overhead of each thread creation and destruction. The thread is freed to the thread pool when the task finishes executing.

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.