search
HomeJavajavaTutorialJava thread status BLOCKED

Java thread status BLOCKED

Nov 22, 2016 pm 04:20 PM
java

Definition of BLOCKED state

As mentioned before, the simple definition of BLOCKED is:

A thread that is blocked waiting for a monitor lock is in this state. (A thread that is blocked waiting for a monitor lock is in this state.)

For a more detailed definition, please refer to the javadoc in Thread.State:

/**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

This sentence is very long and can be broken into two simple sentences to understand .

A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method.

A thread in blocked state is waiting for a monitor lock to enter a synchronized block or method.

A thread in the blocked state is waiting for a monitor lock to reenter a synchronized block/method after calling Object.wait.

A thread in the blocked state is waiting for a monitor lock after it calls the Object.wait method to re-enter a synchronized block or method.

Blocked when entering (enter) synchronization block

Let me talk about the first sentence first, this is easier to understand.

Monitor lock is used for synchronous access to achieve mutual exclusion between multiple threads. So once a thread acquires the lock and enters the synchronized block, before it comes out, if other threads want to enter, they will be blocked outside the synchronized block because they cannot obtain the lock. At this time, the state is BLOCKED.

Note: The entry and exit of this state are not under our control. When the lock is available, the thread will recover from the blocking state.

We can use some code to demonstrate this process:

@Test
public void testBlocked() throws Exception {
    class Counter {
        int counter;
        public synchronized void increase() {
            counter++;
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    Counter c = new Counter();
    
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            c.increase();
        }
    }, "t1线程");
    t1.start();
    
    Thread t2 = new Thread(new Runnable() {
        public void run() {
            c.increase();
        }
    }, "t2线程");
    t2.start();
    
    Thread.sleep(100); // 确保 t2 run已经得到执行
    assertThat(t2.getState()).isEqualTo(Thread.State.BLOCKED);
}

The above defines an access counter counter with a synchronous increase method. The t1 thread enters first and then sleeps in the synchronization block, causing the lock to be delayed. When t2 tries to execute the synchronization method, it is blocked because it cannot obtain the lock.

VisualVM monitoring shows the status of the t2 thread:

Java thread status BLOCKED

The "monitor" status on the picture is the BLOCKED status. You can see that t2 is in the BLOCKED state during t1 sleep.

BLOCKED state can be regarded as a special kind of WAITING, specifically waiting for a lock.

wait blocks when reentering the synchronized block

Now look at the second sentence again:

2. A thread in the blocked state is waiting for a monitor lock to reenter a synchronized block/method after calling Object. wait.

A thread in the blocked state is waiting for a monitor lock after it calls the Object.wait method to re-enter a synchronized block or method.

This sentence is a bit convoluted, and it is not easy to translate into a concise Chinese sentence. It is not easy to understand this sentence without a good understanding of the relevant background of wait. Let’s expand on it a little bit here. Since it is reenter, it means there are two enters. The process is like this:

Call the wait method must be in a synchronized block, that is, you must first acquire the lock and enter the synchronized block. This is the first enter.

After calling wait, the lock will be released and entered into the waiting queue (wait set) of this lock.

After receiving the notify or notifyAll notification from other threads, the waiting thread cannot resume execution immediately because the stop is in the synchronized block and the lock has been released, so it must reacquire the lock before reentering (reenter) Synchronize the block and resume execution from the last wait point. This is the second time to enter, so it is called reenter.

But the lock will not be given to it first. The thread still has to compete with other threads for the lock. This process is actually the same as the enter process, so it may also cause BLOCKED because the lock has been occupied by other threads.

This process is called reenter a synchronized block/method after calling Object.wait.

We also use a piece of code to demonstrate this process:

@Test
public void testReenterBlocked() throws Exception {
    class Account {
        int amount = 100; // 账户初始100元
        public synchronized void deposit(int cash) { // 存钱
            amount += cash;
            notify();
            try {
                Thread.sleep(30000); // 通知后却暂时不退出
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        public synchronized void withdraw(int cash) { // 取钱
            while (cash > amount) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            amount -= cash;
        }
    }
    Account account = new Account();
    
    Thread withdrawThread = new Thread(new Runnable() {
        public void run() {
            account.withdraw(200);
        }
    }, "取钱线程");
    withdrawThread.start();
    
    Thread.sleep(100); // 确保取钱线程已经得到执行
    
    assertThat(withdrawThread.getState()).isEqualTo(Thread.State.WAITING);
    
    Thread depositThread = new Thread(new Runnable() {
        public void run() {
            account.deposit(100);
        }
    }, "存钱线程");
    Thread.sleep(10000); // 让取钱线程等待一段时间
    depositThread.start();

    Thread.sleep(300); // 确保取钱线程已经被存钱线程所通知到

    assertThat(withdrawThread.getState()).isEqualTo(Thread.State.BLOCKED);
}

Brief introduction to the above code scenario:

There is an account object with deposit and withdraw methods, and the initial amount is 100 yuan.

The money withdrawal thread starts first and enters the (enter) synchronization block. It tries to withdraw 200 yuan. When it is found that the money is not enough, wait is called, the lock is released, and the thread hangs (WAITING state).

The deposit thread starts after 10 seconds, deposits money and notifies the withdrawal thread, but then continues to sleep in the synchronization block, resulting in the lock not being released.

After receiving the notification, the money-withdrawing thread exits the WAITING state, but no longer holds the lock. When trying to re-enter the synchronization block to resume execution, the lock has not been released by the money-saving thread, so it is blocked (BLOCKED state) ).

Monitored display:

Java thread status BLOCKED

As shown in the figure, the money withdrawal thread is WAITING first, and is blocked (BLOCKED) after receiving the notification because it cannot obtain the lock.

Summary

Looking at these two sentences together, they have two meanings, but they still mean the same thing. Simply put, it is enter, reenter, or enter. In summary:

When synchronization cannot be entered because the lock cannot be obtained block, the thread is in the BLOCKED state.

If a thread is in the BLOCKED state for a long time, consider whether a deadlock has occurred.

BLOCKED state can be regarded as a special kind of waiting, which is a subdivision of the traditional waiting state:

Java thread status BLOCKED

Since the WAITING state has not been mentioned yet, and the wait method is involved here, the wait is also slightly discussed above. After doing some analysis, in the next chapter, the two states WAITING and TIMED_WAITING will be analyzed in more detail.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java Platform Independence: What does it mean for developers?Java Platform Independence: What does it mean for developers?May 08, 2025 am 12:27 AM

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

How to set up JVM for first usage?How to set up JVM for first usage?May 08, 2025 am 12:21 AM

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

How can I check Java platform independence for my product?How can I check Java platform independence for my product?May 08, 2025 am 12:12 AM

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

Java Features for Modern Development: A Practical OverviewJava Features for Modern Development: A Practical OverviewMay 08, 2025 am 12:12 AM

Javastandsoutinmoderndevelopmentduetoitsrobustfeatureslikelambdaexpressions,streams,andenhancedconcurrencysupport.1)Lambdaexpressionssimplifyfunctionalprogramming,makingcodemoreconciseandreadable.2)Streamsenableefficientdataprocessingwithoperationsli

Mastering Java: Understanding Its Core Features and CapabilitiesMastering Java: Understanding Its Core Features and CapabilitiesMay 07, 2025 pm 06:49 PM

The core features of Java include platform independence, object-oriented design and a rich standard library. 1) Object-oriented design makes the code more flexible and maintainable through polymorphic features. 2) The garbage collection mechanism liberates the memory management burden of developers, but it needs to be optimized to avoid performance problems. 3) The standard library provides powerful tools from collections to networks, but data structures should be selected carefully to keep the code concise.

Can Java be run everywhere?Can Java be run everywhere?May 07, 2025 pm 06:41 PM

Yes,Javacanruneverywhereduetoits"WriteOnce,RunAnywhere"philosophy.1)Javacodeiscompiledintoplatform-independentbytecode.2)TheJavaVirtualMachine(JVM)interpretsorcompilesthisbytecodeintomachine-specificinstructionsatruntime,allowingthesameJava

What is the difference between JDK and JVM?What is the difference between JDK and JVM?May 07, 2025 pm 05:21 PM

JDKincludestoolsfordevelopingandcompilingJavacode,whileJVMrunsthecompiledbytecode.1)JDKcontainsJRE,compiler,andutilities.2)JVMmanagesbytecodeexecutionandsupports"writeonce,runanywhere."3)UseJDKfordevelopmentandJREforrunningapplications.

Java features: a quick guideJava features: a quick guideMay 07, 2025 pm 05:17 PM

Key features of Java include: 1) object-oriented design, 2) platform independence, 3) garbage collection mechanism, 4) rich libraries and frameworks, 5) concurrency support, 6) exception handling, 7) continuous evolution. These features of Java make it a powerful tool for developing efficient and maintainable software.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.