The
Thread is blocked for some reason during the run, and the blocking state is characterized by the fact that the thread abandons the use of the CPU and suspends the operation until the cause of the blocking is eliminated. Or if it is interrupted by another thread, the thread exits the blocking state and throws interruptedexception.
There are many reasons for blocking, roughly divided into three kinds to discuss, usually in the thread of blocking, socket client blocking, socket server side blocking.
General thread blocking A, thread executes the Thread.Sleep () method, the current thread discards the CPU, sleeps for a period of time, and then resumes execution. B, the thread executes a synchronization code, but still cannot get the associated synchronization lock, can only enter the blocking state, wait until the synchronization lock is acquired to resume execution. C, the thread executes the wait () method of an object, goes directly into the blocking state, waits for another thread to execute the Notify () or Notifyall () method. D, the thread performs some IO operations because it waits for the associated resource to enter a blocking state, such as listening on system.in (), but does not receive input from the keyboard, and then goes into a blocking state. The socket client is blocking A, requesting a connection to the server, calling the Connect method and entering a blocking state until the connection succeeds.
B, when reading data from the socket input stream, enters a blocking state before reading enough data. For example, when using the ReadLine () method with the BufferedReader class, the amount of data is not enough to be blocked until a row of data has been read.
C, the Setsolinger () method that calls the socket closes the socket delay, and when the Close method of the socket is executed, it goes into a blocking state, knowing that the underlying socket is sending out all the remaining data
Socket Server blocking A, thread execution ServerSocket's accept () method, waiting for the client to connect, Knowing that a connection to a client is received, only a socket object is returned from the Accept method
B, when the data is read from the socket input stream, if the input stream does not have enough data, it enters the blocking state
C, the thread writes a batch of data to the output stream of the socket. May enter blocking status