多线程 - synchronized修饰实例方法、修饰静态方法、修饰代码块

太过爱你忘了你带给我的痛 2024-03-22 12:13 171阅读 0赞

static synchronized 和 synchronized 修饰普通方法的区别在于:
static synchronized 是在类级别上获取锁,而 synchronized 是在对象级别上获取锁。

在 static synchronized 方法中,获取的是类级别的锁,不同的对象共享该锁,
只有当一个线程持有该类的锁时,才能够调用该类的其他 static synchronized 方法。
而在 synchronized 方法中,获取的是对象级别的锁,每个对象都有自己的锁,不同的对象之间互不影响。

对于 synchronized 代码块和方法的区别,主要在于锁的粒度不同。
synchronized 代码块可以在代码块中只锁定需要同步的部分,而 synchronized 方法会锁定整个方法。
因此,如果一个类中有多个同步方法,使用 synchronized 代码块可以提高并发性能,减少锁的竞争。

修饰实例方法

  1. package com.lfsun.highconcurrency000.mysynchronized;
  2. /**
  3. * 修饰实例方法
  4. */
  5. public class MySynchronizedDemo1 {
  6. // 实例变量
  7. private int count = 0;
  8. // synchronized 修饰实例方法
  9. public synchronized void increment() {
  10. count++;
  11. }
  12. public static void main(String[] args) throws InterruptedException {
  13. MySynchronizedDemo1 demo = new MySynchronizedDemo1();
  14. // 创建两个线程
  15. Thread thread1 = new Thread(() -> {
  16. for (int i = 0; i < 10000; i++) {
  17. demo.increment();
  18. }
  19. });
  20. Thread thread2 = new Thread(() -> {
  21. for (int i = 0; i < 10000; i++) {
  22. demo.increment();
  23. }
  24. });
  25. thread1.start();
  26. thread2.start();
  27. // 等待两个线程执行完成
  28. thread1.join();
  29. thread2.join();
  30. // 输出计数器的值
  31. System.out.println("Count: " + demo.count);
  32. }
  33. }

修饰静态方法

  1. package com.lfsun.highconcurrency000.mysynchronized;
  2. /**
  3. * 修饰静态方法
  4. */
  5. public class MySynchronizedDemo2 {
  6. // 静态变量
  7. private static int count = 0;
  8. // synchronized 修饰静态方法
  9. public static synchronized void increment() {
  10. count++;
  11. }
  12. public static void main(String[] args) throws InterruptedException {
  13. // 创建两个线程
  14. Thread thread1 = new Thread(() -> {
  15. for (int i = 0; i < 10000; i++) {
  16. MySynchronizedDemo2.increment();
  17. }
  18. });
  19. Thread thread2 = new Thread(() -> {
  20. for (int i = 0; i < 10000; i++) {
  21. MySynchronizedDemo2.increment();
  22. }
  23. });
  24. thread1.start();
  25. thread2.start();
  26. // 等待两个线程执行完成
  27. thread1.join();
  28. thread2.join();
  29. // 输出计数器的值
  30. System.out.println("Count: " + MySynchronizedDemo2.count);
  31. }
  32. }

修饰代码块

  1. package com.lfsun.highconcurrency000.mysynchronized;
  2. /**
  3. * 修饰代码块
  4. */
  5. public class MySynchronizedDemo3 {
  6. // 实例变量
  7. private int count = 0;
  8. public void increment() {
  9. // synchronized 修饰代码块
  10. synchronized (this) {
  11. count++;
  12. }
  13. }
  14. public static void main(String[] args) throws InterruptedException {
  15. MySynchronizedDemo3 demo = new MySynchronizedDemo3();
  16. // 创建两个线程
  17. Thread thread1 = new Thread(() -> {
  18. for (int i = 0; i < 10000; i++) {
  19. demo.increment();
  20. }
  21. });
  22. Thread thread2 = new Thread(() -> {
  23. for (int i = 0; i < 10000; i++) {
  24. demo.increment();
  25. }
  26. });
  27. thread1.start();
  28. thread2.start();
  29. // 等待两个线程执行完成
  30. thread1.join();
  31. thread2.join();
  32. // 输出计数器的值
  33. System.out.println("Count: " + demo.count);
  34. }
  35. }

发表评论

表情:
评论列表 (有 0 条评论,171人围观)

还没有评论,来说两句吧...

相关阅读