martes, junio 26, 2012

Bye, Bye, 5 * 60 * 1000 //Five Minutes, Bye, Bye


Llueve, llueve, y mientras nos mojamos como tontos. LLueve, llueve, y en un simple charco a veces nos ahogamos. (Llueve - Melendi)

In this post I am going to talk about one class that was first introduced in version 1.5, that I have used too much but talking with some people they said that they didn't know it exists. This class is TimeUnit.

TimeUnit class represents time durations at a given unit of granularity and also provides utility methods to convert to different units, and methods to perform timing delays.

TimeUnit is an enum with seven levels of granularity: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS and SECONDS.

The first feature that I find useful is the convert method. With this method you can say good bye to typical:

     private static final int FIVE_SECONDS_IN_MILLIS = 1000 * 5;

to something like:

     long duration = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);

But also equivalent operations in a better readable method exist. For example the same conversion could be expressed as:

     long duration = TimeUnit.SECONDS.toMillis(5);

The second really useful sets of operations are those related with stopping current thread.

For example you can sleep current thread with method:

     TimeUnit.MINUTES.sleep(5);

instead of:

     Thread.sleep(5*60*1000);

But you can also use it with join and wait with timeout.

     Thread t = new Thread();
     TimeUnit.SECONDS.timedJoin(t, 5);

So as we can see TimeUnit class is though in terms of expressiveness, you can do the same as you do previously but in a more fashionable way. Notice that you can also use static import and code will be even more readable.

Keep Learning,

26 comentarios:

  1. Anónimo10:02 a. m.

    Thanks for the tip :-) Didn't know about TimeUnit.

    Alternatively you could use the constants from Commons lang:

    long delay = 5 * MILLIS_PER_MINUTE;

    ResponderEliminar
  2. Hi Arend thank you very much for reading my blog. I didn't know commons lang has this constants, thanks for the comment. The only disadvantage that I see is that you add a new dependency to your project.

    Thanks for the comment,
    Alex.

    ResponderEliminar

  3. This blog is a great source of information which is very useful for me.

    Jual Obat Aborsi Pekanbaru

    Jual Obat Aborsi Bekasi

    Jual Obat Aborsi malang

    Jual Obat Aborsi

    Obat Aborsi semarang

    Obat Aborsi yogyakarta

    Obat Aborsi solo

    Obat Aborsi

    -Can be very slow but shows all backlinks along with their PR, Anchor and if it's a Nofollow

    ResponderEliminar
  4. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  5. https://www.wattpad.com/664416225-jual-obat-kuat-di-jakarta-0852-9333-9996-jual-obat/page/4

    ResponderEliminar
  6. nice share thanks,situs togel Singapura prize terbaik dan terpercaya hanya ada disakuratogel,raja nya bandar togel online,kemenangan berapa pun pasti akan dibayar 100%.diskon tertinggi disemua pasaran,bukan bandar kaleng-kaleng,
    klik link dibawah untuk bergabung bersama sakuratogel:
    http://sakuratogel.xyz/

    ResponderEliminar
  7. PUSAKATOGEL merupakan bandar togel singapore yang aman dan terpercaya di seluruh negeri Indonesia karena telah terbukti sebagai salah satu bandar togel online terbesar dan terbaik dengan memberikan hadiah kemenangan terbesar dan diskon tertinggi.
    togel singapore
    togel sgp
    togel sgp
    togel sgp
    togel singapura
    singapore prize
    hongkong prize

    ResponderEliminar
  8. QQOKBET merupakan Bandar qq slot Online Terbaik Terpercaya Indonesia yang selalu menghadirkan berbagai jenis game slot qq online terbaru dan terbaik yang sangat digemari oleh para penggemar slot online di Indonesia dengan memberikan hadiah jackpot terbesar dan tertinggi yang mencapai hingga ratusan juta rupiah.
    qq slot
    slot qq
    slotqq
    slot qq
    slotqq
    slot qq
    slotqq
    qq slot

    ResponderEliminar
  9. Atmqq.org merupakan salah satu bandar kiu online dan poker online terbaik dan terpercaya di seluruh Indonesia dengan presentase kemenangan terbesar dan tertinggi hingga 90% serta menjunjung tinggi nilai Fair Play serta murni melawan sesama pemain server Poker-V tanpa boot sama sekali.

    bandar kiu
    bandarq

    ResponderEliminar
  10. Hongkong prize merupakan sebuah situs yang menceritakan mengenai sejarah dan asal usul togel Hongkong sehingga menjadi salah satu permainan togel yang menjadi sangat digemari oleh para penggemar togel di negara Indonesia. Selain itu kami juga merokomendasikan Sakuratogel yang merupakan salah satu agen togel hongkong terbaik dan terpercaya yang telah terbukti sebagai bandar togel online terbesar dan terbaik di seluruh negeri Indonesia.

    Hongkong Prize
    Singapore Prize

    ResponderEliminar
  11. Este comentario ha sido eliminado por el autor.

    ResponderEliminar