Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
DelayQueue implementation in Java

I'm a beginner in understanding threads and concurrency package. Understood that to use java.util.concurrent.DelayedQueue we have to implement the java.util.concurrent.Delayed interface and it has a abstract method getDelay()

long getDelay(TimeUnit unit)

Returns the remaining delay associated with this object, in the given time unit.

@Override
public long getDelay(TimeUnit unit) {
    long diff = startTime - System.currentTimeMillis();  // long variable
    return unit.convert(diff, TimeUnit.MILLISECONDS);   // Not clear about this part which also returns long
}

All getDelay does is get the milliseconds difference which i believe we get from above diff variable then why do we need to use convert method? convert method says that diff needs to be converted to milliseconds (Why does it need to convert when it is already in milliseconds). TimeUnit is just for specifying that convert to milliseconds if my understanding is not wrong. Can anyone help me understand more on this topic.

EDIT-1

diff contains milliseconds difference and it is printed in the below screenshot console. Tried using convert method of Timeunit and also printed in the console below which are exactly same and are of long datatype. When both the values are same and long datatype why do i need to use this extra line of

unit.convert(diff, TimeUnit.MILLISECONDS);

instead i could also return the diff directly without using the convert method.

Console screenshot

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

unit is supplied by the caller of getDelay(). The implementation must return the delay in the time unit specified by unit.

Therefore, unit.convert(diff, TimeUnit.MILLISECONDS) converts diff, which is expressed in TimeUnit.MILLISECONDS, to unit.

TimeUnit.MILLISECONDS refers to what diff is. diff is then converted from TimeUnit.MILLISECONDS to unit.

Note that you must also implement compareTo() of Comparable, which Delayed extends.


The unit parameter is not fixed. It is supplied by the caller of the method, therefore it may vary. It may be TimeUnit.MILLISECONDS or TimeUnit.SECONDS or any other enum value. That's why you need to use the convert method.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda