需要有关比赛条件的建议 [英] Need advice regarding race condition

查看:72
本文介绍了需要有关比赛条件的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我目前正在调试船舶监控系统.在该应用程序中,我发现了所有的bug,幸运的是,我可以解决其中的每一个bug,除了这个bug.所以请帮帮我.

解释如下:
该错误位于分发服务器中,我怀疑(不,我确定)在接收器类(将被称为Receiver)和发送器类(将被解决)之间存在极端的竞争条件.作为发件人).

分发服务器的工作方式如下:
1.接收器接收数据.
2.接收方将数据写入数据存储类(将寻址为DataStorage).
3.发送方检查DataStorage是否为空.
4.如果DataStorage不为空,发件人将继续发送数据.
或者他们认为.

我到目前为止发现的问题:
1.保证的比赛条件使用标志导致发件人检查.
好的,我已经对发件人进行了锁定.
2.我无法安全释放发件人的锁.
为什么?这很简单,真的.这是因为接收器充当接口.当jar文件(称为QuickServer)调用onDataReceived方法时,它将接收数据.

所以在这里,我正在寻找有关此事的一些建议.到目前为止,我发现的唯一解决方案是:
1.收到新数据时,请释放发件人的锁.
2.发件人将引发一个标志,以挂起任何接收方的传入数据.
3.发送方等待接收方的任何未完成的操作(Thread.sleep(500)?).
4.发件人获取所有数据.
5.发送方拉下该标志.
6.接收方开始其暂停的操作,发送方处理并发送数据.
7.每个人都和睦相处,直到发送数据后重新应用发件人的锁为止,因此返回到步骤1.

[edit]我的解决方案失败,[/edit]还有其他解决方案吗?我是一所职业高中的应届毕业生,因此我正在寻找一些经验丰富的专家解决方案.


预先感谢,

Firo AV

Hi all,

I''m currently debugging a ship monitoring system. In that application I found whole heaps of bugs, fortunately I can solve every single one of them, except this one. So please help me.

Here''s the explanation:
The bug is located within a distribution server, and I suspect (No, I''m sure of it) that there''s an extreme race condition between the receiver class (will be addressed as Receiver) and sender class (will be addressed as Sender).

Distribution server works like this:
1. Receiver receive data.
2. Receiver write data into a data storage class (will be addressed as DataStorage).
3. Sender check whether DataStorage is empty or not.
4. If DataStorage is not empty, Sender will proceed to send the data.
Or so they think.

Problems I found so far:
1. A guaranteed race condition ''cause Sender check using a flag.
Okay, I''ve applied a lock into Sender.
2. I can''t safely release Sender''s lock.
Why? it''s simple, really. It''s because receiver works as interface. It receive data when onDataReceived method is called by a jar file (it''s called QuickServer).

So here I am, looking for some advice regarding this matter. The only solution I''ve found so far is:
1. Release Sender''s lock when a new data is received.
2. Sender will raise a flag to suspend any Receiver''s incoming data.
3. Sender wait for any unfinished operation by Receiver (Thread.sleep(500)?).
4. Sender grab all data.
5. Sender pull down the flag.
6. Receiver start it''s suspended operation, Sender process and send the data.
7. Everyone live in harmony till Sender''s lock is re-applied after sending data therefore go back to step1.

[edit]My solution failed,[/edit]Is there any other solution? I''m a fresh graduate from a vocational high school therefore I''m looking for some experienced expert''s solution.


Thanks in advance,

Firo A.V.

推荐答案

您需要使用等待并通知 [^ ]结构.
只需简单地将发送者进入空队列时,它进入等待状态,而当接收者向队列中添加某些东西时,它会发出通知.

如果所有发送方和接收方都在同一个JVM中运行,则可以通过将所有逻辑放在同步块中并使用notifyAll()来唤醒所有线程,而不是通过notify()来唤醒,从而确保有人获得了锁.最多只能有一个线程.
You need to use a wait and notify[^] structure.
Simply put the when the sender gets to an empty queue, it goes into a wait state, and when the receiver adds something to the queue it notifies.

If all the Senders and Receivers are running within the same JVM, then you can ensure that someone gets the lock by putting all the logic in Synchronized blocks and using notifyAll() to wake up ALL the threads, rather than notify() which will wake up one and only one thread.


您可以从这里获得一些启发: http: //en.wikipedia.org/wiki/Producer-consumer_problem [ ^ ]

在这里,您的接收者是生产者,而发送者是消费者.
You can get some inspiration from here: http://en.wikipedia.org/wiki/Producer-consumer_problem[^]

Here your Receivers are the Producers and your Sender is a Consumer.


这篇关于需要有关比赛条件的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆