


Interviewer: Could you please tell me the differences between sleep(), wait(), join() and yield()
Let’s first introduce the concepts of lock pool and waiting pool.
First let’s look at the concept of lock pool
All threads that need to compete for synchronization locks will be placed in the lock pool. For example, the lock of the current object has been obtained by one of the threads, then other threads need to Waiting in this lock pool, when the previous thread releases the synchronization lock, the threads in the lock pool compete for the synchronization lock. When a thread obtains it, it will enter the ready queue to wait for CPU resource allocation.
Let’s take a look at the concept of waiting pool
When we call the wait() method, the thread will be placed in the waiting pool, and the threads waiting in the pool will not compete for synchronization locks. Only after calling notify() or notifyAll() will the threads in the waiting pool start to compete for the lock. notify() randomly selects a thread from the waiting pool and puts it into the lock pool, while notifyAll() puts all threads in the waiting pool Put it in the lock pool.
sleep is a static local method of the Thread class, and wait is a local method of Object.
The sleep method will not release the lock, but the wait will release it and add it to the waiting queue.
sleep就是把cpu的执行资格和执行权释放出去,不再运行此线程,当定时事件结束再取回cpu资源,参与cpu的调度,获取到cpu资源后就可以继续运行了,而如果sleep时该线程有锁,那么sleep不会释放这个锁,而是把锁带着进入了冻结状态,也就是说其它需要这个锁的线程根本不可能获取到这个锁。也就是说无法执行程序,如果在睡眠期间其它线程调用了这个线程的interrupt方法,那么这个线程也会抛出interruptexception异常返回,这点和wait是一样的。
The sleep method does not depend on the synchronizer synchronized, but wait needs to depend on the synchronized keyword.
sleep does not need to be awakened (exits blocking after sleeping), but wait does (it does not need to be interrupted by others at a specified time).
sleep is generally used for sleeping of the current thread, or polling and suspending operations, while wait is mostly used for communication between multiple threads.
sleep will give up CPU execution time and force context switching, but wait will not. After wait, there may still be a chance to compete for the lock again and continue execution.
yield() After execution, the thread directly enters the ready state and immediately releases the execution right of the cpu, but still retains the execution qualification of the cpu, so it is possible that the cpu will perform thread scheduling next time. This thread will obtain execution rights and continue execution.
(Learning video sharing: java video tutorial)
After join() is executed, the thread enters the blocking state, for example, calling join() of thread A in thread B, Then thread B will enter the blocking queue and instruct thread A to end or interrupt the thread.
public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("休眠sleep线程"); }); t1.start(); t1.join(); System.out.println("线程执行完成"); }
Original link: https://blog.csdn.net/lxn1023143182/article/details/114134498
Related recommendations: java interview questions and answers
The above is the detailed content of Interviewer: Could you please tell me the differences between sleep(), wait(), join() and yield(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Cross-domain is a scenario often encountered in development, and it is also an issue often discussed in interviews. Mastering common cross-domain solutions and the principles behind them can not only improve our development efficiency, but also perform better in interviews.

The Go framework is a set of components that extend Go's built-in libraries, providing pre-built functionality (such as web development and database operations). Popular Go frameworks include Gin (web development), GORM (database operations), and RESTful (API management). Middleware is an interceptor pattern in the HTTP request processing chain and is used to add functionality such as authentication or request logging without modifying the handler. Session management maintains session status by storing user data. You can use gorilla/sessions to manage sessions.

The JS singleton pattern is a commonly used design pattern that ensures that a class has only one instance. This mode is mainly used to manage global variables to avoid naming conflicts and repeated loading. It can also reduce memory usage and improve code maintainability and scalability.

How does JavaScript determine data type? This article will share with you 8 ways to use JS to determine data type, which can effectively help work and interviews. The interviewer smiled slightly after reading it.

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

The url module and querystring module are two very important URL processing modules. It is often used when developing node servers.

Translator | Planning by Zhu Xianzhong | Xu Jiecheng Unlike other software development tools that developers trust, AI tools have some unique risks in terms of training, construction, hosting, and usage. Since the release of ChatGPT at the end of 2022, the Internet has been filled with arguments supporting and doubting it in almost equal proportions. Whether you like it or not, AI is making its way into your development organization. Even if you don’t plan to develop an AI product or leverage an AI tool to write the code for you, it may still be integrated into the tools and platforms used to build, test, and run source code. AI tools present some unique risks that may impact the productivity gains from automating tasks. These risks are mainly
