简体   繁体   中英

Why is TimeUnit a member of java.util.concurrent?

Java's TimeUnit enum is useful for many different tasks related to time, not just concurrency; and other time-related classes like Date are part of java.util . So why is TimeUnit a member of java.util.concurrent ?

It is probably there for historical reasons:

  • the legacy date API has not evolved much over the past few years
  • TimeUnit is widely used in the concurrency utilities

Interestingly, the new date API in Java 8 has a ChronoUnit enum which is similar to the TimeUnit enum, but applied to dates and times. In particular, a ChronoUnit can be converted to a Duration .

As well as providing methods to work with different granularities of time, TimeUnit provides Thread-aware methods like timedJoin :

public void timedJoin(Thread thread, long timeout) throws InterruptedException

I think the class originated to help with common concurrent programming tasks like delayed execution. As it stands it has two distinct functions (manage time granularities; apply temporal concepts to threading problems), which violates the single responsibility principle .

Given the utility and popularity of TimeUnit outside concurrent code, a neater solution could be a java.util.TimeUnit enum to manage time granularities and a separate java.util.concurrent class for its applications in threading.

TimeUnit was developed by the Concurrency expert group for the concurrency utilities primarily. Such libraries are often developed using a typical 3rd party package outside the java. ... packages to allow testing with existing JVMs. Later they are integrated by renaming the packages. So it is a historical reason, mostly.

I believe it is more of a design decision. They added it when they needed it for concurrency. Hence, they put it under the concurrent package. Yes, we can use it even when concurrency is not involved. But it was a necessity in concurrent applications. Well, I can be wrong.

"A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units."

I guess this is because they wanted to write intuitive code for concurrency. The TimeUnit JavaDoc says:

A TimeUnit is mainly used to inform time-based methods how a given timing parameter should be interpreted. For example, the following code will timeout in 50 milliseconds if the lock is not available:

  Lock lock = ...; if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ... 

So, I guess that they were writing the Lock class, and wanted such a way to specify time. For that reason, they created an auxiliary class TimeUnit which was originally only intended to use for their Lock code and friends. Over time, it probably got abused for general purposes.

The JavaDoc for the java.util.concurrent says:

Utility classes commonly useful in concurrent programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM