1313import java .util .concurrent .TimeUnit ;
1414import java .util .concurrent .locks .Condition ;
1515import java .util .concurrent .locks .Lock ;
16+ import java .util .concurrent .locks .LockSupport ;
1617import java .util .concurrent .locks .ReentrantLock ;
1718
1819/**
@@ -39,25 +40,15 @@ String tryRedisLock(String lockId,long time, TimeUnit unit) {
3940 final long startMillis = System .currentTimeMillis ();
4041 final Long millisToWait = (unit != null ) ? unit .toMillis (time ) : null ;
4142 String lockValue =null ;
42- ReentrantLock lock =new ReentrantLock ();
43- try {
44- lock .lock ();
45- Condition reTryCondition = lock .newCondition ();
46- while (lockValue ==null ){
47- lockValue =createRedisKey (lockId );
48- if (lockValue !=null ){
49- break ;
50- }
51- if (System .currentTimeMillis ()-startMillis -retryAwait >millisToWait ){
52- break ;
53- }
54- reTryCondition .await (retryAwait , TimeUnit .MILLISECONDS );
43+ while (lockValue ==null ){
44+ lockValue =createRedisKey (lockId );
45+ if (lockValue !=null ){
46+ break ;
5547 }
56- } catch (InterruptedException e ) {
57- log .error (e .getMessage (),e );
58- Thread .interrupted ();
59- }finally {
60- lock .unlock ();
48+ if (System .currentTimeMillis ()-startMillis -retryAwait >millisToWait ){
49+ break ;
50+ }
51+ LockSupport .parkNanos (TimeUnit .MILLISECONDS .toNanos (retryAwait ));
6152 }
6253 return lockValue ;
6354 }
@@ -130,4 +121,10 @@ private String randomId(int size) {
130121 }
131122 return new String (cs );
132123 }
124+
125+ public static void main (String [] args ){
126+ System .out .println (System .currentTimeMillis ());
127+ LockSupport .parkNanos (TimeUnit .MILLISECONDS .toNanos (300 ));
128+ System .out .println (System .currentTimeMillis ());
129+ }
133130}
0 commit comments