兔子

    1. package ThreadDemo.Tortoiseandrabbitrace2;/*
    2. @create 2021--01--07--18:18
    3. */
    4. public class ThreadRabbit implements Runnable{
    5. @Override
    6. public void run() {
    7. try {
    8. for (int i = 1; i <= 100; i+=10) {
    9. System.out.println("兔子跑了" + i + "米");
    10. Thread.sleep(1000);
    11. if (i == 51) {
    12. Thread.sleep(15000);
    13. }
    14. }
    15. } catch (Exception e) {
    16. }
    17. }
    18. }

    乌龟

    1. package ThreadDemo.Tortoiseandrabbitrace2;/*
    2. @create 2021--01--07--18:17
    3. */
    4. public class ThreadTortoise implements Runnable {
    5. @Override
    6. public void run() {
    7. try {
    8. for (int i = 1; i <= 100; i += 5) {
    9. System.out.println("乌龟跑了" + i + "米");
    10. Thread.sleep(1000);
    11. }
    12. } catch (InterruptedException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. }

    测试类

    1. package ThreadDemo.Tortoiseandrabbitrace2;/*
    2. @create 2021--01--07--18:19
    3. */
    4. public class ThreadTest {
    5. public static void main(String[] args) {
    6. ThreadRabbit tr = new ThreadRabbit();
    7. ThreadTortoise tt = new ThreadTortoise();
    8. Thread t1 = new Thread(tr);
    9. Thread t2 = new Thread(tt);
    10. t1.start();
    11. t2.start();
    12. }
    13. }