File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed
Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change 8686
8787``` java
8888synchronized void method() {
89- // 业务代码
89+ // 业务代码
9090}
9191```
9292
9393** 2.修饰静态方法:** 也就是给当前类加锁,会作用于类的所有对象实例 ,进入同步代码前要获得 ** 当前 class 的锁** 。因为静态成员不属于任何一个实例对象,是类成员( _ static 表明这是该类的一个静态资源,不管 new 了多少个对象,只有一份_ )。所以,如果一个线程 A 调用一个实例对象的非静态 ` synchronized ` 方法,而线程 B 需要调用这个实例对象所属类的静态 ` synchronized ` 方法,是允许的,不会发生互斥现象,** 因为访问静态 ` synchronized ` 方法占用的锁是当前类的锁,而访问非静态 ` synchronized ` 方法占用的锁是当前实例对象锁** 。
9494
9595``` java
9696synchronized static void method() {
97- // 业务代码
97+ // 业务代码
9898}
9999```
100100
101101** 3.修饰代码块** :指定加锁对象,对给定对象/类加锁。` synchronized(this|object) ` 表示进入同步代码库前要获得** 给定对象的锁** 。` synchronized(类.class) ` 表示进入同步代码前要获得 ** 当前 class 的锁**
102102
103103``` java
104104synchronized (this ) {
105- // 业务代码
105+ // 业务代码
106106}
107107```
108108
@@ -755,7 +755,7 @@ pool-1-thread-1 End. Time = Tue Nov 12 20:59:54 CST 2019
755755
756756现在,我们就分析上面的输出内容来简单分析一下线程池原理。
757757
758- ** 为了搞懂线程池的原理,我们需要首先分析一下 ` execute ` 方法。** 在 4.6 节中的 Demo 中我们使用 ` executor.execute(worker) ` 来提交一个任务到线程池中去,这个方法非常重要,下面我们来看看它的源码:
758+ ** 为了搞懂线程池的原理,我们需要首先分析一下 ` execute ` 方法。** 在 4.6 节中的 Demo 中我们使用 ` executor.execute(worker) ` 来提交一个任务到线程池中去,这个方法非常重要,下面我们来看看它的源码:
759759
760760``` java
761761// 存放线程池的运行状态 (runState) 和线程池内有效线程的数量 (workerCount)
You can’t perform that action at this time.
0 commit comments