• 线程练习题


    有三个线程,分别只能打印A,B和C,要求按顺序打印ABC,打印10次

    输出示例:

    ABC

    ABC

    ABC

    ABC

    ABC

    ABC

    ABC

    ABC

    ABC

    ABC

    (1)、这种方法并不能达到题目要求,因为无法确认当线程A在打印完一次后释放锁并唤醒其他线程时,它会唤醒线程B和线程C中的哪一个。这就导致,线程的执行顺序可能是ACB,也可能是其他顺序。

    1. public class Demo25 {
    2. public static void main(String[] args) {
    3. final Object lock = new Object();
    4. Thread a = new Thread(() -> {
    5. for (int i = 0; i < 10; i++) {
    6. synchronized (lock) {
    7. System.out.print("A");
    8. lock.notifyAll();
    9. try {
    10. lock.wait();
    11. } catch (InterruptedException e) {
    12. Thread.currentThread().interrupt();
    13. }
    14. }
    15. }
    16. });
    17. Thread b = new Thread(() -> {
    18. for (int i = 0; i < 10; i++) {
    19. synchronized (lock) {
    20. System.out.print("B");
    21. lock.notifyAll();
    22. try {
    23. lock.wait();
    24. } catch (InterruptedException e) {
    25. Thread.currentThread().interrupt();
    26. }
    27. }
    28. }
    29. });
    30. Thread c = new Thread(() -> {
    31. for (int i = 0; i < 10; i++) {
    32. synchronized (lock) {
    33. System.out.println("C");
    34. lock.notifyAll();
    35. try {
    36. if (i < 9) {
    37. lock.wait();
    38. }
    39. } catch (InterruptedException e) {
    40. Thread.currentThread().interrupt();
    41. }
    42. }
    43. }
    44. });
    45. a.start();
    46. b.start();
    47. c.start();
    48. }
    49. }

    (2)、我们刚刚写的代码中的线程因为没有合适的条件判断从而不能够按相应的顺序打印字母,又因为没有终止条件,所以无法正常停止。

    现在改进以上错误,重新写一个实现方法:

    1. public class Thread_1{
    2. // 计数器
    3. // 定义一个单独的锁对象
    4. private static volatile int COUNTER = 0;
    5. private static Object lock = new Object();
    6. public static void main(String[] args) {
    7. // 创建三个线程,并指定线程名,每个线程名分别用A,B,C表示
    8. Thread t1 = new Thread(() -> {
    9. // 循环10次
    10. for (int i = 0; i < 10; i++) {
    11. // 执行的代码加锁
    12. synchronized (lock) {
    13. // 每次唤醒后都重新判断是否满足条件
    14. // 每条线程判断的条件不一样,注意线程t1,t2
    15. while (COUNTER % 3 != 0) {
    16. try {
    17. // 不满足输出条件时,主动等待并释放锁
    18. lock.wait();
    19. } catch (InterruptedException e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. // 满足输出条件,打印线程名,每条线程打印的内容不同
    24. System.out.print(Thread.currentThread().getName());
    25. // 累加计数
    26. COUNTER++;
    27. // 唤醒其他线程
    28. lock.notifyAll();
    29. }
    30. }
    31. }, "A");//线程被命名为 "A"
    32. Thread t2 = new Thread(() -> {
    33. for (int i = 0; i < 10; i++) {
    34. synchronized (lock) {
    35. while (COUNTER % 3 != 1) {
    36. try {
    37. lock.wait();
    38. } catch (InterruptedException e) {
    39. e.printStackTrace();
    40. }
    41. }
    42. System.out.print(Thread.currentThread().getName());
    43. COUNTER++;
    44. lock.notifyAll();
    45. }
    46. }
    47. }, "B");
    48. Thread t3 = new Thread(() -> {
    49. for (int i = 0; i < 10; i++) {
    50. synchronized (lock) {
    51. while (COUNTER % 3 != 2) {
    52. try {
    53. lock.wait();
    54. } catch (InterruptedException e) {
    55. e.printStackTrace();
    56. }
    57. }
    58. //换行打印
    59. System.out.println(Thread.currentThread().getName());
    60. COUNTER++;
    61. lock.notifyAll();
    62. }
    63. }
    64. }, "C");
    65. // 启动线程
    66. t1.start();
    67. t2.start();
    68. t3.start();
    69. }
    70. }

    代码 Thread_1 停下来的原因是每个线程都在循环中执行一定次数,并且使用了 lock.wait() 来等待条件满足。当满足条件后,线程会继续执行,否则会等待。

    具体来说,代码中的三个线程 t1、t2、t3 会依次执行,它们之间使用了 lock 对象来同步,确保每次只有一个线程可以进入临界区执行。每个线程都有自己的条件检查,例如 t1 检查 COUNTER % 3 是否等于 0,t2 检查 COUNTER % 3  是否等于 1,t3 检查 COUNTER % 3  是否等于 2。这些条件控制了线程的执行顺序,确保了按照 “ABCABCABC” 的顺序输出。

    当线程执行完一轮后,会通过 lock.wait() 主动释放锁并等待唤醒,然后其他线程会继续执行,直到满足条件后再次唤醒等待的线程。这种方式保证了线程的有序执行。

    总之,这个代码中的线程之间使用了条件判断和等待来控制执行顺序,因此能够正常停止。

    3、还有一种实现方法,就是使用三个锁, 分别控制。

    1. public class Demo26 {
    2. private static Object locker1 = new Object();
    3. private static Object locker2 = new Object();
    4. private static Object locker3 = new Object();
    5. public static void main(String[] args) throws InterruptedException {
    6. Thread t1 = new Thread(() -> {
    7. try {
    8. for (int i = 0; i < 10; i++) {
    9. synchronized (locker1) {
    10. locker1.wait();
    11. }
    12. System.out.print("A");
    13. synchronized (locker2) {
    14. locker2.notify();
    15. }
    16. }
    17. } catch (InterruptedException e) {
    18. e.printStackTrace();
    19. }
    20. });
    21. Thread t2 = new Thread(() -> {
    22. try {
    23. for (int i = 0; i < 10; i++) {
    24. synchronized (locker2) {
    25. locker2.wait();
    26. }
    27. System.out.print("B");
    28. synchronized (locker3) {
    29. locker3.notify();
    30. }
    31. }
    32. } catch (InterruptedException e) {
    33. e.printStackTrace();
    34. }
    35. });
    36. Thread t3 = new Thread(() -> {
    37. try {
    38. for (int i = 0; i < 10; i++) {
    39. synchronized (locker3) {
    40. locker3.wait();
    41. }
    42. //注意,只有C是println
    43. System.out.println("C");
    44. synchronized (locker1) {
    45. locker1.notify();
    46. }
    47. }
    48. } catch (InterruptedException e) {
    49. e.printStackTrace();
    50. }
    51. });
    52. t1.start();
    53. t2.start();
    54. t3.start();
    55. Thread.sleep(1000);
    56. // 从线程 t1 启动
    57. synchronized (locker1) {
    58. locker1.notify();
    59. }
    60. }
    61. }

     

    再来看道题吧,和上面的差不多。

    有三个线程,线程名称分别为:a,b,c。每个线程打印自己的名称。

    需要让他们同时启动,并按 c,b,a 的顺序打印。

    1. public class PrintOrderDemo {
    2. private static final Object lock = new Object();
    3. private static volatile int turn = 0;
    4. public static void main(String[] args) {
    5. Thread threadA = new Thread(() -> {
    6. while (true) {
    7. synchronized (lock) {
    8. while (turn != 2) {
    9. try {
    10. lock.wait();
    11. } catch (InterruptedException e) {
    12. Thread.currentThread().interrupt();
    13. }
    14. }
    15. System.out.println("a");
    16. turn = 0;
    17. lock.notifyAll();
    18. }
    19. }
    20. });
    21. Thread threadB = new Thread(() -> {
    22. while (true) {
    23. synchronized (lock) {
    24. while (turn != 1) {
    25. try {
    26. lock.wait();
    27. } catch (InterruptedException e) {
    28. Thread.currentThread().interrupt();
    29. }
    30. }
    31. System.out.println("b");
    32. turn = 2;
    33. lock.notifyAll();
    34. }
    35. }
    36. });
    37. Thread threadC = new Thread(() -> {
    38. while (true) {
    39. synchronized (lock) {
    40. while (turn != 0) {
    41. try {
    42. lock.wait();
    43. } catch (InterruptedException e) {
    44. Thread.currentThread().interrupt();
    45. }
    46. }
    47. System.out.println("c");
    48. turn = 1;
    49. lock.notifyAll();
    50. }
    51. }
    52. });
    53. threadA.start();
    54. threadB.start();
    55. threadC.start();
    56. }
    57. }

     

  • 相关阅读:
    计算机毕业设计Java校园外卖零食商城系统(源码+系统+mysql数据库+Lw文档)
    【漏洞复现】广联达OA漏洞合集(信息泄露+SQL注入+文件上传)
    《Java笔记——基础语法》
    vue 多环境文件配置(开发,测试,生产)
    JAVA班主任管理系统(源代码+论文)
    leetcode 20. 有效的括号
    连夜解决 maven 阿里云镜像无法下载 gexin-rp-* 的问题
    Verilog使用vscode
    【JAVA】值传递与引用传递
    Java 计算某年份二月的天数
  • 原文地址:https://blog.csdn.net/m0_74343467/article/details/132795989