Blocking causes of threads

Source: Internet
Author: User
Tags socket

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


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.