Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.libg.filelock; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | public class DirectoryLock { |
| 6 | final File lock; |
| 7 | final long timeout; |
| 8 | final public static String LOCKNAME = ".lock"; |
| 9 | |
| 10 | public DirectoryLock(File directory, long timeout) { |
| 11 | this.lock = new File(directory, LOCKNAME); |
| 12 | this.lock.deleteOnExit(); |
| 13 | this.timeout = timeout; |
| 14 | } |
| 15 | |
| 16 | |
| 17 | public void release() { |
| 18 | lock.delete(); |
| 19 | } |
| 20 | |
| 21 | public void lock() throws InterruptedException { |
| 22 | if (lock.mkdir()) |
| 23 | return; |
| 24 | |
| 25 | long deadline = System.currentTimeMillis()+ timeout; |
| 26 | while ( System.currentTimeMillis() < deadline) { |
| 27 | if (lock.mkdir()) |
| 28 | return; |
| 29 | Thread.sleep(50); |
| 30 | } |
| 31 | } |
| 32 | } |