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:


instead of:


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,

4 comentarios:

Arend v. Reinersdorff dijo...

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

Alternatively you could use the constants from Commons lang:

long delay = 5 * MILLIS_PER_MINUTE;

Alex Soto dijo...

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,

Libin Huang dijo...

20160509libinmulberry outlet store
true religion outlet
herve leger dresses
pandora jewelry
cartier watches
polo ralph lauren
cheap oakley sunglasses
tory burch outlet online
nfl jersey wholesale
swarovski jewelry
ray ban sunglasses
michael kors online
michael kors outlet
snapbacks wholesale
lebron james shoes
nike blazer pas cher
michael kors outlet online
mcm backpack
coach outlet
tory burch outlet online
michael kors handbags
calvin klein underwear
kate spade outlet
oakley sunglasses
michael kors outlet
louis vuitton handbags
true religion canada
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
gucci outlet online
michael kors outlet
air max 2015
nike air force 1
swarovski crystal
fitflops shoes
timberland shoes

chenlina dijo...

coach outlet
vans shoes sale
jordan 3 white cenment
michael kors outlet
jordan concords
ralph lauren
coach outlet
christian louboutin outlet
oakley outlet
cheap oakley sunglasses
fitflops sale clearance
louis vuitton outlet
christian louboutin shoes
adidas yeezy
oakley sunglasses
jordan 8
cheap toms
nike free run
jordan 4
oakley sunglasses
coach factory outlet
gucci outlet
louis vuitton handbags
ray ban sunglasses
jordan retro
louis vuitton outlet
air jordan pas cher
louis vuitton outlet
ralph lauren outlet
michael kors outlet
coach outlet
jordan retro
coach factory outlet online
coach outlet store online
burberry outlet
coach factory outlet
adidas yeezy 350
lebron 11
marc jacobs handbags
polo ralph lauren

Donate If You Can and Find Post Useful