package com.mmall.concurrency.example.sync;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
@author ymy
*/
@Slf4j
public class SynchronizedExample1 {
// 修饰一个代码块
public void test1(int j) {
synchronized (this) {
for (int i = 0; i < 10; i++) {
log.info(“test1 {} - {}”, j, i);
}
}
}
// 修饰一个方法
public void test2(int j) {
for (int i = 0; i < 10; i++) {
log.info(“test2 {} - {}”, j, i);
}
}
public static void main(String[] args) {
SynchronizedExample1 example1 = new SynchronizedExample1();
SynchronizedExample1 example2 = new SynchronizedExample1();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> {
example1.test2(1);
});
executorService.execute(() -> {
example2.test2(2);
});
}
}
synchronized 关键字加与不加对程序运行有什么影响,影响是什么原因导致的,请问有详细说明吗,感觉就是把程序放上去,展示一下结果,没有详细说明synchronized 关键字加与不加对程序的影响。