java模拟运动,java线程赞助以及并发框架模拟运动员跑步比赛

java线程协助以及并发框架模拟运动员跑步比赛

java多线程版本:

import java.util.Collection;

import java.util.Collections;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Set;

class Athlete implements Runnable {

private final int id;

private Game game;

public Athlete(int id, Game game) {

this.id = id;

this.game = game;

}

public boolean equals(Object o) {

if (!(o instanceof Athlete))

return false;

Athlete athlete = (Athlete) o;

return id == athlete.id;

}

public String toString() {

return "Athlete";

}

public int hashCode() {

return new Integer(id).hashCode();

}

public void run() {

try {

game.prepare(this);

} catch (InterruptedException e) {

System.out.println(this + " quit the game");

}

}

}

public class Game implements Runnable {

private Set players = new HashSet();

private boolean start = false;

public synchronized boolean isStart() {

return start;

}

public synchronized void setStart(boolean start) {

this.start = start;

}

public void addPlayer(Athlete one) {

players.add(one);

}

public void removePlayer(Athlete one) {

players.remove(one);

}

public Collection getPlayers() {

return Collections.unmodifiableSet(players);

}

public void prepare(Athlete athlete) throws InterruptedException {

synchronized (this) {

System.out.println(athlete + " ready!");//报告准备完毕...

while (!isStart()) {//阻塞条件不满足,就保持阻塞状态,就等开启条件,然后唤醒,唤醒就开始跑

System.out.println(athlete + " isStart=false");

wait();

}

}

System.out.println(athlete + " go!");//唤醒就跑

}

public void go() {

setStart(true);//改变条件,一旦线程唤醒就可以跑了...

synchronized (this) {

//唤醒所有的阻塞线程,开始跑...

//注意不能使用notify(),notify只能唤醒一个线程

notifyAll();

}

}

public void ready() {

setStart(false);

Iterator iter = getPlayers().iterator();

while (iter.hasNext()) {

new Thread(iter.next()).start();

}

}

public void run() {

System.out.println("Ready......");

//口令:预备...

ready();

try {

Thread.sleep(100L);//给一点时间,运动员动作准备就绪...

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("Go!!!");

//口令:跑...

go();

}

public static void main(String[] args) {

Game game = new Game();

for (int i = 0; i < 10; i++) {

game.addPlayer(new Athlete(i, game));

}

new Thread(game).start();

}

}

java并发包改写版本:

class Athlete implements Runnable {

private final int id;

private CountDownLatch countDown;

public Athlete(int id, CountDownLatch countDown) {

this.id = id;

this.countDown = countDown;

}

public boolean equals(Object o) {

if (!(o instanceof Athlete))

return false;

Athlete athlete = (Athlete) o;

return id == athlete.id;

}

public String toString() {

return "Athlete";

}

public int hashCode() {

return new Integer(id).hashCode();

}

public void run() {

try {

System.out.println(this + " ready");//报告准备就绪

countDown.await();//阻塞

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(this + " go~~~"); //开始跑...

}

}

public class CountDownLatchGame {

private static CountDownLatch countDown = new CountDownLatch(1);

public static void main(String[] args) {

ExecutorService exe = Executors.newFixedThreadPool(10);

System.out.println("Ready!!!!");

for(int i=0;i<10;i++) {

exe.execute(new Athlete(i+1, countDown));

}

try {

Thread.sleep(200L);//给一点时间,运动员动作准备就绪...

System.out.println("Go!");

countDown.countDown();//命令:跑...

Thread.sleep(300L);

} catch (InterruptedException e) {

e.printStackTrace();

}

//执行完毕

exe.shutdown();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值