Synchronized 理解
JMM关于Synchronized的两条规定:
1、线程解锁前,必须把共享变量的最新值刷新到主内存;
2、线程加锁时,将清空工作内存重共享变量的值,从而使用共享变量时需要从主内存重重新读取最新的值。
注意:加锁与解锁是同一把锁。如何理解?要么是锁的同一个对象,要么是锁的同一个类,接着往下看
锁对象
锁对象有以下两种种情况:Synchronized修饰普通方法,Synchronized作用于方法块
test1 和 test2 锁的对象是this,他们是互斥的 test3 和 test4 锁的对象str,他们是互斥的
package com.example.concurrent.example.sync;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class SynchronizedObjectExample1 {
private String str = new String();
/**
* 锁代码快--锁定当前对象,相当与this
*/
private void test1() {
log.info("Thread:{}, inout", Thread.currentThread().getName());
synchronized (this) {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test1 i:{}", Thread.currentThread().getName(), i);
}
}
}
/**
* 锁代码快--锁定当前对象,相当于锁的this
*/
private synchronized void test2() {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test2 i:{}", Thread.currentThread().getName(), i);
}
}
/**
* 锁代码快--锁定其他对象,会和test1或test2交叉执行
*/
private void test3() {
synchronized (str) {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test3 i:{}", Thread.currentThread().getName(), i);
}
}
}
/**
* 和test3锁同一个对象,不会和test3交叉执行
* 注意是同一个对象,不要新new出一个对象
*/
private void test4() {
synchronized (str) {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test4 i:{}", Thread.currentThread().getName(), i);
}
}
}
public static void main(String[] args) {
SynchronizedObjectExample1 synchronizedExample1 = new SynchronizedObjectExample1();
ExecutorService executorService = Executors.newCachedThreadPool();
// test1 和 test2 锁的对象是this
// test3 和 test4 锁的对象str
executorService.execute(() -> {
synchronizedExample1.test1();
});
executorService.execute(() -> {
synchronizedExample1.test2();
});
executorService.execute(() -> {
synchronizedExample1.test3();
});
executorService.execute(() -> {
synchronizedExample1.test4();
});
executorService.shutdown();
}
}
锁类
锁对象有以下两种种情况:Synchronized修饰静态方法、锁方法块的对象为xxx.class
如下代码:
test1、test2锁的是class,会发生竞争
test3不会和test1、test2发生竞争,因为锁对象不同
package com.example.concurrent.example.sync;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class SynchronizedClassExample2 {
/**
* 锁类
*/
private void test1() {
log.info("Thread:{}, inout", Thread.currentThread().getName());
synchronized (SynchronizedClassExample2.class) {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test1 i:{}", Thread.currentThread().getName(), i);
}
}
}
/**
* 锁静态方法,为类级锁
*/
private synchronized static void test2() {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test2 i:{}", Thread.currentThread().getName(), i);
}
}
private synchronized void test3() {
for (int i = 1; i <= 10; i++) {
log.info("Thread:{}, test3 i:{}", Thread.currentThread().getName(), i);
}
}
public static void main(String[] args) {
SynchronizedClassExample2 synchronizedExample1 = new SynchronizedClassExample2();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> {
synchronizedExample1.test1();
});
executorService.execute(() -> {
SynchronizedClassExample2.test2();
});
// test3不会和test1、test2发生竞争,因为锁对象不同
executorService.execute(() -> {
synchronizedExample1.test3();
});
executorService.shutdown();
}
}