Java多线程实现跑步比赛【SpringBoot——调用逻辑】

6 篇文章 0 订阅
4 篇文章 1 订阅
该博客详细介绍了使用Java多线程实现跑步比赛的全过程,包括地图映射类、运动员映射类、工具类和SpringBoot调用逻辑。PlayerInfoController负责接收请求并启动比赛,PlayerInfoService及其实现类处理比赛逻辑,通过PlayerInfoMapper查询所有运动员信息,并使用线程池并发执行比赛。
摘要由CSDN通过智能技术生成

文章链接

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值