Homework.java
package edu.mama.ls18.homework;
public class Homework {
/**
* 编写一段程序启动三个线程,要求:
* 1、三个线程的名称是A,B,C
* 2、每个线程打印自己的名称5遍
* 3、B打印完成后再开始打印A和C
*/
public static void print() {
//TODO
Runnable aThread = new AThread();
Thread aTask = new Thread(aThread,"ATread");
Runnable bThread = new BThread();
Thread bTask = new Thread(bThread,"BTread");
Runnable cThread = new CThread();
Thread cTask = new Thread(cThread,"CTread");
bTask.start();
try {
bTask.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
aTask.start();
cTask.start();
}
/**
* 龟兔赛跑
*
* 编写一段程序,要求:
* 1、兔子速度3米/秒,每跑5秒就要睡觉休息,每次休息时长为5~20秒不等
* 2、乌龟速度1米/秒,从来不休息
* 3、每秒打印兔子和乌龟所跑长度(单位:米)
* 4、任何一方到达50米的终点时,比赛结束,打印比赛结果(是乌龟胜利还是兔子胜利)
*/
public static void run() {
//TODO
PrintThread printThread = new PrintThread(50);
Thread printTask = new Thread(printThread);
printTask.start();
}
public static void main(String[] args) {
// print();
run();
}
}
ATread.java
package edu.mama.ls18.homework;
public class AThread implements Runnable {
@Override
public void run() {
int count = 5;
while(count > 0){
System.out.println(Thread.currentThread().getName());
count--;
}
}
}
BTread.java
package edu.mama.ls18.homework;
public class BThread implements Runnable {
@Override
public void run() {
int count = 5;
while(count > 0){
System.out.println(Thread.currentThread().getName());
count--;
}
}
}
CTread.java
package edu.mama.ls18.homework;
public class CThread implements Runnable {
@Override
public void run() {
int count = 5;
while(count > 0){
System.out.println(Thread.currentThread().getName());
count--;
}
}
}
PrintThread.java
package edu.mama.ls18.homework;
public class PrintThread implements Runnable {
//比赛距离
private int gameDistance;
public PrintThread(int gameDistance) {
this.gameDistance = gameDistance;
}
@Override
public void run() {
TortoiseThread tortoiseThread = new TortoiseThread("乌龟",gameDistance);
Thread tortoiseTask = new Thread(tortoiseThread);
RabbitThread rabbitThread = new RabbitThread("兔子",gameDistance);
Thread rabbitTask = new Thread(rabbitThread);
//设置守护线程
tortoiseTask.setDaemon(true);
tortoiseTask.start();
rabbitTask.setDaemon(true);
rabbitTask.start();
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(tortoiseThread.getName() + "跑了" + tortoiseThread.getDistance() + "米");
if (tortoiseThread.getDistance() >= gameDistance){
System.out.println(tortoiseThread.getName()+"胜利");
break;
}
System.out.println(rabbitThread.getName() + "跑了" + rabbitThread.getDistance() + "米");
if (rabbitThread.getDistance() >= gameDistance){
System.out.println(rabbitThread.getName()+"胜利");
break;
}
}
}
}
TortoiseThread.java
package edu.mama.ls18.homework;
public class TortoiseThread implements Runnable {
//名字
private String name;
//跑的距离
private int distance = 0;
//比赛距离
private int gameDistance;
public TortoiseThread(String name, int gameDistance) {
this.name = name;
this.gameDistance = gameDistance;
}
@Override
public void run() {
while(distance < gameDistance){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
distance ++;
}
}
public int getDistance() {
return distance;
}
public String getName() {
return name;
}
}
RabbitThread.java
package edu.mama.ls18.homework;
import java.util.Random;
public class RabbitThread implements Runnable {
//名字
private String name;
//跑的距离
private int distance = 0;
//休息时间
private int runningSec = 0;
//比赛距离
private int gameDistance;
public RabbitThread(String name, int gameDistance) {
this.name = name;
this.gameDistance = gameDistance;
}
@Override
public void run() {
while (distance < gameDistance) {
if (delRunningSec()) {
haveRest();
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
distance = distance + 3;
}
}
}
/**
* 每跑5秒就要睡觉休息
* @return
*/
private boolean delRunningSec (){
runningSec ++;
if(runningSec > 5){
runningSec = 0;
return true;
}else{
return false;
}
}
/**
* 休息时长为5~20秒不等
*/
private void haveRest(){
Random rand = new Random();
int restSec = rand.nextInt(16) + 5;
System.out.println(name + "跑了" + distance + "米,跑累了,需要休息"+ restSec +"秒");
while(restSec > 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
restSec--;
System.out.println(name + "跑了" + distance + "米,休息中,还剩"+ restSec +"秒");
}
}
public int getDistance() {
return distance;
}
public String getName() {
return name;
}
}
结果