Java Java Data Structures Efficiency! Custom Serialization

Nhat Anh Dao
Nhat Anh Dao
7,156 Points

Get confused with the code challenge

I don't know what i did wrong but it seem like i stuck at the last step in the code challenge, and it gave me Exception in thread "main" java.lang.OutOfMemoryError: Java heap space error. This is my code

 public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    csr = mSupportReps.poll();
    while(csr == null){
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(csr);
}

Somebody please help me out. Thank you !!

3 Answers

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

The only problem that you have is in the while loop try this

while ((csr = mSupportReps.poll()) == null) { // all in 1 line
    playHoldMusic();
}
Nhat Anh Dao
Nhat Anh Dao
7,156 Points

Now you point that, i just realize that my while loop gonna loop forever. Thanks a lot !

Colby Wise
Colby Wise
3,165 Points

Geovanie, can you please explain what this line of code doing? Why your formatting works but Nhat does not work? P.S. I saw something similar to your formatting in the video and did not understand it which is why i'm asking...

Another way I defined it, which was more intuitive to me was:

import java.util.ArrayDeque;
import java.util.Queue;

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

public CallCenter(Queue<CustomerSupportRep> queue) {
    mSupportReps = queue;
  }

 public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;

    while(mSupportReps  == 0){   //while queue is zero 
      playHoldMusic();
    }

   csr = mSupportReps.poll(); 
   csr.assist(customer);
    mSupportReps.add(csr);
}
Geovanie Alvarez
Geovanie Alvarez
21,500 Points

Well in the Nhat example the problem is going to loop forever because is never update the CustomerSupportRep csr variable in the while loop

 public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    csr = mSupportReps.poll(); // <-- here just update once
    while(csr == null){ // <-- here loop forever with the same csr variable poll data
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(csr);
}

that's why i post the code that is update the csr and the same time cheking if is null

while ((csr = mSupportReps.poll()) == null) { // <-- update the csr and the same time cheking if isnull
    playHoldMusic();
}

hope this help you i'm not very good explain things

Hi Geovanie Alvarez, ich just want to let you know, that you actually helped me very much by pointing out, why there was an infinity loop. So, thank you!!!