|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The same thread can call a synchronized method on an object for which it already holds the lock, thereby reacquiring the lock. The Java runtime environment allows a thread to reacquire a lock because the locks are reentrant. Reentrant locks are important because they eliminate the possibility of a single thread�s waiting for a lock that it already holds.Consider this class:
public class Reentrant { public synchronized void a() { b(); System.out.println("here I am, in a()"); } public synchronized void b() { System.out.println("here I am, in b()"); } }Reentrantcontains two synchronized methods:aandb. The first,a, calls the other,b. When control enters methoda, the current thread acquires the lock for theReentrantobject. Now,acallsb; becausebis also synchronized, the thread attempts to acquire the same lock again. Because the Java platform supports reentrant locks, this works. In platforms that don�t support reentrant locks, this sequence of method calls causes deadlock. The current thread can acquire theReentrantobject's lock again, and bothaandbexecute to conclusion, as is evidenced by the output:here I am, in b() here I am, in a()
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.