Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Trail:
Essential Java Classes
Lesson:
Doing Two or More Tasks At Once: Threads



Help on searching
is available.

Search technology
by Excite, Inc.

Reaquiring a Lock

The Java runtime system allows a thread to re-acquire a monitor that it already holds because Java monitors are reentrant. Reentrant monitors are important because they eliminate the possibility of a single thread deadlocking itself on a monitor 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()");
    }
}
Reentrant contains two synchronized methods: a and b. The first synchronized method, a, calls the other synchronized method, b.

When control enters method a, the current thread acquires the monitor for the Reentrant object. Now, a calls b and because b is also synchronized the thread attempts to acquire the same monitor again. Because Java supports reentrant monitors, this works. The current thread can acquire the Reentrant object's monitor again and both a and b execute to conclusion as is evidenced by the output:

here I am, in b()
here I am, in a()
In systems that don't support reentrant monitors, this sequence of method calls would cause deadlock.
 
Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Michel RIVEILL

Laboratoire I3S - Bât. ESSI
930 Route des Colles
06903 Sophia Antipolis CEDEX

email : riveill at unice.fr

Généralité

Projet Rainbow

Ressources en lignes

Les rubriques des cours :

 

Rechercher sur ce site avec Google

 

dernière mise à jour
le 08 juillet 2003

 

 

Réservation d'Hôtel à Prix Réduits - HotelClub