轉載請標明博客的地址
本人博客和github賬號,如果對你有幫助請在本人github項目AioSocket上點個star,激勵作者對社區貢獻
- 個人博客:https://www.cnblogs.com/haibiscuit/
- 個人github: https://github.com/haibiscuit?tab=repositories
雖然代碼有點多,但是這樣設計也是為了程序的可擴展性,和解耦合
比賽規則如下:
1.有四個選手, A1和A2為一個隊, B1和B2為另一個隊. A1首先發球(啟動球), 然后B1, A2, B2將最后發球. 每一輪每個選手發6個球.
2.選手不改變他們的位置.
3.比賽期間, 雙方選手必須輪流發球,並且在同一個隊伍的兩個選手可以競爭發球.
4.當輪到某個選手時, 他/她可以調用一個叫做shot(rate) 的隨機函數來模擬比賽,在給定概率rate以內,該函數返回 “in”, 否則返回”out”. 例如 rate=85%, 則球在界內的概率為85%, 出界的概率為15%.
5.如果shot函數返回”in”, 對方選手必須調用shot函數把球打回.
6.如果shot函數返回”out”, 對方選手贏得1分,隨后重新發球.
7.當每個選手發完6個球后比賽終止.分數多的一方贏得比賽.分數一樣多,比賽為平局.
8.每個選手作為一個線程實現.
直接上源程序:
package Test;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author haibiscuit(本人博客名)
*/
public class TableTennisThreadDemo {
public static void main(String args[]) {
CountDownLatch countDownLatch = new CountDownLatch(4);
double rate = 0.75; //設置選手贏的概率
int sendNum = 6; //每個選手發球次數
//兩個隊的名字
String team1_Name = TeamController.getTeamName()[0];
String team2_Name = TeamController.getTeamName()[1];
//四個選手名字
String team1_memName1 = TeamController.getTeamMems(team1_Name)[0];
String team1_memName2 = TeamController.getTeamMems(team1_Name)[1];
String team2_memName1 = TeamController.getTeamMems(team2_Name)[0];
String team2_memName2 = TeamController.getTeamMems(team2_Name)[1];
//創建兩個隊
Team team1 = new Team(team1_Name);
Team team2 = new Team(team2_Name);
//設置對手
TeamController.setTeamRival(team2, team1);
//設置比賽場數
TeamController.setMatchTimes(sendNum*4); //參數為選手數*每個選手的發球數
//創建乒乓球對象
TableTennis tableTennis = new TableTennis(rate);
//創建四個線程類
Thread thread1 = new Thread(new TeamMember(team1_memName1, sendNum, team1, tableTennis, countDownLatch));
Thread thread2 = new Thread(new TeamMember(team1_memName2, sendNum, team1, tableTennis, countDownLatch));
Thread thread3 = new Thread(new TeamMember(team2_memName1, sendNum, team2, tableTennis, countDownLatch));
Thread thread4 = new Thread(new TeamMember(team2_memName2, sendNum, team2, tableTennis, countDownLatch));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(TableTennisThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
}
thread1.start();
thread2.start();
thread3.start();
thread4.start();
//等待四個選手運行完
try {
countDownLatch.await();
System.out.println("team1得分:"+team1.getScore());
System.out.println("team2得分:"+team2.getScore());
if(team1.getScore()>team2.getScore()){
System.out.println("恭喜team1隊贏!");
}else if(team1.getScore()<team2.getScore()){
System.out.println("恭喜team2隊贏!");
}else{
System.out.println("平局了!");
}
} catch (InterruptedException ex) {
Logger.getLogger(TableTennisThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class TableTennis {
int rate; //贏的頻率
volatile int state = 0; //球的狀態,0代表發球,1代表傳球,默認是發球狀態
String trail = "team1"; //傳球的軌跡
String teamTrail = "team1"; //發球的軌跡
volatile boolean isEnd = false; //判斷比賽有沒有結束
volatile int totalSendNum = 0; //總共發球次數
public int getTotalSendNum() {
return totalSendNum;
}
public void addTotalSendNum() {
this.totalSendNum = ++totalSendNum;
}
public boolean isIsEnd() {
return isEnd;
}
public void setIsEnd(boolean isEnd) {
this.isEnd = isEnd;
}
public String getTeamTrail() {
return teamTrail;
}
public void setTeamTrail(String teamTrail) {
this.teamTrail = teamTrail;
}
public TableTennis(double rate) {
this.rate = (int) (rate * 100);
}
/**
* 判斷輸贏
*
* @return
*/
public final String shot() {
int randRate = (int) (Math.random() * 100);
if (((100 - randRate) > (this.rate))) {
return "out";
} else {
return "in";
}
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getTrail() {
return trail;
}
public void setTrail(String trail) {
this.trail = trail;
}
}
class Team {
String teamName; //球隊的名稱
int score = 0; //球隊的得分
public Team(String teamName) {
this.teamName = teamName;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public int getScore() {
return score;
}
public void addScore() {
this.score = ++score;
}
}
class TeamController {
private static final String[] team = {"team1", "team2"}; //比賽隊的名稱
private static final HashMap<String, String[]> map = new HashMap(); //用於存放各隊的隊員信息
private static final HashMap<String, Team> rivalTeam = new HashMap(); //用於存放各隊的對手
private static int totalMatchTime; //默認0場比賽
static {
map.put(team[0], new String[]{"A1", "A2"}); //初始化team1隊球員
map.put(team[1], new String[]{"B1", "B2"}); //初始化team2隊球員
}
//設置各隊的對方隊伍
public static void setTeamRival(Team rivalTeam1, Team rivalTeam2) {
rivalTeam.put(team[0], rivalTeam1);
rivalTeam.put(team[1], rivalTeam2);
}
public static void setMatchTimes(int time){
totalMatchTime = time;
}
public static int getMatchTimes(){
return totalMatchTime;
}
public static Team getRivalTeam(String teamName) {
return rivalTeam.get(teamName);
}
public static String[] getTeamName() {
return team;
}
public static String[] getTeamMems(String teamName) {
return map.get(teamName);
}
}
//隊員
class TeamMember implements Runnable {
Team team; //球員所在的球隊
TableTennis tableTennis; //每個隊員所傳的球
String memName; //球員名稱
int sendNum; //每個隊員傳球的次數
CountDownLatch countDownLatch; //保證每個球員發球次數
public TeamMember(String memName, int sendNum, Team team, TableTennis tableTennis, CountDownLatch countDownLatch) {
this.team = team;
this.tableTennis = tableTennis;
this.memName = memName;
this.sendNum = sendNum;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
while (!this.tableTennis.isIsEnd()) { //當在發球此數內
sendBall();
}
countDownLatch.countDown();
}
/**
* 發球的邏輯注意這里的synchronized,並發控制
*/
private void sendBall() {
synchronized (tableTennis) {
// System.out.println("當前發球線程:" + Thread.currentThread().getName());
while (!(this.tableTennis.getTrail().equals(this.team.getTeamName()))) { //判斷是否是該隊傳球
// System.out.println("發球睡眠線程:" + Thread.currentThread().getName());
if(this.tableTennis.isIsEnd()){
return;
}
try {
tableTennis.wait();
} catch (InterruptedException ex) {
Logger.getLogger(TableTennis.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(this.tableTennis.isIsEnd()){ //判斷比賽有沒有結束,結束直接返回
return;
}
if (this.tableTennis.getState() == 0) { //判斷是不是發球狀態
this.sendNum--; //將發球次數減一
}
//發球的時候判斷在界內還是界外
if (this.tableTennis.shot().equals("in")) { //發球表示球在界內
this.tableTennis.setTrail(TeamController.getRivalTeam(this.team.getTeamName()).getTeamName()); //對手接球
this.tableTennis.setState(1); //如果發球在界內,將發球狀態改為傳球狀態
System.out.print(this.memName + "-in-");
} else { //如果在界外,此時還是發球狀態
TeamController.getRivalTeam(this.team.getTeamName()).addScore(); //對方隊加上一分
System.out.print(this.memName + "-out\n");
this.tableTennis.setTeamTrail(TeamController.getRivalTeam(this.tableTennis.getTeamTrail()).getTeamName());
this.tableTennis.setTrail(this.tableTennis.getTeamTrail());
this.tableTennis.setState(0);
this.tableTennis.addTotalSendNum(); //將發球次數加一
if(this.tableTennis.getTotalSendNum()==TeamController.getMatchTimes()){
this.tableTennis.setIsEnd(true);
}
}
//執行完喚醒對手線程
tableTennis.notifyAll();
}
}
}
實現思路:利用多線程的wait和notifyall的方法實現線程切換,核心類是TableTennis類