I am having trouble making a servlet thread-safe using synchronization.

I do know it's best to avoid instance (state) variables in servlets, but my college assignment calls strictly for this.

My servlet has an instance variable, shared among any number of threads that run. I need to use synchronization such that modification of this variable is only allowed to one thread at a time. I then need to test the effects of synchronization employed by using a Thread.currentThread().sleep(10000) delay timer within the synchronized(){} block.

- The instance variable is of type double, so a number.
- I hence can't use it as monitor, as it's not an object.
- I hence set up synchronization blocks around code lines that modify this variable.
- The syntax I used for these synchronization blocks is as follows:

Code:
synchronized(this)
    {
    // Actions to perform with the instance variable.
    }
I researched a lot about servlets in this regard. My findings are the following:

- using the "this" word as synchronization object does not work, because "this" is its own "this" for each thread of the servlet running. So if 4 users have simultaneously the servlet open in their browsers and are using it, that means there are 4 instances of the servlet running and using "this", each instance simply refers back to itself -> meaning it is as if there is no synchronization condition anyway. For synchronization to work, all instances must lock on one single variable.
- Another finding states that there is always only one instance of a servlet running, and the multiple simultaneous users means multiple threads. But there is only one instance of the servlet.

This is conflicting information.

Please help: what should I do, how should I code, to synchronize operations using this instance variable, such that each next user (thread) must wait for the first thread to release the lock, before it can perform the operation???

There were suggestions as follows:

1) Construct a new object -> Object lock = new Object();
2) Use synchronization on the desired instance variable, using the object as a "dummy" reference -> synchronize(lock){// Code with variable}.

This still did not work. Currently the servlet behaves as if there are no synchronized blocks at all. Help is hugely appreciated!