企业即时通外文文献翻译(编辑修改稿)内容摘要:
trying to deal with the connections as they arrive. Here39。 s what it looks like, in the abstract: // start listening on the port ServerSocket ss = new ServerSocket( port )。 // loop forever while (true) { // get a connection Socket newSocket = ()。 // deal with the connection // ... } 5 The accept() routine is the key here. When this method of ServerSocket is called, it returns a new Socket object that represents a new connection that has e in. After you39。 ve dealt with this connection, you call accept() and get the next one. This way, no matter how fast connections are ing, and no matter how many processors or work interfaces your machine has, you get the connections one at a time. (And if there aren39。 t any connections ing in at the moment, then the accept () routine simply blocks doesn39。 t return until there are.) Serialization is a useful way, in general, to deal with things that are happening simultaneously. A potential drawback, however, is that it can remove parallelism? That is to say serialization can prevent us from saving time by working on multiple things at the same time. In the code above, while the program is dealing with one connection, other connection might be piling up. But serialization is not a problem for us because each time a connection es in, we39。 re going to create a new thread to deal with it. Once the new thread is created, it can go off and deal with the new connection, and our whileaccept loop can go back to accepting new connections. If the act of creating this new thread is fast enough, then the connections won39。 t pile up. The code Let39。 s take a look at the code that does all this. The code below takes care of the things we39。 ve been talking about: listening on a port, accepting new connections, and creating threads to deal with them. It also does a few other things that will be useful later. Let39。 s take a look: private void listen( int port ) throws IOException { // Create the ServerSocket ss = new ServerSocket( port )。 // Tell the world we39。 re ready to go ( Listening on +ss )。 // Keep accepting connections forever while (true) { // Grab the next ining connection Socket s = ()。 // Tell the world we39。 ve got it ( Connection from +s )。 // Create a DataOutputStream for writing data to the // other side DataOutputStream dout = new DataOutputStream( () )。 6 // Save this stream so we don39。 t need to make it again ( s, dout )。 // Create a new thread for this connection, and then forget // about it new ServerThread( this, s )。 } } The last line of this listing creates a thread to deal with the new connection. This thread is an object called a ServerThread, which is the topic of the next section. 5. PerThread class What is a thread? Two of the Java language39。 s main strengths are working and multithreading. That is not to say that other languages don39。 t support these functions they do. But the abstractions that the Java language uses to provide these features are particularly elegant, especially for a mercial language. A thread is generally defined as a separate line of control within a single process. What this really means is that a multithreaded program has multiple, semiautonomous activities going on inside of it at the same time. Multithreading is similar to the concepts of a task and multitasking, except that the multiple threads within a program all share the same data space. This makes it easier for them to share data directly and efficiently and it also makes it easier for them to mess each other up. Why use multithreading? A detailed discussion of threads is beyond the scope of this tutorial. There are a few reasons why you39。 d want to use threads in your program, but there is one reason most pertinent to the construction of a chat server: input/output. Your chat server is municating (in a sense) with the users at the client. Users are usually much slower than servers, which means that your server code is going to spend a lot of time simply waiting for users to say things. And you never know who is going to say something first. If you have a single thread, and it39。 s waiting for user 0 to say something, then it39。 s not going to know that users 1 through 10 are talking like crazy. For this reason, we39。 re going to create a thread for each user connected to the system. The advantage of multithreading is that when one thread is listening for a slow user to say something, it essentially goes to sleep 7 until something es in from that user. In the meantime, another thread can be receiving data from another user. In effect, multithreading allows us to respond as quickly as we can to each user. In Java programs, any class can be made into a thread by implementing the Runnable interface. Or you can simply subclass from the class . We have chosen the latter route, for no particular reason: public cla。企业即时通外文文献翻译(编辑修改稿)
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。