search

java interview-lock

Jan 04, 2021 pm 05:32 PM
javaLockinterview

java interview-lock

Interview question:

Do you know anything about locks in java? What kinds of locks are there? What's the difference between them? Tell me.

(Learning video sharing: java video tutorial)

Answer:

It can be roughly divided into the following points: what is a lock, what is it used for, what types of locks are there, and the differences between locks.

1:What is a lock and what is it used for?

Locks are mainly used to achieve synchronization of resource sharing. Only after acquiring the lock can you access the synchronization code, otherwise wait for other threads to finish using the lock to release the lock.

2:What kind of locks are there?

The main focus here is: synchronize and Lock (because the interview is asking about these two).

synchronize: It can be placed in front of the method; it can also be placed in front of the code block, but the locked object needs to be specified. Usually used together with wait, notify, notifyAll. wait: Release the object lock held and release the CPU. sleep: releases the CPU, but does not release the occupied object lock.

notify: Wake up a thread in the waiting queue so that it can obtain the lock for access.

notifyAll: Wake up all threads waiting for the object lock in the waiting queue and let them compete to obtain the lock.

Lock: Has the same semantics as synchronize, but adds some other features, such as Interrupt lock waiting and timed lock waiting, So you can use lock instead of synchronize. The methods provided are:

(Recommendations for more related interview questions: java interview questions and answers)

lock(): Obtain the lock in a blocking manner. It will wait until it is not acquired and will not be interrupted.

tryLock(): Get it. If it is obtained, it will return true. If it is not obtained, it will return false.

tryLock(long timeout,TimeUnit unit): Returns true if obtained, waits for the given time if not obtained, and has not been obtained yet Return false when it arrives.

##lockInterruptibly(): Similar to lock, but if the lock is not acquired, it will enter the dormant state until the lock is acquired or the current thread is blocked. The thread is interrupted.

Three:What is the difference between the two?

Inconsistent performance: When resource competition is motivated, lock performance will be better than synchronize. When competition is not motivated, synchronize performance will be better than lock.

The lock mechanism is different: synchronize is implemented at the JVM level, and the system will monitor whether the lock is released or not. The lock is implemented in code and needs to be released manually, in the finally block. Locks can be acquired in a non-blocking manner.

The usage is different: synchronize can be used on code blocks and methods. Lock is implemented through code and has more precise thread semantics.

Related recommendations: java introductory tutorial

The above is the detailed content of java interview-lock. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use