java多线程读书笔记4 synchronized关键字

本文解析了Java中的`synchronized`关键字如何实现类似互斥锁的功能,介绍了锁的作用范围,方法级和代码块锁的区别,并讨论了线程同步的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以上内容均来自于本书和我的自己理解的总结

Java Threads, 3rd Edition [Book] (oreilly.com)

synchronized

The Java specification provides certain mechanisms that deal specifically with this problem. The Java language provides the synchronized keyword; in comparison with other threading systems, this keyword allows the programmer access to a resource that is very similar to a mutex lock. For our purposes, it simply prevents two or more threads from calling the methods of the same object at the same time 

Java规范提供了特定的机制来专门处理这个问题。Java语言提供了synchronized关键字;与其他线程系统相比,该关键字允许程序员访问非常类似于互斥锁的资源。出于我们的目的,它只是防止两个或多个线程同时调用同一对象的方法 

Mutex Lock互斥锁

A mutex lock is also known as a mutually exclusive lock. This type of lock is provided by many threading systems as a means of synchronization. Only one thread can grab a mutex at a time: if two threads try to grab a mutex, only one succeeds. The other thread has to wait until the first thread releases the lock before it can grab the lock and continue operation.

In Java, every object has an associated lock. When a method is declared synchronized, the executing thread must grab the lock associated with the object before it can continue. Upon completion of the method, the lock is automatically released.

互斥锁也称为互斥锁。这种类型的锁是由许多线程系统作为同步手段提供的。一次只有一个线程可以获取互斥体:如果两个线程试图获取互斥体,则只有一个线程成功。另一个线程必须等到第一个线程释放锁后才能抓住锁并继续操作。

在Java中,每个对象都有一个关联的锁。当一个方法被声明为synchronized时,执行线程必须先获取与该对象关联的锁,然后才能继续。方法完成后,锁会自动释放。

 If  thread1 wants to call synchronized method. while another thread2 is already executing it, the  thread1 must wait: the  thread1 gets to complete execution of its method before the  thread2 can execute its method. Since only one thread gets to call the synchronized method at a time, only one thread at a time accesses the data

如果thread1想要调用synchronized方法。当另一个thread2已经在执行它时,thread1必须等待:thread1在thread2可以执行其方法之前完成其方法的执行。因为一次只有一个线程可以调用synchronized方法,所以一次只有一个线程访问数据 

Under the covers, the concept of synchronization is simple: when a method is declared synchronized, the thread that wants to execute the method must acquire a token, which we call a lock. Once the method has acquired (or checked out or grabbed) this lock, it executes the method and releases (or returns) the lock. No matter how the method returns—including via an exception—the lock is released. There is only one lock per object, so if two separate threads try to call synchronized methods of the same object, only one can execute the method immediately; the other has to wait until the first thread releases the lock before it can execute the method. 

实际上,同步的概念很简单:当一个方法被声明为已同步时,想要执行该方法的线程必须获取一个令牌,我们称之为锁。一旦该方法获得(或签出或抓取)该锁,它就会执行该方法并释放(或返回)锁。无论该方法如何返回,包括通过异常返回,锁都会被释放。每个对象只有一个锁,因此如果两个单独的线程试图调用同一对象的同步方法,则只有一个线程可以立即执行该方法;另一个线程必须等到第一个线程释放锁后才能执行该方法。 

Scope of a Lock(锁的范围)

The scope of a lock is defined as the period of time between when the lock is grabbed and released. we have used only synchronized methods; this means that the scope of these locks is the period of time it takes to execute the methods. This is referred to as method scope.

we'll examine locks that apply to any block of code inside a method or that can be explicitly grabbed and released; these locks have a different scope. We'll examine this concept of scope as locks of various types are introduced.

锁的范围定义为抓取锁和释放锁之间的时间段。我们只使用了同步方法;这意味着这些锁的作用域是执行方法所需的时间。这被称为方法范围。

我们将研究应用于方法内任何代码块的锁,或者可以显式地抓取和释放的锁;这些锁的作用域不同。随着各种类型锁的引入,我们将研究范围的这个概念。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值