In-depth analysis of threadlocal, network programming __ Programming

Source: Internet
Author: User

first, the understanding of threadlocal

ThreadLocal, many places are called thread-local variables, and some places are called thread-local storage, in fact, the meaning is similar. Probably a lot of friends know that Threadlocal creates a copy for a variable in each thread, so each thread can access its own internal replica variable. This sentence is literally easy to understand, but it's not so easy to really understand.

Threadlocal's official API is interpreted as:

"This class provides a thread-local (thread-local) variable. These variables are different from their normal counterparts because each thread that accesses a variable (through its Get or set method) has its own local variable, which is independent of the initialization copy of the variable. ThreadLocal instances are usually private static fields in a class that want to associate a state with a thread (for example, a user ID or a transaction ID). "

The approximate meaning has two points:

1. Threadlocal provides a special way to access a variable: The variable being accessed is the current thread that guarantees that each thread's variable is different, and that the same thread gets the same variable everywhere, which is called thread isolation.

2, if you want to use threadlocal, usually defined as private static type, in my opinion it is best to define the private static final type.

Threadlocal can be summed up as a sentence: the role of threadlocal is to provide local variables within the thread, which function in the life cycle of the threads, reducing the complexity of the delivery of some common variables between multiple functions or components within the same thread.

second, network programming

Network programming refers to writing programs that run on multiple devices (computers) that are connected through the network.

The J2SE API in the java.net package contains classes and interfaces that provide low-level communication details. You can use these classes and interfaces directly to focus on solving problems without having to focus on the details of the communication.

The java.net package provides support for two common network protocols:

TCP:TCP is the acronym for Transmission Control Protocol, which guarantees reliable communication between two applications. Usually used in Internet protocol, is called TCP/IP.

UDP:UDP is the abbreviation of the User Datagram Protocol, a connectionless protocol. A packet that provides data to be sent between applications.

This tutorial focuses on the following two topics.

Socket Programming: This is the most widely used network concept, and it has been interpreted in very detail.

URL Processing: This section will be in another space, click here to learn more about URL processing in the Java language. Socket Programming

A socket uses TCP to provide a communication mechanism between two computers. The client program creates a socket and attempts to connect to the server's sockets.

When the connection is established, the server creates a Socket object. The client and server can now communicate by writing and reading to the Socket object.

The Java.net.Socket class represents a socket, and the Java.net.ServerSocket class provides a mechanism for server programs to listen to and establish a connection to the client.

The following steps appear when using sockets to establish a TCP connection between two computers:

The server instantiates a ServerSocket object that represents communication over the port on the server.

The server invokes the accept () method of the ServerSocket class, which waits until the client connects to a given port on the server.

When the server is waiting, a client instantiates a Socket object, specifying the server name and port number to request a connection.

The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, creating a Socket object on the client can communicate with the server.

On the server side, the Accept () method returns a new socket reference to the server that connects to the client's socket.

After the connection is established, by using I/O flow in the communication, each socket has an output stream and an input stream, the client's output is linked to the server-side input stream, while the client's input to the server side of the output stream.

TCP is a two-way communication protocol, so data can be sent at the same time through two streams. The following are some of the complete and useful methods provided by the classes to implement the socket.

Reprint Link Reprint Link

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.