首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
软件与程序人生
›
后端开发
›
Java
›
MAUI 已知问题 PathFigureCollectionConverter 非线程安 ...
返回列表
发新帖
MAUI 已知问题 PathFigureCollectionConverter 非线程安全
[复制链接]
发表于 2023-1-9 22:09:24
|
显示全部楼层
|
阅读模式
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
在 MAUI 里,可以使用 PathFigureCollectionConverter 将 Path 字符串转换为 PathFigureCollection 对象,从而实现从 Path 字符串转换为路径几何。然而这个 PathFigureCollectionConverter 转换器非线程
安全
,即使创建多个实例对象,依然由于内部的静态字段导致非线程
安全
,本文将记录此问题的原理
当前,此 PathFigureCollectionConverter 非线程
安全
问题,没有规避方法
我将此问题报告给官方,请看
https://github.com/dotnet/maui/issues/11321
然后官方说,你也是 Member 了,是一个成熟的开发者了,自己发现的问题就要自己修。于是我乖乖修了这个问题,请看
https://github.com/dotnet/maui/pull/11497
修了之后发现
性能
居然还提升了一点
复现步骤:
只需要让 PathFigureCollectionConverter 进入多线程转换 Path 字符串即可,转换过程,将随机抛出异常
最简
代码
如下
var pathFigureCollectionConverter = new PathFigureCollectionConverter();
var pathString1 =
"M320 96a64 64 0 0 0-64 64V896a64 64 0 0 0 64 64h384a64 64 0 0 0 64-64V160a64 64 0 0 0-64-64H320z m384 64V896H320V160h384zM128 256v576h64V256H128zM832 256v576h64V256h-64z";
var pathString2 =
"M230.08 738.56l58.816 58.88L574.336 512l-285.44-285.44-58.88 58.88L456.704 512l-226.56 226.56z m487.04 29.44V256h-83.2v512h83.2z";
_ = pathFigureCollectionConverter.ConvertFrom(pathString1);
_ = pathFigureCollectionConverter.ConvertFrom(pathString2);
for (int i = 0; i < 100; i++)
{
Task.Run(() =>
{
var pathFigureCollection = pathFigureCollectionConverter.ConvertFrom(pathString1);
});
}
for (int i = 0; i < 100; i++)
{
Task.Run(() =>
{
var pathFigureCollection = pathFigureCollectionConverter.ConvertFrom(pathString2);
});
}
复制
代码
包含此
代码
的项目放在 GitHub 上,请看
https://github.com/lindexi/lindexi_gd/tree/abc3042ddbfc3bd46563119fc88df0463b155c8b/TestPathFigureCollectionConverter
导致 PathFigureCollectionConverter 非线程安全的核心原因是采用静态字段记录状态,以下是 PathFigureCollectionConverter 的部分源代码,代码有删减
static bool _figureStarted;
static string _pathString;
static int _pathLength;
static int _curIndex;
static Point _lastStart;
static Point _lastPoint;
static Point _secondLastPoint;
static char _token;
static void ParseToPathFigureCollection(PathFigureCollection pathFigureCollection, string pathString, int startIndex)
{
PathFigure pathFigure = null;
_pathString = pathString;
_pathLength = pathString.Length;
_curIndex = startIndex;
// 忽略其他代码
}
复制代码
完全的代码请看
https://github.com/dotnet/maui/blob/a541df0816d1867f494186a0bdc214d431e000cd/src/Controls/src/Core/Shapes/PathFigureCollectionConverter.cs
从上面代码可以看到,用到了静态字段。这是非多线程安全的,多个线程将会随机更改污染静态字段,从而让转换逻辑无法成功执行
回复
使用道具
举报
返回列表
浏览过的版块
物联网
移动端开发
前端开发
人工智能
SAAS
云原生
自由的羽毛
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表