Homework.java

    1. package edu.mama.ls18.homework;
    2. public class Homework {
    3. /**
    4. * 编写一段程序启动三个线程,要求:
    5. * 1、三个线程的名称是A,B,C
    6. * 2、每个线程打印自己的名称5遍
    7. * 3、B打印完成后再开始打印A和C
    8. */
    9. public static void print() {
    10. //TODO
    11. Runnable aThread = new AThread();
    12. Thread aTask = new Thread(aThread,"ATread");
    13. Runnable bThread = new BThread();
    14. Thread bTask = new Thread(bThread,"BTread");
    15. Runnable cThread = new CThread();
    16. Thread cTask = new Thread(cThread,"CTread");
    17. bTask.start();
    18. try {
    19. bTask.join();
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. }
    23. aTask.start();
    24. cTask.start();
    25. }
    26. /**
    27. * 龟兔赛跑
    28. *
    29. * 编写一段程序,要求:
    30. * 1、兔子速度3米/秒,每跑5秒就要睡觉休息,每次休息时长为5~20秒不等
    31. * 2、乌龟速度1米/秒,从来不休息
    32. * 3、每秒打印兔子和乌龟所跑长度(单位:米)
    33. * 4、任何一方到达50米的终点时,比赛结束,打印比赛结果(是乌龟胜利还是兔子胜利)
    34. */
    35. public static void run() {
    36. //TODO
    37. PrintThread printThread = new PrintThread(50);
    38. Thread printTask = new Thread(printThread);
    39. printTask.start();
    40. }
    41. public static void main(String[] args) {
    42. // print();
    43. run();
    44. }
    45. }

    ATread.java

    1. package edu.mama.ls18.homework;
    2. public class AThread implements Runnable {
    3. @Override
    4. public void run() {
    5. int count = 5;
    6. while(count > 0){
    7. System.out.println(Thread.currentThread().getName());
    8. count--;
    9. }
    10. }
    11. }

    BTread.java

    1. package edu.mama.ls18.homework;
    2. public class BThread implements Runnable {
    3. @Override
    4. public void run() {
    5. int count = 5;
    6. while(count > 0){
    7. System.out.println(Thread.currentThread().getName());
    8. count--;
    9. }
    10. }
    11. }

    CTread.java

    1. package edu.mama.ls18.homework;
    2. public class CThread implements Runnable {
    3. @Override
    4. public void run() {
    5. int count = 5;
    6. while(count > 0){
    7. System.out.println(Thread.currentThread().getName());
    8. count--;
    9. }
    10. }
    11. }

    PrintThread.java

    1. package edu.mama.ls18.homework;
    2. public class PrintThread implements Runnable {
    3. //比赛距离
    4. private int gameDistance;
    5. public PrintThread(int gameDistance) {
    6. this.gameDistance = gameDistance;
    7. }
    8. @Override
    9. public void run() {
    10. TortoiseThread tortoiseThread = new TortoiseThread("乌龟",gameDistance);
    11. Thread tortoiseTask = new Thread(tortoiseThread);
    12. RabbitThread rabbitThread = new RabbitThread("兔子",gameDistance);
    13. Thread rabbitTask = new Thread(rabbitThread);
    14. //设置守护线程
    15. tortoiseTask.setDaemon(true);
    16. tortoiseTask.start();
    17. rabbitTask.setDaemon(true);
    18. rabbitTask.start();
    19. while (true) {
    20. try {
    21. Thread.sleep(1000);
    22. } catch (InterruptedException e) {
    23. e.printStackTrace();
    24. }
    25. System.out.println(tortoiseThread.getName() + "跑了" + tortoiseThread.getDistance() + "米");
    26. if (tortoiseThread.getDistance() >= gameDistance){
    27. System.out.println(tortoiseThread.getName()+"胜利");
    28. break;
    29. }
    30. System.out.println(rabbitThread.getName() + "跑了" + rabbitThread.getDistance() + "米");
    31. if (rabbitThread.getDistance() >= gameDistance){
    32. System.out.println(rabbitThread.getName()+"胜利");
    33. break;
    34. }
    35. }
    36. }
    37. }

    TortoiseThread.java

    1. package edu.mama.ls18.homework;
    2. public class TortoiseThread implements Runnable {
    3. //名字
    4. private String name;
    5. //跑的距离
    6. private int distance = 0;
    7. //比赛距离
    8. private int gameDistance;
    9. public TortoiseThread(String name, int gameDistance) {
    10. this.name = name;
    11. this.gameDistance = gameDistance;
    12. }
    13. @Override
    14. public void run() {
    15. while(distance < gameDistance){
    16. try {
    17. Thread.sleep(1000);
    18. } catch (InterruptedException e) {
    19. e.printStackTrace();
    20. }
    21. distance ++;
    22. }
    23. }
    24. public int getDistance() {
    25. return distance;
    26. }
    27. public String getName() {
    28. return name;
    29. }
    30. }

    RabbitThread.java

    1. package edu.mama.ls18.homework;
    2. import java.util.Random;
    3. public class RabbitThread implements Runnable {
    4. //名字
    5. private String name;
    6. //跑的距离
    7. private int distance = 0;
    8. //休息时间
    9. private int runningSec = 0;
    10. //比赛距离
    11. private int gameDistance;
    12. public RabbitThread(String name, int gameDistance) {
    13. this.name = name;
    14. this.gameDistance = gameDistance;
    15. }
    16. @Override
    17. public void run() {
    18. while (distance < gameDistance) {
    19. if (delRunningSec()) {
    20. haveRest();
    21. } else {
    22. try {
    23. Thread.sleep(1000);
    24. } catch (InterruptedException e) {
    25. e.printStackTrace();
    26. }
    27. distance = distance + 3;
    28. }
    29. }
    30. }
    31. /**
    32. * 每跑5秒就要睡觉休息
    33. * @return
    34. */
    35. private boolean delRunningSec (){
    36. runningSec ++;
    37. if(runningSec > 5){
    38. runningSec = 0;
    39. return true;
    40. }else{
    41. return false;
    42. }
    43. }
    44. /**
    45. * 休息时长为5~20秒不等
    46. */
    47. private void haveRest(){
    48. Random rand = new Random();
    49. int restSec = rand.nextInt(16) + 5;
    50. System.out.println(name + "跑了" + distance + "米,跑累了,需要休息"+ restSec +"秒");
    51. while(restSec > 0){
    52. try {
    53. Thread.sleep(1000);
    54. } catch (InterruptedException e) {
    55. e.printStackTrace();
    56. }
    57. restSec--;
    58. System.out.println(name + "跑了" + distance + "米,休息中,还剩"+ restSec +"秒");
    59. }
    60. }
    61. public int getDistance() {
    62. return distance;
    63. }
    64. public String getName() {
    65. return name;
    66. }
    67. }

    结果
    image.png
    image.png