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<" + id + ">";
}

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<" + id + ">";
}

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();
}
}

你可能感兴趣的:(java,thread)