Java 线程无法与链表正常工作
你好,我正在研究理发师睡觉的问题。此外,当他们到达时,他们会排在队伍的前面,并且是下一个理发的人。
我使用的是linkedlist
,如果我看到优先客户,我会将他放在列表的开头,如果客户不是优先客户,他会转到列表的末尾。然后我调用 WantHaircut 方法来获取列表的第一个元素。
我的问题是,客户按照到达的顺序进行处理,而优先客户必须等待。这是发生这一切的代码。有什么想法我做错了吗?谢谢
public void arrivedBarbershop(Customer c){
if(waiting < numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - SITTING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting >= numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - STANDING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting < numChairs && !c.isPriority()){
waiting++;
System.out.println("Customer " + c.getID() + ": arrived, sitting in the waiting room");
customer_list.addLast(c);
customers.up(); // increment waiting customers
}
else if(waiting >= numChairs && !c.isPriority()) {
System.out.println("Customer " + c.getID() + ": went to another barber because waiting room was full - " + waiting + " waiting");
mutex.up();
}
if(!customer_list.isEmpty()){
this.wantHairCut(customer_list.removeFirst());
}
}
public void wantHairCut(Customer c) {
mutex.up();
barber.down(); // waits for being allowed in barber chair
System.out.println("Customer " + c.getID() + ": getting haircut");
try {
/** haircut takes between 1 and 2 seconds **/
Thread.sleep(Barbershop.randomInt(1, 2) * 1000);
} catch (InterruptedException e) { }
System.out.println("Barber: finished cutting customer " + c.getID() + "'s hair");
c.gotHaircut = true;
cutting.up(); // signals cutting has finished
/** customer must pay now **/
this.wantToCashout(c);
}
Hi I am working on the sleeping barber problem. with the addition of having priority customer when they arrive they go in the front of the line and they are the next ones to get a haircut.
I'm using a linkedlist
and if I see a priority customer I put him in the beginning of the list, if the customer is not priority he goes to the end of the list. then I call the wantHaircut method getting the first element of the list.
my problem is that the customer are being processed in the order they arrive, and the priority customer have to wait. here is the code where it all happens. any ideas what I am doing wrong? thanks
public void arrivedBarbershop(Customer c){
if(waiting < numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - SITTING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting >= numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - STANDING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting < numChairs && !c.isPriority()){
waiting++;
System.out.println("Customer " + c.getID() + ": arrived, sitting in the waiting room");
customer_list.addLast(c);
customers.up(); // increment waiting customers
}
else if(waiting >= numChairs && !c.isPriority()) {
System.out.println("Customer " + c.getID() + ": went to another barber because waiting room was full - " + waiting + " waiting");
mutex.up();
}
if(!customer_list.isEmpty()){
this.wantHairCut(customer_list.removeFirst());
}
}
public void wantHairCut(Customer c) {
mutex.up();
barber.down(); // waits for being allowed in barber chair
System.out.println("Customer " + c.getID() + ": getting haircut");
try {
/** haircut takes between 1 and 2 seconds **/
Thread.sleep(Barbershop.randomInt(1, 2) * 1000);
} catch (InterruptedException e) { }
System.out.println("Barber: finished cutting customer " + c.getID() + "'s hair");
c.gotHaircut = true;
cutting.up(); // signals cutting has finished
/** customer must pay now **/
this.wantToCashout(c);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用同步集合:
此方法根据参数列表返回同步列表实例
you must use synchronized collections:
this method returns synchronized List instanse based on parameter list
我很确定逻辑错误就在这里:
是的,避免复制/粘贴编程!
I'm pretty sure the logical mistake is here:
And yes, avoid copy/paste programming!
每当您修改列表时,您可能应该将该代码放在同步块上。
一种方法是:
另一种方法,这样您就不必到处编写代码:
然后替换该函数。
当你执行 customer.up() 时,同样的事情,看起来它也需要同步,否则你可能会得到很多不一致的行为。
你甚至不需要使用“mutex.up()”
提示:如果您必须同步线程,您可能需要看看 CountDownLatch,它非常易于使用,对于线程同步非常有效。
whenever you modify your list, you should probably put that code on a synchronized block.
One way to do it:
Another, so that you don't have to code that all over the place:
and then substitute that function.
Same thing when you do customer.up(), that looks like it needs to be synchronized as well, otherwise you might be getting a lot of inconsistent behavior.
You shouldn't even need to use that "mutex.up()"
Tip: If you have to synchronize threads, you might want to take a look at CountDownLatch, very easy to use, works solidly for thread synchronization.
您的代码将客户添加到列表(开始或结束),然后从列表中删除客户。该列表最多只包含一名客户,因此客户将按照添加顺序进行处理。
Your code adds a customer to the the list (begin or end) then removes a customer from the list. The list only ever contains one customer at most, so customers are processed in the order they are added.