|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java runtime system allows a thread to re-acquire a lock that it already holds because Java locks are reentrant. Reentrant locks are important because they eliminate the possibility of a single thread deadlocking itself on 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 synchronized method,a, calls the other synchronized method,b.When control enters method
a, the current thread acquires the lock for theReentrantobject. Now,acallsband becausebis also synchronized the thread attempts to acquire the same lock again. Because Java supports reentrant locks, this works. The current thread can acquire theReentrantobject's lock again and bothaandbexecute to conclusion as is evidenced by the output:In systems that don't support reentrant locks, this sequence of method calls would cause deadlock.here I am, in b() here I am, in a()
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.