简体   繁体   中英

how to I make sure that only one servlet(not instance) is call at a time?

I have an application which makes lot of multiple servlet calls. How do I make sure that till a particular servlet X processing is completed no other servlet instance of Y, Z is called.

A SingleThreadModel or synchronization will make sure that only one thread per servlet is called. However that may not work on more than one servlet.

Use Callback mechanism for each server calls. Onsuccess of first server call you can call second.

You could implement manual synchronization by defining your own locks.

For Example declare a static Object to use as semaphore:

public static final Object MY_LOCK = new Object();

and where you want to synchronize you could use it like that:

public void someMethod() {
    synchronized(MY_LOCK) {
        // do stuff that may not be called at same time
    }
}

public void someOtherMethod() {
    synchronized(MY_LOCK) {
        // do other stuff that may not be called at same time
    }
}

That way you can use the same Lock in different Methods (or servlets in your case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM