KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > util > ReentrantLock


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.util;
10
11 /**
12  * <p> This class represents a reentrant lock with the same semantics as
13  * built-in Java synchronized locks: Once a thread has a lock, it can
14  * re-obtain it any number of times without blocking. </p>
15  *
16  * @author <a HREF="http://gee.cs.oswego.edu/dl/">Doug Lea</a>
17  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
18  * @version 1.0, October 4, 2004
19  */

20 public class ReentrantLock {
21
22     /**
23      * Holds the owner of this lock.
24      */

25     private Thread JavaDoc _owner;
26
27     /**
28      * Holds the number of time this lock has been acquired by its owner.
29      */

30     private long _count;
31
32     /**
33      * Default constructor.
34      */

35     public ReentrantLock() {
36     }
37
38     /**
39      * Acquires the lock.
40      */

41     public void lock() {
42         Thread JavaDoc caller = Thread.currentThread();
43         synchronized (this) {
44             if (caller == _owner) {
45                 _count++;
46             } else {
47                 try {
48                     while (_owner != null) {
49                         this.wait();
50                     }
51                     _owner = caller;
52                     _count = 1;
53                 } catch (InterruptedException JavaDoc exception) {
54                     return;
55                 }
56             }
57         }
58     }
59
60     /**
61      * Acquires the lock only if it not held by another thread.
62      *
63      * @return <code>true</code> if the lock was free and was acquired by the
64      * current thread, or the lock was already held by the current
65      * thread; <code>false</code> otherwise.
66      */

67     public boolean tryLock() {
68         synchronized (this) {
69             if (_owner == null) {
70                 lock();
71                 return true;
72             } else {
73                 return false;
74             }
75         }
76     }
77
78     /**
79      * Attempts to release this lock. The lock is actually released if at
80      * least as many {@link #unlock} as {@link #lock} have been performed
81      * on this {@link ReentrantLock} by the current thread.
82      *
83      * throws IllegalMonitorStateExeception if the current thread does not hold
84      * this lock.
85      */

86     public void unlock() {
87         synchronized (this) {
88             if (Thread.currentThread() == _owner) {
89                 if (--_count == 0) {
90                     _owner = null;
91                     this.notify();
92                 }
93             } else {
94                 throw new IllegalMonitorStateException JavaDoc(
95                         "Current thread does not hold this lock");
96             }
97         }
98     }
99
100     /**
101      * Returns the thread owner of this {@link ReentrantLock}.
102      *
103      * @return the owner of this lock.
104      */

105     public Thread JavaDoc getOwner() {
106         synchronized (this) {
107             return _owner;
108         }
109     }
110 }
Popular Tags