diff --git a/xutils/src/main/java/org/xutils/common/util/ProcessLock.java b/xutils/src/main/java/org/xutils/common/util/ProcessLock.java index 64b7f4ba..9d939fb0 100644 --- a/xutils/src/main/java/org/xutils/common/util/ProcessLock.java +++ b/xutils/src/main/java/org/xutils/common/util/ProcessLock.java @@ -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) { + } } } } @@ -136,6 +138,8 @@ private static void release(String lockName, FileLock fileLock, File file, Close } IOUtil.closeQuietly(stream); + + LOCK_MAP.notifyAll(); } } @@ -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;