Implementing multithreaded server programs in Java

Source: Internet
Author: User
Tags set time socket thread thread class port number

----Summary: Before the advent of Java, writing multithreaded programs is a cumbersome and fraught with many unsafe factors. With Java, it is easy to write secure and efficient multithreaded programs, and we can easily implement multi-threaded server program with multithreading and Java network package.

----Java is accompanied by the emergence of the Internet, the network and multithreading has the inherent support, with the network ERA programming language of all characteristics. From the current application of Java, Java is mainly used for network programming on the Internet or LAN, and the trend of Java as the mainstream network programming language is becoming more and more obvious. In practice, in addition to using commercially available server software, we often need to write our own server software to perform specific tasks or interact with specific client software in the actual environment. In the implementation of the server program, in order to improve the efficiency of the program and reduce the user wait time, we have applied in Java applets common multithreading technology.

----One, Java server programs and multithreading

----Before Java, there is no mainstream programming language that can provide intrinsic support for advanced network programming. In other locales, the implementation of network programs often requires deep reliance on the technology of the network APIs that operate the platform, while Java provides a complete package of platform-independent dependencies for network support, making it unnecessary for programmers to worry about the specifics of the system's network support.

The network protocols supported in----Java packages are TCP/IP and are the most popular WAN/LAN protocols today. Java-related classes and interfaces for the network are defined in the java.net package. The client software usually uses the core class socket in the java.net package to establish a connection to a port on the server, and the server program is different from the client, it needs to initialize a port for listening, and encounters a connection call to establish a connection with the corresponding client. The ServerSocket class of the java.net package contains everything you need to write a server system. A partial definition of the ServerSocket class is given below.

public class ServerSocket {
public ServerSocket(int port)
throws IOException ;
public Socket accept() throws IOException ;
public InetAddress getInetAddress() ;
public int getLocalPort() ;
public void close() throws IOException ;
public synchronized void setSoTimeout
(int timeout) throws SocketException ;
public synchronized int
getSoTimeout() throws IOException ;
}

----The ServerSocket constructor is the basis for running a server program that initializes the port specified by the parameter port as the port for that server and listens for client connection requests. Port ranges from 0 to 65536, but 0 to 1023 are standard Internet Protocol reserved ports, and these ports are available only to root users on UNIX hosts. The generic custom port number is between 8000 and 16000. Initializing ServerSocket is still not enough, it does not have a socket (socket) that interacts with the client, so the accept method of the class needs to be invoked to accept the customer call. The Accept () method returns an instance of the communication socket (socket) until a connection request has been received. Through the input and output stream of this instance, the server can receive the user instruction and respond the corresponding result to the client. The Getinetaddress and Getlocalport methods of the ServerSocket class can get the IP address and port of the server. The Setsotimeout and Getsotimeout methods are set and get the server timeout setting, and if the server does not get the socket instance returned by the Accept method within the Timout set time, throw the IOException exception.

----Java multithreading is one of the essence of Java programming, the use of appropriate can greatly improve the response time of the program, improve the parallelism of the program. In a server program, because you tend to receive simultaneous requests or commands from different clients, you can generate a command-processing thread for each client's request and respond to the user's instructions. In some of the more complex systems, we can also generate a separate thread for each database query instruction and operate the database in parallel. It has been proved that multithread design can improve the response of the system and guarantee the independence of user instruction execution. Because Java itself is "thread-safe," there is a programming principle that an operation that can be done independently of a thread should open up a new thread.

There are two ways to implement threads----Java, one is to generate a subclass of the thread class and define its own run method, which is implemented in method run. But the classes we define are generally subclasses of other classes, and Java does not allow multiple inheritance, so the second way to implement a thread is to implement the Runnable interface. The function of the thread is implemented by overwriting the Run method in the Runnable interface. This example uses the first method to implement the thread.

Related Article

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.