当前位置:网站首页>关于玩家身上有个普通Set并发安全的讨论
关于玩家身上有个普通Set并发安全的讨论
2022-06-11 02:43:00 【zfoo-framework】
SceneManager.java
Map<Long, SceneRole> allRolesMap = new ConcurrentHashMap();
Scene.java
Map<Long, SceneRole> humanObjects = new ConcurrentHashMap();
在每次玩家切换地图时,都会从全局的SceneManager中拷贝SceneRole到Scene中,之后的修改,也都是发的消息包装为Task到这个场景线程中,从而进行修改这个SceneRole上的普通的Set对象,因此不会有线程安全问题。
再看下面的例子:
package org.example.testHashSet;
import com.google.common.collect.Maps;
import lombok.Data;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main2 {
public static Map<Integer, A> map = Maps.newConcurrentMap();
public static A getA() {
return map.get(1);
}
public static void main(String[] args) throws Exception {
final int NUM = 100;
map.put(1, new A());
ExecutorService logicThread = Executors.newFixedThreadPool(10);
for (int j = 0; j < 100000; j++) {
for (int i = 0; i < NUM; i++) {
// 模拟可以有多个线程访问,但是同一时刻只能有一个线程能访问到(比作是切换场景)
// 虽然num是一个普通的值,但是由于没有并发访问,因此依然是没啥问题
CountDownLatch lock = new CountDownLatch(1);
logicThread.submit(() -> {
getA().add();
lock.countDown();
int a = 0;
});
lock.await();
}
int r = getA().getValue() % NUM;
if (r != 0) {
System.err.println(r);
}
}
logicThread.shutdown();
}
@Data
public static class A {
private int num;
public void add() {
num++;
}
public int getValue() {
return num;
}
}
}
边栏推荐
- 那些笑着离开“北上广”的人,为何最后都哭了?
- WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
- 微信模版消息errcode“:40165,“errmsg“:“invalid weapp pagepath
- AOSP ~ logcat chatty line expired
- List filtering, sorting, verification and other processing methods
- Construction of Flink development environment and wordcount
- OpenJudge NOI 1.13 18:Tomorrow never knows?
- Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
- The new colleague asked me what "where 1=1" means???
- AOSP ~ 修改WebView默认实现
猜你喜欢

Go语言之Go 快速入门篇(一):第一个 Go 程序

. Net module and assembly - NET Module vs Assembly

Three special data types, day3 and redis (geographic location, cardinality statistics and bitmap scene usage)

CPT 102_ LEC 16

The two request sessionids of the same user are inconsistent ----- record the problem

Wechat applet

pip 安装 qt5 。

CPT 102_ LEC 15

CPT 102_LEC 17

CocosCreator原生二次开发的正确姿势
随机推荐
The solution of invalid @data annotation in idea2018
Helm deploy traifik ingress
数据库唯一索引和普通索引的区别?
Go quick start of go language (I): the first go program
DNS Optimization Practice of APP network optimization
[interview question 17.04. missing numbers]
Cygwin reports an error child_ info_ fork::abort: XXX. dll: Loaded to different address: parent(XXX) != child(XXX)
Wechat applet
Prophet
AOSP ~ 修改默认音量
Recent learning and update plan
js 内存泄漏
第七章 常用的协议简介(1)
How to ensure reliable delivery of messages?
. Net module and assembly - NET Module vs Assembly
OpenJudge NOI 1.13 17:文字排版
CPT 102_LEC 17
Go 语言的优势和学习路线图
CPT 102_ LEC 16
CPT 102_LEC 15