Simple Chat Program Tutorial (iii) Multithreading of servers

Source: Internet
Author: User

Source code Download Link: http://download.csdn.net/detail/sky453589103/9514686

If you have any questions, please leave a message.
If a server can only provide servers for two users, it is really too slag. Most of the time, chat customers will be more than two. Therefore, you need to increase the concurrency.
It is convenient to use multithreading in Java. I used this server program to implement the Runnable interface method to define my own multithreaded operations. You can implement your own multithreaded operations by overriding the Run method.
Each thread will have a client member variable, which is used to record the socket for clients. Then, each thread will use the same server. Server multi-threading, based on the idea that if a new customer landed, creating a new thread for the client, the thread would listen to the information from the customer in a blocking way. Once the information is obtained, the Requestmessage class is used to parse the information. Then, depending on the information, execute the corresponding interface in the server. Such as:

Obviously, server resources are critical resources, so you need to add mutually exclusive conditions to the use of critical resources. The use of the Synchronized keyword can be very convenient to meet our needs.
A simple introduction to this keyword:when it is used to decorate a method or a block of code, it is guaranteed that at most one thread at a time executes the code. Popularly speaking, is to use synchronized to lock.
Note that the return value of the logout interface in the server's interface is always null, and if response is null, the thread will not send anything to the client. in Simplechatserver, the call to accept function is blocked to listen for a new connection establishment, and if there is a Serverthread class for the connection, perform the corresponding task. Create an action that creates a thread for each connection in the STARTN function, the meaning of this function is the meaning of the start. Once this server is started, the above actions will be executed.
public void Startn () {while (true) {try {Socket s = serversocket.accept (); Thread t = new Thread (new Serverthread (S, this)); T.start ();} catch (IOException e) {}}}

Because the server should be the daemon, it will always loop.

Simple Chat Program Tutorial (iii) Multithreading of servers

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.