接上篇,本篇演示锁加在类上面

    1. public class T {
    2. private static Integer count = 0;
    3. public static void m() {
    4. //任何线程要执行下面的代码,必须先拿到当前对象的锁
    5. //锁的是当前对象
    6. synchronized (T.class) {
    7. for (int i = 0; i < 100000; i++) {
    8. count++;
    9. }
    10. }
    11. }
    12. public static void main(String[] args) throws InterruptedException {
    13. // 采用 CountDownLatch 保证了所有线程同时开始,同时结束
    14. CountDownLatch start = new CountDownLatch(1);
    15. CountDownLatch end = new CountDownLatch(10);
    16. T t = new T();
    17. for (int i = 0; i < 10; i++) {
    18. new Thread(() -> {
    19. try {
    20. start.await();
    21. m();
    22. } catch (InterruptedException e) {
    23. e.printStackTrace();
    24. } finally {
    25. end.countDown();
    26. }
    27. }).start();
    28. }
    29. start.countDown();
    30. end.await();
    31. System.out.println(Thread.currentThread().getName() + " count = " + count);
    32. }
    33. }
    34. 输出结果:
    35. main count = 1000000
    1. public class T {
    2. private static Integer count = 0;
    3. public synchronized static void m() {
    4. //任何线程要执行下面的代码,必须先拿到当前对象的锁
    5. //锁的是当前对象
    6. for (int i = 0; i < 100000; i++) {
    7. count++;
    8. }
    9. }
    10. public static void main(String[] args) throws InterruptedException {
    11. // 采用 CountDownLatch 保证了所有线程同时开始,同时结束
    12. CountDownLatch start = new CountDownLatch(1);
    13. CountDownLatch end = new CountDownLatch(10);
    14. T t = new T();
    15. for (int i = 0; i < 10; i++) {
    16. new Thread(() -> {
    17. try {
    18. start.await();
    19. m();
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. } finally {
    23. end.countDown();
    24. }
    25. }).start();
    26. }
    27. start.countDown();
    28. end.await();
    29. System.out.println(Thread.currentThread().getName() + " count = " + count);
    30. }
    31. }
    32. main count = 1000000

    如果要锁对象必须是静态的

    1. public class T {
    2. private static Integer count = 0;
    3. static Object o = new Object();
    4. public static void m() {
    5. //任何线程要执行下面的代码,必须先拿到当前对象的锁
    6. //锁的是当前对象
    7. synchronized (o) {
    8. for (int i = 0; i < 100000; i++) {
    9. count++;
    10. }
    11. }
    12. }
    13. public static void main(String[] args) throws InterruptedException {
    14. // 采用 CountDownLatch 保证了所有线程同时开始,同时结束
    15. CountDownLatch start = new CountDownLatch(1);
    16. CountDownLatch end = new CountDownLatch(10);
    17. T t = new T();
    18. for (int i = 0; i < 10; i++) {
    19. new Thread(() -> {
    20. try {
    21. start.await();
    22. m();
    23. } catch (InterruptedException e) {
    24. e.printStackTrace();
    25. } finally {
    26. end.countDown();
    27. }
    28. }).start();
    29. }
    30. start.countDown();
    31. end.await();
    32. System.out.println(Thread.currentThread().getName() + " count = " + count);
    33. }
    34. }
    35. main count = 1000000

    错误例子,不可以直接锁this

    1. public class T {
    2. private static Integer count = 0;
    3. public static void m() {
    4. //任何线程要执行下面的代码,必须先拿到当前对象的锁
    5. //锁的是当前对象
    6. synchronized (this) {// 此处编译报错
    7. for (int i = 0; i < 100000; i++) {
    8. count++;
    9. }
    10. }
    11. }
    12. public static void main(String[] args) throws InterruptedException {
    13. // 采用 CountDownLatch 保证了所有线程同时开始,同时结束
    14. CountDownLatch start = new CountDownLatch(1);
    15. CountDownLatch end = new CountDownLatch(10);
    16. T t = new T();
    17. for (int i = 0; i < 10; i++) {
    18. new Thread(() -> {
    19. try {
    20. start.await();
    21. m();
    22. } catch (InterruptedException e) {
    23. e.printStackTrace();
    24. } finally {
    25. end.countDown();
    26. }
    27. }).start();
    28. }
    29. start.countDown();
    30. end.await();
    31. System.out.println(Thread.currentThread().getName() + " count = " + count);
    32. }
    33. }