Concept of Java thread Blocking

Source: Internet
Author: User
Tags socket connect


Causes of thread Blocking

The main reasons for thread blocking are as follows.

1. The Thread executes the Thread. sleep (int n) method. The Thread discards the CPU, sleeps for n milliseconds, and then resumes running.

2. The thread needs to execute a synchronization code. Because it cannot obtain the relevant synchronization lock, it has to enter the blocking status and wait until the synchronization lock is obtained to resume running.

3. The thread executes the wait () method of an object and enters the blocking state. Only when other threads execute the notify () or yyall () method of the object, to wake it up.

4. When the thread executes I/O operations or performs remote communication, it will enter the blocking status because it waits for relevant resources. For example, when the thread executes System. in. when the read () method is used, if the user does not input data to the console, the thread will wait until the user's input data is read before returning the data from the read () method. During remote communication, the thread may be blocked in the following cases.

5. When the request establishes a connection with the server, that is, when the thread executes the Socket construction method with parameters or the Socket connect () method, it will enter the blocking status until the connection is successful, this thread returns from the Socket constructor or connect () method.

6. When the thread reads data from the input stream of the Socket, if there is not enough data, it will enter the blocking status until enough data is read or the end of the input stream is reached, or the read () method of the input stream is returned or an exception is interrupted when an exception occurs. How much data is enough in the input stream? This depends on the type of the read () method executed by the thread.

Bytes --------------------------------------------------------------------------------------------------------------------

Int read (); only one byte in the input stream is enough.


Int read (byte [] buff); it is sufficient if the number of bytes in the input stream is the same as the length of the parameter buff array.


String readLine (); it is sufficient to mail a String in the input stream. It is worth noting that the InputStream class does not have the readLine method. This method is available only in the BufferedReader class for filtering streams.


When a thread writes a batch of data to the Socket output stream, it may enter the blocking status. When all the data is output or an exception occurs, it will write () from the output stream () method return or exception interruption.


The setSoLinger () method of the Socket is called to set the delay time for closing the Socket. When the thread executes the close method of the Socket, it will be blocked until the underlying Socket sends all the remaining data, or if the delay time set by the setSoLinger () method is exceeded, it is returned from the close () method.


In a server program, threads may be blocked in the following situations.


The thread executes the ServerSocket accept () method and waits for the client's connection until it receives the client's connection before the accept () method returns.


When a thread reads data from the Socket input stream, if the input stream does not have enough data, it will enter the blocking status.

When a thread writes a batch of data to the Socket output stream, it may enter the blocking status. When all the data is output or an exception occurs, it will write () from the output stream () method return or exception interruption.


Server programs use multiple threads to handle blocking I/O. Although they can meet the needs of multiple clients at the same time, they have the following limitations:

(1) the Java Virtual Machine allocates an independent stack space for each thread. The larger the number of worker threads, the higher the system overhead, and the higher the burden on the scheduling thread of the Java Virtual Machine, increases the complexity of synchronization between threads and increases the possibility of thread deadlocks;

(2) The worker threads waste a lot of time on blocking I/O operations. the Java Virtual Machine needs to frequently transfer the CPU usage right so that the thread in the blocking status will give up the CPU, then, allocate the CPU to a running thread.




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.