Detailed introduction and example code of java thread lock
java thread lock
Use the synchronized keyword in Java threads to achieve synchronization
synchronized can lock methods, lock classes, lock objects, and lock code blocks
Method lock
// 加在方法上面的同步锁是this public synchronized void print() { System.out.println("同步方法"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } }
Class lock
public synchronized void print(String msg) { // 类锁 synchronized (MyThread.class) { System.out.println(msg); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
Object lock
Take selling train tickets as an example
public class Window extends Thread { public Window(String name) { super(name); } static int tick = 100; static String obj = new String(); @Override public void run() { // 开始卖票 while (tick > 0) { // 同步代码块 // 一把锁 钥匙 // 所有的线程 必须在这里排队 synchronized (obj) { if (tick > 0) { System.out.println(getName() + "卖出了第【" + tick + "】张票");// 失去了cpu资源 tick--; } } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Thank you for reading, I hope it can help everyone, thank you for your support of this site!
For more detailed introductions to java thread locks and articles related to example codes, please pay attention to 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











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
