龟兔鸡赛跑:
场景如下。兔子、乌龟和公鸡进行赛跑,其中兔子每秒0.5米的速度,每跑2米休息10秒;乌龟每秒跑0.1米,不休息;公鸡每秒0.8米,每跑3秒需要吃一条虫子,耗时0.6秒。 当其中一个跑到终点后其他动物就不跑了;比赛道路长20米。试用多线程模拟该比赛过程。
Animal类(父类)
package com.guoyu.anli;
/**
* @author ShuaiGUO
* 父类 动物类,继承Thread,变成线程类
*/
public class Animal extends Thread {
//赛道长度共有的属性,使用protected,让能子类使用到
protected static double length = 20;
//跑步速度
private double speed;
//动物名称
private String animalName;
//总过跑了多少米
//volatile实现了多线程之间的看见性
protected static volatile double count;
public Animal() {
}
public Animal(double speed, String animalName) {
//设置线程名称
super(animalName);
this.speed = speed;
this.animalName = animalName;
}
public static double getLength() {
return length;
}
public static void setLength(double length) {
Animal.length = length;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
public static double getCount() {
return count;
}
public static void setCount(double count) {
Animal.count = count;
}
}
Rabbit类
package com.guoyu.anli;
/**
* @author ShuaiGUO
* 兔子线程,因为继承了Animal所以也间接的变成了线程类
*/
public class Rabbit extends Animal {
//聚合其余线程
private Chicken chicken;
private Tortoise tortoise;
public Rabbit(double speed, String animalName) {
super(speed, animalName);
this.chicken = chicken;
this.tortoise = tortoise;
}
public Chicken getChicken() {
return chicken;
}
public void setChicken(Chicken chicken) {
this.chicken = chicken;
}
public Tortoise getTortoise() {
return tortoise;
}
public void setTortoise(Tortoise tortoise) {
this.tortoise = tortoise;
}
@Override
public void run() {
for (double i = 0; i <length ; i += this.getSpeed()) {
//累加所有动物总共跑了多少米
count += this.getSpeed();
System.out.println(this.getName()+"跑了"+(i + this.getSpeed())+"米");
try {
//模拟1秒钟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("所有动物一共 跑了"+count+"米");
//模拟兔子每跑2米休息10秒
if (i % 2 ==0){
try {
//模拟10秒钟
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//率先跳出循环的就是冠军
System.out.println(this.getName()+"是冠军!!!!");
//判断线程是否存活
if (this.chicken.isAlive()){
//结束线程
this.chicken.stop();
}
//判断线程是否存活
if (this.tortoise.isAlive()){
//结束线程
this.tortoise.stop();
}
}
}
Chicken类
package com.guoyu.anli;
/**
* @author ShuaiGUO
* 小鸡线程
*/
public class Chicken extends Animal {
private Integer time = 0;
private Rabbit rabbit;
private Tortoise tortoise;
public Chicken(double speed, String animalName) {
super(speed, animalName);
}
public Rabbit getRabbit() {
return rabbit;
}
public void setRabbit(Rabbit rabbit) {
this.rabbit = rabbit;
}
public Tortoise getTortoise() {
return tortoise;
}
public void setTortoise(Tortoise tortoise) {
this.tortoise = tortoise;
}
@Override
public void run() {
for (double i = 0; i <length ; i += this.getSpeed()) {
//累加所有动物总共跑了多少米
count += this.getSpeed();
System.out.println(this.getName()+"跑了"+(i + this.getSpeed())+"米");
try {
//模拟1秒钟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time++;
System.out.println("所有动物一共 跑了"+count+"米");
//模拟小鸡每跑3秒需要吃一条虫子,耗时0.6秒
if (time != 0 && time % 3 ==0){
System.out.println("小鸡正在吃虫子");
try {
//模拟0.6秒钟
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//率先跳出循环的就是冠军
System.out.println(this.getName()+"是冠军!!!!");
//判断线程是否存活
if (this.tortoise.isAlive()){
//结束线程
this.tortoise.stop();
}
//判断线程是否存活
if (this.rabbit.isAlive()){
//结束线程
this.rabbit.stop();
}
}
}
Tortoise类
package com.guoyu.anli;
/**
* 乌龟线程
* @author ShuaiGUO
*/
public class Tortoise extends Animal {
private Rabbit rabbit;
private Chicken chicken;
public Tortoise(double speed, String animalName) {
super(speed, animalName);
}
public Rabbit getRabbit() {
return rabbit;
}
public void setRabbit(Rabbit rabbit) {
this.rabbit = rabbit;
}
public Chicken getChicken() {
return chicken;
}
public void setChicken(Chicken chicken) {
this.chicken = chicken;
}
@Override
public void run() {
for (double i = 0; i <length ; i += this.getSpeed()) {
//累加所有动物总共跑了多少米
count += this.getSpeed();
System.out.println(this.getName()+"跑了"+(i + this.getSpeed())+"米");
try {
//模拟1秒钟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("所有动物一共 跑了"+count+"米");
}
//率先跳出循环的就是冠军
System.out.println(this.getName()+"是冠军!!!!");
//判断线程是否存活
if (this.chicken.isAlive()){
//结束线程
this.chicken.stop();
}
//判断线程是否存活
if (this.rabbit.isAlive()){
//结束线程
this.rabbit.stop();
}
}
}
主函数
package com.guoyu.anli;
import java.util.ArrayList;
import java.util.List;
/**
* @author ShuaiGUO
*/
public class Main {
public static void main(String[] args) {
//创建线程对象
Rabbit rabbit = new Rabbit(0.5,"兔子");
Tortoise tortoise = new Tortoise(0.1, "乌龟");
Chicken chicken = new Chicken(0.8, "小鸡");
//给属性赋值
rabbit.setTortoise(tortoise);
rabbit.setChicken(chicken);
tortoise.setRabbit(rabbit);
tortoise.setChicken(chicken);
chicken.setRabbit(rabbit);
chicken.setTortoise(tortoise);
//创建集合对象来存储三个动物
ArrayList<Animal> animals = new ArrayList<>();
//将三个动物存储到集合
animals.add(rabbit);
animals.add(tortoise);
animals.add(chicken);
//遍历
for (Animal animal : animals) {
//启动线程
animal.start();
}
}
}