前言:
synchronized关键字主要代表对这个方法(代码块)进行加锁,运行到此看有没有加锁,有的话就等待~
有两种用法:synchronized同步方法,同步代码块,一般将当前对象this和类作为“对象监视器”来实现同步功能~
当然还可以将 非this对象作为对象监视器,那么在同一对象监视器的前提下,同一时间只有一个线程可以执行其synchronized的代码(当然用的比较少了)~
方法内的变量是线程安全的, 非线程安全问题只存在于实例变量中(对象安全会讲)~
一、对象监视器是当前对象this
@Slf4j
public class SynchronizedExample {
// 修饰一个代码块
public void test1(int j) {
synchronized (this) {
for (int i = 0; i < 10; i++) {
log.info("test1 {} - {}", j, i);
}
}
}
// 修饰一个方法
public synchronized void test2(int j) {
for (int i = 0; i < 10; i++) {
log.info("test2 {} - {}", j, i);
}
}
public static void main(String[] args) {
//不用线程池的话,本身就是顺序执行的,没法看到同步性
//一个对象就会执行同步,两个对象就不会,只会锁当前对象
SynchronizedExample example1 = new SynchronizedExample();
SynchronizedExample example2 = new SynchronizedExample();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> {
example1.test2(1);
});
executorService.execute(() -> {
example2.test2(2);
});
}
}
结果:由于是两个对象,所以并不存在锁的问题,可以交替打印出来~
二、对象监视器是类
@Slf4j
public class SynchronizedExample {
// 修饰一个类
public static void test1(int j) {
synchronized (SynchronizedExample.class) {
for (int i = 0; i < 10; i++) {
log.info("test1 {} - {}", j, i);
}
}
}
// 修饰一个静态方法
public static synchronized void test2(int j) {
for (int i = 0; i < 10; i++) {
log.info("test2 {} - {}", j, i);
}
}
public static void main(String[] args) {
SynchronizedExample example1 = new SynchronizedExample();
SynchronizedExample example2 = new SynchronizedExample();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> {
example1.test1(1);
});
executorService.execute(() -> {
example2.test1(2);
});
}
}
结果:由于都是同一个类,所以显示同步打印~
三、对象监视器是非this对象
@Slf4j
public class SynchronizedExample3 {
public void Mehtod(Object obj){
synchronized (obj){
try{
log.info("{} start time {}", Thread.currentThread().getName(), System.currentTimeMillis());
Thread.sleep(1000);
log.info("{} end time {}", Thread.currentThread().getName(),System.currentTimeMillis());
}catch (InterruptedException e){
log.info("exception", e);
}
}
}
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
SynchronizedExample3 syn = new SynchronizedExample3();
new Thread(() -> {
syn.Mehtod(obj1);
}).start();
new Thread(() -> {
syn.Mehtod(obj2);
}).start();
}
}
只要抓住一点,只有使用了同一个对象监视器才会有同步,否则就不会有同步效果,而class监视器,是对所有对象都起作用的~