玩轉Java多線程(乒乓球比賽)

来源:https://www.cnblogs.com/haibiscuit/archive/2019/06/26/11094348.html
-Advertisement-
Play Games

轉載請標明博客的地址 本人博客和github賬號,如果對你有幫助請在本人github項目AioSocket上點個star,激勵作者對社區貢獻 個人博客:https://www.cnblogs.com/haibiscuit/ 個人github: https://github.com/haibiscui ...


轉載請標明博客的地址

本人博客和github賬號,如果對你有幫助請在本人github項目AioSocket上點個star,激勵作者對社區貢獻

 

雖然代碼有點多,但是這樣設計也是為了程式的可擴展性,和解耦合

比賽規則如下:

  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類

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前幾天,星球有人提到貪吃蛇,一下子就勾起了我的興趣,畢竟在那個Nokia稱霸的年代,這款游戲可是經典中的經典啊!而用Python(蛇)玩Snake(貪吃蛇),那再合適不過了
  • 一、鏈表 鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域。 相比於線性表順序結構,操 ...
  • [TOC] 記憶體分配和釋放 STL中有兩個分配器,一級分配器和二級分配器,預設使用二級分配器,使用二級分配器分配大記憶體時會調用一級分配器去執行,一級分配器使用malloc和free分配和釋放記憶體。如果分配小記憶體那麼二級分配器會從記憶體池中進行查找,防止malloc/free的開銷。 為了瞭解原理,不深 ...
  • 代理模式:為其他對象提供一種代理來控制對這個對象的訪問。我們來看這樣一個簡單的例子,現在超市商家不直接把商品交給客戶,而是通過一些平臺的外賣小哥把商品送到客戶手中,此時外賣小哥就起到了代理的作用。代碼如下: ...
  • 今天我們來看一個編程語言入門必演示的HelloWorld程式,藉此來展示我們的重點知識。話不多說,先看代碼。 本段代碼在eclipse中編輯運行,怎麼在eclipse中新建項目呢:點擊左上角File選擇New一個Project.雖然本例僅僅實現了一個簡單的輸出HelloWorld一行字元串的簡單功能 ...
  • 花下貓語:之前說過,我對於編程語言跟其它學科的融合非常感興趣,但我還說漏了一點,就是我對於 Python 跟其它編程語言的對比學習,也很感興趣。所以,我一直希望能聚集一些有其它語言基礎的同學,一起討論共通的語言特性間的話題。不同語言的碰撞,常常能帶給人更高維的視角,也能觸及到語言的根基,這個過程是極 ...
  • 數據結構與演算法的關係 數據結構(data structure)是一門研究組織數據方式的學科,有了編程語言也就有了數據結構。學好數據結構可以編寫出跟家漂亮,更加有效率的代碼 要學好數據結構就要多多考慮如何將生活中遇到的問題,用程式去實現解決 程式=數據結構+演算法 數據結構是演算法的基礎,換言之,想要學好 ...
  • 今天在別人代碼中發現java8 新特性,發現自己閱讀代碼有點兒吃力,很是汗顏,java8新特性都出來這麼久了,只知其名不見其形,所有今天回家補了補知識。 一、 介面 在java8 中,介面中引入了新的關鍵字default和static,通過使用default修飾方法,可以讓我們在介面中定義具體的方法 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...