Java 线程无法与链表正常工作

发布于 2024-08-27 19:51:58 字数 2083 浏览 6 评论 0原文

你好,我正在研究理发师睡觉的问题。此外,当他们到达时,他们会排在队伍的前面,并且是下一个理发的人。

我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

束缚m 2024-09-03 19:51:59

您必须使用同步集合:

Collections.synchronizedList(List<Object> list)

此方法根据参数列表返回同步列表实例

you must use synchronized collections:

Collections.synchronizedList(List<Object> list)

this method returns synchronized List instanse based on parameter list

是你 2024-09-03 19:51:59

我很确定逻辑错误就在这里:

  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);
  } 

是的,避免复制/粘贴编程!

I'm pretty sure the logical mistake is here:

  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);
  } 

And yes, avoid copy/paste programming!

写给空气的情书 2024-09-03 19:51:59

每当您修改列表时,您可能应该将该代码放在同步块上。

一种方法是:

synchronized(customer_list) {
 customer_list.addFirst(c);
}

另一种方法,这样您就不必到处编写代码:

public synchronized void addCustomerAtStart(Customer c) {
 customer_list.addFirst(c);
}

然后替换该函数。

当你执行 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:

synchronized(customer_list) {
 customer_list.addFirst(c);
}

Another, so that you don't have to code that all over the place:

public synchronized void addCustomerAtStart(Customer c) {
 customer_list.addFirst(c);
}

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.

╰◇生如夏花灿烂 2024-09-03 19:51:59

您的代码将客户添加到列表(开始或结束),然后从列表中删除客户。该列表最多只包含一名客户,因此客户将按照添加顺序进行处理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文