Java Network Programming Surface questions

Source: Internet
Author: User
Tags modifier serialization

1. Synchronous, asynchronous, blocking, non-blocking during network programming?

Synchronous: The function call does not call the result until it has been obtained, and returns no results.
Async: The function call does not call the result and returns the status information until the result is not obtained.
Blocking: The current thread hangs before the function call does not get the result. Returned after the result has been obtained.
Non-blocking: The current thread does not suspend and returns the result immediately before the function call does not get the result.

2.Java How to implement non-blocking socket programming?

NiO effectively solves the problem of threading overhead for multi-threaded servers.

The primary purpose of using multithreading in NIO is not to allocate a separate service thread for each client request.

But through multithreading to make full use of the processing power of multiple CPUs and processing of waiting time, to improve service capabilities.

3. What is the serialization of Java (serialized)?

Simply to save the state of various objects in memory (that is, instance variables, not methods),

And you can read the saved object state again. Although you can save object states in a variety of ways, Java provides you with a mechanism that should be better than your own to preserve the state of objects, which is serialization.

4. What is the need for serialization? Serialization considerations, how do I implement Java serialization (serializable)?

· When you want to save the state of an object in memory in a file or in a database;

· When you want to use sockets to transfer objects on the network;

· When you want to transfer objects through RMI;

Serialization considerations

1, if the subclass implements the serializable interface and the parent class is not implemented, the parent class will not be serialized, but the parent class must have an parameterless constructor, or the invalidclassexception exception will be thrown.

2, static variables will not be serialized, that is the "dish" of the class, not the object. Serialization saves the state of an object, which is a non-static property, an instance variable. Class variables cannot be saved.

3. The transient keyword modifier variable can limit serialization. For properties that you do not need or should not save, you should add the transient modifier. The class of the object to serialize must be public.

4, whether the virtual machine allows deserialization, not only depends on the class path and function code consistency, a very important point is that the serialization ID of two classes is consistent, is the private static final long Serialversionuid = 1L.

5, Java serialization mechanism in order to save disk space, with a specific storage rules, when the file is written to the same object, it will no longer store the contents of the object, but only to store a copy of the reference. When deserializing, the reference relationship is resumed.

6, when serializing to the same file, such as the second modification of the same object property value is saved again, the virtual machine according to the reference relationship to know that there is already an identical object has been written to the file, so only the second write reference, so read, is the first time to save the object.

how many types of streams are there in 5.java? The JDK provides some abstract classes for inheritance for each type of stream, tell me what classes they are.

The stream provided by the JDK inherits four classes:

InputStream (byte input stream), OutputStream (byte output stream), Reader (character input stream), Writer (character output stream).

Sort by Flow:

Input stream: The stream from which the program can read data.
Output stream: The stream to which the program can write data.

By Data Transmission Unit classification:

BYTE stream: Processed in bytes (8-bit binary). Binary data primarily used to read and write images or sounds.
Character stream: Processed in characters (16-bit binary).
are implemented by means of a byte stream. Character stream is the byte stream is encapsulated, convenient operation. At the bottom, all input and output are in bytes.
The suffix is stream is a byte stream, and the prefix is Reader,writer is a character stream.

Classification by Function:

Node stream: A stream class that reads and writes from a specific place, such as a disk or a piece of memory area.
Filter stream: Use node stream as input or output. The filter stream is created using an existing input stream or an output stream connection.

6. Java SOCKET Programming, read the server several characters, and then write to the local display.

After the client sends a connection request to the server, it passively waits for the server to respond.

A typical TCP client is going through the following three steps:

1. Create a Socket instance: the constructor establishes a TCP connection to the specified remote host and port;
2, through the socket I/O flow and service-side communication;
3. Use the Close method of the socket class to close the connection.

 

The job of the server is to establish a communication terminal and passively wait for the client to connect.

The typical TCP server side performs the following two steps:

1. Create a ServerSocket instance and specify a local port to listen for TCP connection requests sent by the client on that port;
2. Repeated execution:
1) Call ServerSocket's Accept () method to get the client connection and create a socket instance from its return value;
2) Open a new thread for the returned socket instance and communicate with the client using the I/O stream of the returned socket instance;
3) After the communication is complete, use the close () method of the socket class to close the socket connection for the client.

How many handshakes does the 7.tcp/ip have at the time of connection? How many handshakes do I get when I release?

The process of establishing a TCP three-time handshake connection:

The release process for TCP four waves:

Java Network Programming Surface questions

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.