Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
Z
zhmes-agecal
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
zhmes-agecal
Commits
1e2e8666
Commit
1e2e8666
authored
Jan 09, 2026
by
wanghao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 机械臂 状态检查和指令完成之间存在竞态条件,影响界面显示 指令状态问题调整。
parent
4567dc04
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
21 deletions
+29
-21
NettyUdpServerHandler.java
...om/zehong/system/netty/handler/NettyUdpServerHandler.java
+29
-21
No files found.
zhmes-agecal-system/src/main/java/com/zehong/system/netty/handler/NettyUdpServerHandler.java
View file @
1e2e8666
...
...
@@ -56,7 +56,8 @@ public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramP
private
IRobotArmCommandService
robotArmCommandService
;
@Resource
private
ApplicationEventPublisher
eventPublisher
;
// 新增事件发布器
// 命令完成锁,用于确保命令完成时只有一个线程执行
private
final
Object
completionLock
=
new
Object
();
// 使用原子引用,确保状态变更的原子性
private
final
AtomicReference
<
CommandState
>
commandState
=
new
AtomicReference
<>(
CommandState
.
IDLE
);
...
...
@@ -228,12 +229,14 @@ public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramP
}
private
void
handleCompleteState
()
{
//
原子地获取并清空当前指令ID
//
只在完全空闲时处理
Long
commandIdToComplete
=
currentCommandId
;
if
(
commandIdToComplete
!=
null
)
{
// 尝试将状态从 EXECUTING 转为 COMPLETING
if
(
commandState
.
compareAndSet
(
CommandState
.
EXECUTING
,
CommandState
.
COMPLETING
))
{
synchronized
(
completionLock
)
{
// 再次检查,防止在获取锁期间状态已改变
if
(
commandIdToComplete
.
equals
(
currentCommandId
)
&&
commandState
.
compareAndSet
(
CommandState
.
EXECUTING
,
CommandState
.
COMPLETING
))
{
try
{
// 完成指令
robotArmCommandService
.
completeCommand
(
commandIdToComplete
);
...
...
@@ -245,18 +248,23 @@ public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramP
// 状态转为空闲
commandState
.
set
(
CommandState
.
IDLE
);
// 处理待执行指令
// 处理待执行指令(这里也加锁确保顺序)
synchronized
(
this
)
{
handleFullyIdleState
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
"指令完成失败: {}"
,
commandIdToComplete
,
e
);
commandState
.
set
(
CommandState
.
ERROR
);
}
}
}
}
else
{
// 没有当前指令,直接处理待执行
synchronized
(
this
)
{
handleFullyIdleState
();
}
}
}
/**
* 记录当前执行的指令
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment