Skip to content

Commit

Permalink
update: ProcessLock
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Jun 16, 2020
1 parent 86b5d2c commit 94f0c84
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions xutils/src/main/java/org/xutils/common/util/ProcessLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ public static ProcessLock tryLock(final String lockName, final boolean writeMode
ProcessLock lock = null;
long expiryTime = System.currentTimeMillis() + maxWaitTimeMillis;
String hash = customHash(lockName);
while (System.currentTimeMillis() < expiryTime) {
lock = tryLockInternal(lockName, hash, writeMode);
if (lock != null) {
break;
} else {
try {
Thread.sleep(1); // milliseconds
} catch (InterruptedException iex) {
throw iex;
} catch (Throwable ignored) {
synchronized (LOCK_MAP) {
while (System.currentTimeMillis() < expiryTime) {
lock = tryLockInternal(lockName, hash, writeMode);
if (lock != null) {
break;
} else {
try {
LOCK_MAP.wait(10);
} catch (InterruptedException iex) {
throw iex;
} catch (Throwable ignored) {
}
}
}
}
Expand Down Expand Up @@ -136,6 +138,8 @@ private static void release(String lockName, FileLock fileLock, File file, Close
}

IOUtil.closeQuietly(stream);

LOCK_MAP.notifyAll();
}
}

Expand Down Expand Up @@ -210,6 +214,8 @@ private static ProcessLock tryLockInternal(final String lockName, final String h
IOUtil.closeQuietly(stream);
IOUtil.closeQuietly(channel);
}

LOCK_MAP.notifyAll();
}

return null;
Expand Down

0 comments on commit 94f0c84

Please sign in to comment.