经典的 Java 同步问题(想尝试一下吗?)
我正在尝试解决 Java 同步问题。情况如下:
有类 Person,他们想要配对。所以我有一个耦合类来进行配对。当一个人走进耦合而没有人在等她时,她就开始等待。并等到有人出现或她感到无聊并离开(预定计时器关闭)。
如果她走进去发现有人在等她,他们就会立即联系起来,交换电话号码,然后分道扬镳。 (用对方的信息继续执行。)
同一个人不能带着两个人离开。
我保证这不是我试图作弊的大学练习。:)我只是有一段时间没有做过这些事情了,而且我对此有点生疏。
这是我一开始想到的,所以线程试图设置 Person,如果这不起作用,它将得到 false 作为返回值。然后线程得到服务员。由于显而易见的原因,这将不起作用(调用之间可能会出现另一个线程)以及我将如何向等待线程发出信号以继续进行。
这是我提到的代码:
public class Coupling {
private static volatile Person waitingPerson = null;
public static synchronized Integer getWaitingPerson() {
Integer temp = waitingPerson;
waitingPerson = null;
return temp;
}
public static synchronized Boolean setWaitingPerson(Integer waitingPerson) {
if (waitingPerson == null){
syncro.waitingPerson = waitingPerson;
return new Boolean(true);
}
else
return new Boolean(false);
}
I'm trying to solve a Java synchronization problem. The case is as follows:
There are class Persons and they want to be paired. So I have a class Coupling that does the pairing. When a person walks in to the Coupling and there is no one waiting for her, she starts to wait. And waits until someone comes along or she gets bored and leaves (predifines timer goes off).
If she walks in and finds someone waiting for her, they are immediately coupled and they exchange phone numbers and go their separate ways. (Continue execution with the other's information.)
The same person can't leave with two people.
I promise that this is not a university exercise I'm trying to cheat in. :) I just havn't done this stuff in a while and I'm a bit rusty at it.
This is something I came with up at first, so the Thread is trying to set the Person, and if this doesn't work it'll get false as return value. Then the thread gets the waiter. For obvious reasons, this will not work (another thread might come between the calls) and how would I signal the waiting thread to go on.
Here's the code I mentioned:
public class Coupling {
private static volatile Person waitingPerson = null;
public static synchronized Integer getWaitingPerson() {
Integer temp = waitingPerson;
waitingPerson = null;
return temp;
}
public static synchronized Boolean setWaitingPerson(Integer waitingPerson) {
if (waitingPerson == null){
syncro.waitingPerson = waitingPerson;
return new Boolean(true);
}
else
return new Boolean(false);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您真的只是想了解这些内容并且这就是您的示例问题,请尝试获取 Java 并发实践< /a>,并亲自找出答案。这是一本很棒的书。
If you really just want to get into this stuff and this is your example problem, try to get a copy of Java Concurrency in Practice, and find out for yourself. Its a great book.
好吧,我会稍微修改一下你的问题并提出一个解决方案,然后你会告诉我你是否满意:-)
首先,你是否考虑过使“配对”操作异步?它的工作原理非常像这样:
这在代码中如何运作?那么,您需要两件事:
因此,每次一个人想要配对时,它都会通知耦合线程,该线程添加将人员添加到其“自由人员”列表中,并从信号量中释放一个许可证。另一方面,耦合线程不断尝试获取信号量许可,一次两个。由于只有当一个人留下通知时才会释放许可证,因此拥有两个许可证意味着两个人想要配对。
好吧,少说话,多写代码。这是 Coupling 线程类:
和 Person 类:
Okay, I'll mangle your problem a little bit and propose a solution, then you'll tell me if you're satisfied :-)
First, have you considered making the "pairing" operation asynchronous? It would work pretty much like this:
How would that work in code? Well, you'd need two things:
So, every time a Person wants to be paired, it notifies the Coupling thread, that adds the Person to its "free people" list and releases one permit from the semaphore. The Coupling thread, on the other hand continuously try to acquire semaphore permits, two at a time. Since a permit is only released when a Person leaves a notification, having two permits means that two people want to be paired.
Well, less talk, more code. Here's the Coupling thread class:
And the Person class:
如何不这样做。我在最近处理了类似的测试问题后整理出了粗略的解决方案。存在以下评论中指出的问题。
我故意忽略了 Person 类的任何细节(不确定你的 Integer temp 应该做什么)。
How not to do it. Rough solution I put together after tackling something similar for a test recently. Has issues as noted in a comment below.
I've intentionally ignored any specifics of the Person class (not sure what your Integer temp was supposed to do).
一个简单的解决方案
我假设这些人没有性取向。 ;)
我假设
Person
无法变成Integer
您谈到了等待,但您不在任何地方等待。 ;)
使用 BlockingQueues 实现起来会简单得多。
顺便说一句:永远不要使用
new Boolean(true)
或new Boolean(false)
事实上,没有理由不使用boolean
A simple solution
I assume that these Persons have no sexual preference. ;)
I assume a
Person
cannot turn into anInteger
You talked about waiting but you don't wait anywhere. ;)
This would be much simpler to implement using BlockingQueues.
BTW: Never use
new Boolean(true)
ornew Boolean(false)
in fact there is no reason not to useboolean