文章目录
文章链接
Java多线程实现跑步比赛【比赛详解】
Java多线程实现跑步比赛【基本设计】
Java多线程实现跑步比赛【RunMap——地图映射类】
Java多线程实现跑步比赛【RunMan——运动员映射类】
Java多线程实现跑步比赛【RunManFunc,RunStart,Utils——工具类】
Java多线程实现跑步比赛【SpringBoot——调用逻辑】
PlayerInfoController
package com.sport.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sport.services.PlayerInfoService;
/**
* 运动员信息查询Controller
* @version 1.0.0
*/
@CrossOrigin(origins = "*", maxAge = 10)
@RestController
@RequestMapping("/sports")
public class PlayerInfoController {
/** PlayerInfoService */
@Autowired
protected PlayerInfoService playerInfoService;
/**
* 开始比赛
* @param mapName 地图名称
*/
@RequestMapping(path = "/running", method = { RequestMethod.GET })
public void Running(@RequestParam String mapName) {
playerInfoService.playerRunning(mapName);
}
}
PlayerInfoService
package com.sport.services;
/**
* 运动员信息查询Service
* @version 1.0.0
*/
public interface PlayerInfoService {
/**
* 开始比赛
* @param mapName 地图名称
*/
void playerRunning(String mapName);
}
PlayerInfoServiceImpl
package com.sport.services.impl;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sport.controller.WebSocketServer;
import com.sport.entity.PlayerInfoEntity;
import com.sport.entity.RunMan;
import com.sport.map.RunMap;
import com.sport.mapper.PlayerInfoMapper;
import com.sport.services.PlayerInfoService;
import com.sport.services.RunStart;
import com.sport.utils.CsvUtils;
import com.sport.utils.MapUtils;
import com.sport.utils.ThreadPoolUtils;
/**
* 运动员信息查询Impl
* @version 1.0.0
*/
@Service
public class PlayerInfoServiceImpl implements PlayerInfoService {
/** PlayerInfoMapper */
@Autowired
protected PlayerInfoMapper playerInfoMapper;
/**
* @see com.sport.services.PlayerInfoService#playerRunning(String)
*/
@Override
public void playerRunning(String mapName) {
String uuid = UUID.randomUUID().toString();
List<RunMan> runMans = playerInfoMapper.playerQueryAll();
ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtils.buildRunGameThreadPoolExecutor();
RunMap runMap = MapUtils.initRunMap(mapName);
for (int i = 0; i < runMans.size(); i++) {
threadPoolExecutor.execute(new RunStart(runMap, runMans.get(i), playerInfoMapper, uuid));
}
}
}
PlayerInfoMapper.java
package com.sport.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.sport.entity.GameResult;
import com.sport.entity.RunMan;
/**
* 运动员信息查询Mapper
* @version 1.0.0
*/
@Mapper
@Repository
public interface PlayerInfoMapper {
/**
* 查询所有参赛运动员信息
* @return 参赛运动员信息
*/
public List<RunMan> playerQueryAll();
/**
* 登录比赛结果数据
* @param gameResult 比赛结果
*/
public void addGameResult(GameResult gameResult);
}
PlayerInfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sport.mapper.PlayerInfoMapper">
<resultMap id="PlayerAllResultMap" type="com.sport.entity.RunMan">
<result column="ID" property="id" jdbcType="INTEGER" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="AGE" property="age" jdbcType="INTEGER" />
<result column="GENDER" property="gender" jdbcType="INTEGER" />
<result column="AVERAGESPEED" property="averageSpeed" jdbcType="INTEGER" />
<result column="ADDEDWEIGHT" property="addedWeight" jdbcType="INTEGER" />
</resultMap>
<select id="playerQueryAll" resultMap="PlayerAllResultMap">
SELECT
ID,
NAME,
AGE,
GENDER,
AVERAGESPEED,
ADDEDWEIGHT
FROM
PLAYER_INFO
</select>
<insert id="addGameResult" parameterType="com.sport.entity.GameResult">
INSERT INTO
GAME_RESULT(PLAYERID, GAMEID, DATEOFGAME,GAMEMAPNAME,LEAGUE,AVERAGESPEED,GAMEDURATION,GAMECOMMENTARY)
VALUES(#{playerId},#{gameId},#{dateOfGame},#{gameMapName},#{league},#{averageSpeed},#{gameDuration},#{gameCommentary})
</insert>
</mapper>