兔子
package ThreadDemo.Tortoiseandrabbitrace2;/*
@create 2021--01--07--18:18
*/
public class ThreadRabbit implements Runnable{
@Override
public void run() {
try {
for (int i = 1; i <= 100; i+=10) {
System.out.println("兔子跑了" + i + "米");
Thread.sleep(1000);
if (i == 51) {
Thread.sleep(15000);
}
}
} catch (Exception e) {
}
}
}
乌龟
package ThreadDemo.Tortoiseandrabbitrace2;/*
@create 2021--01--07--18:17
*/
public class ThreadTortoise implements Runnable {
@Override
public void run() {
try {
for (int i = 1; i <= 100; i += 5) {
System.out.println("乌龟跑了" + i + "米");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类
package ThreadDemo.Tortoiseandrabbitrace2;/*
@create 2021--01--07--18:19
*/
public class ThreadTest {
public static void main(String[] args) {
ThreadRabbit tr = new ThreadRabbit();
ThreadTortoise tt = new ThreadTortoise();
Thread t1 = new Thread(tr);
Thread t2 = new Thread(tt);
t1.start();
t2.start();
}
}