Recent work in contact with Delayqueue, online search for information when found an article about the Delayqueue pit. Click to open link
The article has summed up the place where the pits were encountered, and there are solutions. But my first glance did not understand why, so turned over the source to dig a bit, below the cause of the pit and the principle of analysis.
The first is the take () method of Delayqueue:
1 PublicE Take ()throwsinterruptedexception {2 FinalReentrantlock lock = This. Lock;3 lock.lockinterruptibly ();4 Try {5 for (;;) {6E first =Q.peek ();7 if(First = =NULL)8 available.await ();9 Else {Ten LongDelay = First.getdelay (nanoseconds);//1 One if(Delay <= 0) A returnQ.poll (); -First =NULL;//don ' t retain ref while waiting - if(Leader! =NULL) the available.await (); - Else { -Thread Thisthread =Thread.CurrentThread (); -Leader =Thisthread; + Try { -Available.awaitnanos (delay);//2 +}finally { A if(leader = =thisthread) atLeader =NULL; - } - } - } - } -}finally { in if(leader = =NULL&& Q.peek ()! =NULL) - available.signal (); to Lock.unlock (); + } -}
First see note 2, which is an await method with time, the time unit is nanosecond, and the passed parameter delay is obtained from Comment 1 by calling the Getdelay method of the first object. The first object is of type E, and E is a generic type that implements the delayed interface.
Here to see the source of the interface delayed:
1 Public InterfaceDelayedextendsComparable<delayed> {2 3 /**4 * Returns The remaining delay associated with this object, in the5 * Given time unit.6 *7 * @paramUnit the time unit8 * @returnThe remaining delay; zero or negative values indicate9 * That's the delay has already elapsedTen */ One Longgetdelay (timeunit unit); A}
There is only one getdelay (Timeunit) method, which returns the length of time specified by the Timeunit. Obviously, the implementation of the specific class to implement the method is the only line.
So take a look at the specific Getdelay (Timeunit) method of implementation, I read a few articles, basically similar, are as follows:
1 Public Long getdelay (timeunit unit) {2 return Unit.convert (this. Expire- System.currenttimemillis (), timeunit.milliseconds); 3 }
The original blogger was very attentive to the reminder that this place is the second parameter of the Convert method, You should use Timeunit.milliseconds instead of timeunit.nanoseconds (although no matter what time unit you use will not cause the program to get the wrong result, but using the wrong time unit, the CPU will suffer). Then why do you have to emphasize the use of milliseconds this unit?
Continue to look at the source of the Convert method, in the Timeunit enumeration class, define a number of time units, they have the implementation of the respective Convert method, first look at the Timeunit.nanoseconds:
1 nanoseconds {2 Public LongTonanos (LongD) {returnD;}3 Public LongTomicros (LongD) {returnD/(c1/C0); }4 Public LongTomillis (LongD) {returnD/(c2/C0); }5 Public LongToseconds (LongD) {returnD/(c3/C0); }6 Public LongTominutes (LongD) {returnD/(c4/C0); }7 Public LongTohours (LongD) {returnD/(c5/C0); }8 Public LongTodays (LongD) {returnD/(c6/C0); }9 Public LongConvertLongD, Timeunit u) {returnU.tonanos (d);}Ten intExcessnanos (LongDLongm) {return(int) (D-(m*C2)); } One},
As you can see, the Convert method calls the Timeunit.tonanos method directly and returns the first parameter, D, as the length of time of the second nanosecond.
Similarly look at the method defined by Timeunit.milliseconds:
1 MILLISECONDS {2 Public LongTonanos (LongD) {returnX (d, C2/c0, max/(c2/c0)); }//static final Long C0 = 1L; static final Long C1 = C0 * 1000l;static final long C2 = C1 * 1000L;3 Public LongTomicros (LongD) {returnX (d, C2/c1, max/(c2/C1)); }4 Public LongTomillis (LongD) {returnD;}5 Public LongToseconds (LongD) {returnD/(c3/C2); }6 Public LongTominutes (LongD) {returnD/(c4/C2); }7 Public LongTohours (LongD) {returnD/(c5/C2); }8 Public LongTodays (LongD) {returnD/(c6/C2); }9 Public LongConvertLongD, Timeunit u) {returnU.tomillis (d);}Ten intExcessnanos (LongDLongm) {return0; } One},
Back to our actual usage scenario, the take method is long delay = First.getdelay (nanoseconds); -Nanoseconds.convert (Long D, Timeunit u), U.tonanos (d). If we are in the Getdelay method implementation, the second parameter of the Convert method is passed to nanoseconds, then it is returned directly D; If the second argument of the Convert method is passed in milliseconds, Then the return is Milliseconds.tonanos (d), and the result is 1000*1000*d.
It can be found that the second parameter of the Convert method is Timeunit, which is actually followed by the time unit of the first parameter d. If the implementation is directly using Time-system.currenttimemillis () as the first parameter, in fact its time unit should indeed be milliseconds, then if the second argument is passed to nanoseconds, That causes the Awaitnanos method in the Take method to shorten the wait time by 1000*1000 times, which results in a huge amount of CPU idling pressure.
Having analyzed so much, actually look at the Timeunit class in the JDK to the Convert method, it is easy to understand:
/** * Converts the given time duration in the given unit to this unit. * Conversions from Finer to coarser granularities truncate, so * lose precision. For example, converting {@code 999} milliseconds * to seconds results in {@code 0}. Conversions from coarser to * finer granularities with arguments, would numerically * overflow saturate to {@c Ode Long.min_value} If negative or * {@code long.max_value} if positive. * * <p>for example, to convert from minutes to milliseconds, use: * {@code TimeUnit.MILLISECONDS.convert (10L, timeunit.minutes)} * * @param sourceduration the time duration in the given {@code sourceunit} * @param source Unit The unit of the {@code sourceduration} argument * @return The converted duration in this unit, * or {@code Lo Ng. Min_value} if conversion would negatively * overflow, or {@code Long.max_value} if it would positively. */Public long sourceduration, timeUnit sourceunit) {throw new Abstractmethoderror (); }
It is very clear here that the second parameter of the Convert method is Sourceunit (@param sourceunit The unit of the {@code sourceduration} argument) should be the unit of time for the first parameter sourceduration. Will produce the wrong use as mentioned in the original link, it should be to understand the meaning of the conversion method parameter, that the second parameter is the time unit to be converted to the unit of time.
But this trap does a bit of a detour, in the Getdelay (Timeunit unit) method, call Unit.convert (long sourceduration, Timeunit sourceunit) method, Come out. Two timeunit variables, it is easy to be a pit if you are not careful. Of course, the problem may be avoided if the Getdelay method is implemented without a Unit.convert method.
A usage trap analysis of Delayqueue in Java