简体   繁体   English

具有共享资源的多线程服务器

[英]Multi-Threading Server with shared Resource

I'm trying to make a simple game in Java. 我正在尝试用Java做一个简单的游戏。 I want to code a server that accepts multiple players. 我想编码一个可以接受多个玩家的服务器。 Here is the meta of the game: 这是游戏的内容:

A player connects to the server to play a game, and indicate a number. 玩家连接到服务器以玩游戏并指出一个数字。 Each player plays one game only. 每个玩家只能玩一个游戏。 The game only starts or resolve if 4 players get connected. 只有连接了4个玩家,游戏才开始或解决。 If the sum of all players number is greater than 21 all lose, else, all win. 如果所有玩家人数之和大于21,则全部输掉,否则,全部赢。 After the game resolves each player must be warned if it won or lost. 游戏解决后,必须警告每位玩家输赢。

The algorithms for the win/lose check, or accept multiple clients I get it. 获胜/失败检查的算法,或接受多个客户的算法。 My doubt is what should be a thread (or a runnable object) and/or what is a shared resource. 我的疑问是什么应该是线程(或可运行的对象)和/或什么是共享资源。 Just a few guide lines and so I can implement this. 只是一些指导方针,所以我可以实现这一点。

What i have understood from your statement is that you need exact 4 players to decide game's outcome. 我从您的陈述中了解到,您需要确切的4名球员来决定游戏的结果。 each player passes a number while connecting to server and he/she is not allowed to enter or pass anything more. 每个玩家在连接服务器时都会传递一个数字,并且他/她不能再输入或传递其他任何信息。

so the shared resources are TOTAL SUM and NUMBER OF CONNECTION and thread is connection to server . 所以共享资源是TOTAL SUMNUMBER OF CONNECTION ,线程是到服务器的连接 So whenever a player requests to connect you, start a new thread and this thread increment the shared TOTAL SUM and NUMBER OF CONNECTION and after incrementing also check the value of total sum and no. 因此,只要有玩家要求与您建立连接,就启动一个新线程,该线程将增加共享的TOTAL SUMNUMBER OF CONNECTION,并在增加后还检查total sum和no的值。 of connection. 连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM