关闭

JAVA多线程实现龟兔赛跑

标签: JAVA多线程龟兔赛跑
37人阅读 评论(0) 收藏 举报
分类:

要求

1、兔子每秒跑5米,但是每10米要休息2秒

2、乌龟每秒钟4米,不休息

3、谁先到达终点,比赛结束

首先创建一个动物类Animal

package com.th.animal;


import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;


public abstract class Animal implements Runnable {


protected String name;//动物名称


protected int speed;//动物速度


protected int now;//当前已经跑的路程


protected ExecutorService executor;//线程管理器,方便结束线程


protected CyclicBarrier barrier;//等待计时器,要求两个参赛选手 都准备就绪后才  开始比赛


public static volatile boolean FINISH = false;//是否比赛完成的标记


public final static int SUCCESS = 20;//比赛的路程


Animal(String name, int speed, ExecutorService executor, CyclicBarrier barrier) {
this.name = name;
this.speed = speed;
this.executor = executor;
this.barrier = barrier;
}


Animal(String name, int speed, ExecutorService executor) {
this.name = name;
this.speed = speed;
this.executor = executor;
}


@Override
public abstract void run();

//判断是否完成比赛
protected void finish() {
if (now >= SUCCESS) {
System.out.println(name + " 跑完了,结束比赛!");
FINISH = true;
executor.shutdownNow();
}
}
}


创建兔子类 Rabbit

package com.th.animal;


import java.text.SimpleDateFormat;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;


public class Rabbit extends Animal {


Rabbit(String name, int speed, ExecutorService executor, CyclicBarrier barrier) {
super(name, speed, executor, barrier);
}


Rabbit(String name, int speed, ExecutorService executor) {
super(name, speed, executor);
}


@Override
public void run() {
try {
barrier.await();//等待参赛选手都准备就绪
} catch (Exception e1) {
}


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(System.currentTimeMillis()));
while (!FINISH) {
System.out.println(this.name + " : 我的速度是:" + this.speed + " 我跑了这么多" + this.now + "了");
try {
Thread.sleep(1000);
if (now % 10 == 0) {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
now += speed;
finish();
}
}


}


创建乌龟类 Tortoise

package com.th.animal;


import java.text.SimpleDateFormat;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;


public class Tortoise extends Animal {


Tortoise(String name, int speed, ExecutorService executor, CyclicBarrier barrier) {
super(name, speed, executor, barrier);
}
Tortoise(String name, int speed, ExecutorService executor) {
super(name, speed, executor);
}


@Override
public void run() {
System.out.println();
try {
Thread.sleep(2000);
barrier.await();
} catch (Exception e1) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(System.currentTimeMillis()));
while (!FINISH) {
System.out.println(this.name + " : 我的速度是:" + this.speed + " 我跑了这么多" + this.now + "了");
now += speed;
finish();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}


}


参赛选手准备就绪 准备开始比赛

创建测试类

package com.th.animal;


import java.text.SimpleDateFormat;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Test {


public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(2);


CyclicBarrier barrier = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("参赛选手准备就绪;比赛开始! " + sdf.format(System.currentTimeMillis()));
}
});


Rabbit rabbit = new Rabbit("小白兔", 5, es, barrier);
Tortoise tortoise = new Tortoise("乌龟", 4, es, barrier);


es.submit(rabbit);
es.submit(tortoise);
es.shutdown();


}


}


比赛结果:


参赛选手准备就绪;比赛开始! 2017-10-13 09:24:20
2017-10-13 09:24:20
乌龟 : 我的速度是:4 我跑了这么多0了
2017-10-13 09:24:20
小白兔 : 我的速度是:5 我跑了这么多0了
乌龟 : 我的速度是:4 我跑了这么多4了
乌龟 : 我的速度是:4 我跑了这么多8了
小白兔 : 我的速度是:5 我跑了这么多5了
乌龟 : 我的速度是:4 我跑了这么多12了
小白兔 : 我的速度是:5 我跑了这么多10了
乌龟 : 我的速度是:4 我跑了这么多16了
乌龟 跑完了,结束比赛!

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3259次
    • 积分:126
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:22篇
    • 译文:0篇
    • 评论:0条