对于我正在开发的多人游戏,我想将事件记录到 mysql 数据库而不阻塞游戏更新线程,这样如果数据库繁忙或表被锁定,游戏就不会停止运行等待写入。
实现这一目标的最佳方法是什么?
我正在使用 c3p0 来管理数据库连接池。到目前为止,我最好的想法是将查询更新字符串添加到同步列表中,并使用一个独立的线程每 100 毫秒检查该列表并执行它在那里找到的查询。
最佳答案
My best idea so far is to add query update strings to a synchronized list with an independent thread checking the list every 100ms and executing the queries it finds there.
如果你不想阻塞你的主线程,这对我来说听起来不错,除了不使用同步列表和 100ms 轮询,而是使用一些 BlockingQueue而是实现。 LinkedBlockingQueue应该完成这项工作,如果你让它不受限制,它永远不会阻塞你的主线程。
关于java - 用java非阻塞MySQL更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2840015/