public class Main {
public synchronized void test(){
}
}
等价于
public class Main {
public void test(){
synchronized(this){
}
}
}
public class Main {
public static synchronized void test(){
}
}
等价于
public class Main {
public static void test(){
synchronized (Main.class){
}
}
}
结论:
- 非静态同步方法锁住的是实例对象本身
- 静态同步方法锁住的是类对象本身
线程八锁------>考察的就是锁住哪个对象
@Slf4j(topic = "c.Number")
public class Number {
public synchronized void a() {
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
new Thread(()->n1.a(),"t1").start();
new Thread(()->n1.b(),"t2").start();
}
}
线程t1和线程t2锁住的是同一个实例对象n1,有竞争
情况:ab或者ba
@Slf4j(topic = "c.Number")
public class Number {
public synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n1.b(),"t2").start();
}
}
线程t1和线程t2锁住的是同一个实例对象n1,有竞争
情况:1s后ab或者 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
public synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public void c(){
log.debug("c");
}
public static void main(String[] args) {
Number n1=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n1.b(),"t2").start();
new Thread(()->n1.c(),"t3").start();
}
}
线程t3没有去争抢锁
情况:c 1s 后ab 或者 bc 1s 后 a 或者 cb 1s后a
@Slf4j(topic = "c.Number")
public class Number {
public synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
Number n2=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n2.b(),"t2").start();
}
}
线程t1和线程t2锁住的是不同的实例对象,没有竞争
情况 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
public static synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n1.b(),"t2").start();
}
}
线程t1锁住的是类对象本身,线程t2锁住的是n1实例对象本身,没有竞争
情况: b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
public static synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public static synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n1.b(),"t2").start();
}
}
线程t1和线程t2锁住的都是类对象本身,有竞争
情况: 1s 后 ab 或者 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
public static synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
Number n2=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n2.b(),"t2").start();
}
}
线程t1锁住的是类对象本身,线程t2锁住的是n2实例对象本身,没有竞争
情况 b 1s 后 a
@Slf4j(topic = "c.Number")
public class Number {
public static synchronized void a() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
log.debug("a");
}
public static synchronized void b(){
log.debug("b");
}
public static void main(String[] args) {
Number n1=new Number();
Number n2=new Number();
new Thread(()-> {
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->n2.b(),"t2").start();
}
}
线程t1和t2锁住的都是类对象本身,有竞争
情况: 1s后ab 或者 b 1s后 a
有误请指正
参考:黑马程序员全面深入学习Java并发编程,JUC并发编程全套教程.



