如何检查 ArrayBlockingQueue 是否被锁定或队列元素当前由线程处理?
您好,我很好奇是否有办法检查 ArrayBlockingQuery 查询当前是否被锁定? 原因:我有一个服务器,它监听套接字,接收参数,处理它们,然后将一些结果返回给客户端。该服务器(假设是服务器 A)在处理参数时与其他服务器(服务器 B)打开会话,然后将信息发送到 B。连接到 B 时只能创建一个会话,但 A 可以接受多个客户端连接。因为 ArrayBlockingQueue 用于存储参数,而其他一些线程通过队列运行并将它们一一发送到 B。
我需要执行该功能 - 当 ArrayBlockingQueue 为空并且当前时刻没有处理任何内容并且客户端数据发送到然后 A 立即处理 - 将数据发送到 B 服务器,处理来自 B 的响应,然后如果 ArrayBlockingQuery 不为空并且处理了某些内容,则 A 将响应返回给客户端,然后只需将参数添加到队列并将其他响应从 A 发送到客户端。这就是为什么我需要知道是否有办法检查 ArrayBlockingQueue 是否被锁定或队列元素当前由线程处理。
谢谢你!
Greetings, I am curious if there is someway to check if ArrayBlockingQuery query is currently locked ?
The reason for this : I have a server which is listens to a socket, receives parameters, process them and then returns some result to client. That server(let say it is server A) while processing parameters open session with other server( server B) and then send info to B. Only one session can be created when connecting to B, but A can accept several client connection. Because of that ArrayBlockingQueue is used to stored that parameters and some other thread run through the queue and send them one by one to B.
I need to do that functionality - when ArrayBlockingQueue is empty and nothing is processed in current moment and client data send to A then process immediately - send data to B server, process response from B and then A return response to client if ArrayBlockingQuery is not empty and something is processed then just add parameters to queue and send other response from A to client. Thats why i need to know if there is a way to check if ArrayBlockingQueue is locked or queue element currently processed by thread.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有什么理由不只使用锁,因为它是为了保护一次只能在一个线程中使用的资源而设计的。
这确保服务器 B 一次仅在一个线程中使用,并且您不需要向队列添加任何内容。
不确定 ArrayBlockingQuery 是什么,谷歌也不知道。您的意思是 ArrayBlockingQueue 吗?
ArrayBlockingQueue 不会锁定很长时间,因此此信息不是很有用。但是您可以通过反射获得锁。
您似乎过度考虑了您的解决方案。
您希望确保服务器 B 一次仅获得一个连接,并且您正在使用队列来执行此操作,但是您想绕过这一点,但这意味着放弃一次一个的原则。
Is there any reason you don't just use a Lock as this is designed for protecting resources which can only be used in one thread at a time.
This ensures server B will only be use in one thread at a time and you don't need to add anything to a queue.
Not sure what a
ArrayBlockingQuery
is, nor does google. Do you meanArrayBlockingQueue
?An ArrayBlockingQueue is not locked for very long, so this information isn't very useful. However you could obtain the lock via reflection.
You appear to be over thinking your solution.
You want to ensure that server B only get a connection one at a time, and you are using a queue to do this, however you want to way to by-pass this, but this means discarding your one at a time principle.
我建议不要实施您所描述的内容。这会使代码变得更加复杂,并且会出现竞争条件,除非您同步大型部件,并真正考虑您在做什么。最好始终以相同的方式处理所有事件,即始终排队,即使“工作线程”空闲。然后它将出列并照常工作。
但是,您的问题可以通过让“工作线程”更新/使用 信号量。然后您可以检查信号音是否已获取。 (通过检查可用许可证)
I would advice against implementing what you described. It would make the code a lot more complex, and it will open up for race conditions unless you synchronize big parts, and really think about what you are doing. It's much better to always handle all events in the same way, that is always queue, even if the "worker thread" is idle. It will then dequeue and work as usual.
Your problem can however be solved by having the "worker thread" updating/using a Semaphore. You can then check if the semaphone has been acquired or not. (By checking available permits)
如果您指的是 ArrayBlockingQueue,它有一个(非阻塞)
peek()
方法If you mean
ArrayBlockingQueue
, it has a (nonblocking)peek()
method which