dcsimg
How can i lock a method.<
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Elamathi_Sambandam
Posted On:   Tuesday, September 18, 2001 08:13 AM


How can i lock a method.

I want only one thread to execute a

method. Other threads have to wait till
the first one gets completed.

Synchronized locks only the object not the
method. Is it advisable to go for static synchronized method

Suggest some ideas

Re: How can i lock a method.<

Posted By:   Alex_Chaffee  
Posted On:   Wednesday, September 26, 2001 03:11 PM

Actually, synchronized locks only the method, not the code. Read the docs more carefully. Using synchronized will do exactly what you want, really, I promise.

Re: How can i lock a method.<

Posted By:   Sean_Ruff  
Posted On:   Tuesday, September 18, 2001 01:38 PM

One solution would be to create a generic static object for the Class to serve as a lock then synchronize on the lock after entering the method, as such:


public class MyClass {

private static final Object lock = new Object();

//Constructors
//Other Methods

private void myMethod(){
synchronized(lock){
//SYNCHRONIZED CODE HERE
}
}
}
About | Sitemap | Contact