G
grz01
Hi,
I'm trying to understand this synchronized-block stuff in java...
I have a Struts Action-class with a piece of code I want to have
synchronized,
so it looks like this:
public class ProcessData extends org.apache.struts.action.Action {
private static Integer semaphore = 0;
public ActionForward execute(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// ...some code...
synchronized (semaphore)
{
// ...more code...
}
//...more code...
}
}
This works, only one client-call at a time will execute the block, and
any other client-calls will hang waiting until it has completed. So
far so good.
But what puzzles me is that --by experimentation-- if I touch the
semaphore object inside the synchronized-block it seems the lock on
the object gets released.
For example, if I put a stmt:
semaphore++ ;
as the first stmt inside the syncronized block, it appears there is no
locking anymore and 2 different client-calls can now execute the block
simultaneously.
Is this really how it's supposed to work?
Or what am I missing?
I'm trying to understand this synchronized-block stuff in java...
I have a Struts Action-class with a piece of code I want to have
synchronized,
so it looks like this:
public class ProcessData extends org.apache.struts.action.Action {
private static Integer semaphore = 0;
public ActionForward execute(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// ...some code...
synchronized (semaphore)
{
// ...more code...
}
//...more code...
}
}
This works, only one client-call at a time will execute the block, and
any other client-calls will hang waiting until it has completed. So
far so good.
But what puzzles me is that --by experimentation-- if I touch the
semaphore object inside the synchronized-block it seems the lock on
the object gets released.
For example, if I put a stmt:
semaphore++ ;
as the first stmt inside the syncronized block, it appears there is no
locking anymore and 2 different client-calls can now execute the block
simultaneously.
Is this really how it's supposed to work?
Or what am I missing?