File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Concurrent access to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Concurrent access to servlet" Watch "Concurrent access to servlet" New topic
Author

Concurrent access to servlet

Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
Hello,
i am reading through Java concurrency in practice, I found it very interesting but also sometimes not easy to understand. I have a question about following code:


This code increments counter but not in synchronized manner. Now i am trying to simulate concurrent access, using following client:

I ran this client in main method, see following code snippet.

and I got following output:


What I am trying to do is to simulate bad state of things, when counter is not synchronized and clients do not get values of counter in correct order. Expected result would be ordered sequence of numbers, starting with zero and incremented by one. Instead I got following output from my main method. My question is: Am I doing a correct simulation of unsynchronized counter and are my results expected (not ordered sequence)?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The way you have written lines 13 and 14 in the Client class, you can't even guarantee that all of the Clients will have different names. Consider this possibility:

Client A increments "instanceCounter", making it 1.

Client B increments "instanceCounter", making it 2.

Client B uses "instanceCounter" to construct its name, which becomes "Client-2".

Client A uses "instanceCounter" to construct its name, which also becomes "Client-2".

And based on what I see in your output, that might actually be happening.
Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
I modified Client class in following way - I added nextID() method.



Client class now looks like this:


Is the Client name generation correct now? And is my simulation correct?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Boris Belovic wrote:Is the Client name generation correct now?


I really don't know. It's less obviously wrong, but I'm not an expert on threading issues.

And is my simulation correct?


If you didn't change the servlet, then no. I'm surprised you didn't notice that it has precisely the same problem as the one I pointed out with your Client class.
Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
The problem with servlet is that the counter integer isn't synchronized. I know it,my question was whether my simulation of broken servlet behaviour is correct. I know the fix is to synchronize doGet() method or use AtomicInteger. I was asking on how to simulate concurrent access to servlet with unsynchronized counter.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Oh, I see. Then I don't understand what you mean by "correct" when you ask whether your simulation is correct. Your code is certainly connecting to the servlet and receiving a number from its response. So if that's what you wanted to do, then it's correct. If you had some other requirements, then you would have to ask whether they were satisfied. But I can't see any other requirements in your posts.
Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
Basically, I wonder how the output of main() changes when I synchronize servlet. Now I am getting unordered sequence, which is probably expected. But would the sequence be ordered if I synchronize servlet? I tried to synchronize doGet() (using synchronized keyword) but I am still getting unordered sequence of numbers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It's just the same thing over again. You read data from the servlet and then you write it to System.out. This is not an atomic unit of work and so it's vulnerable to exactly the same process that I described in my first reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And also, connecting to the servlet and getting its response is far from atomic. On the other hand you don't want to synchronize that, because the main point of this exercise is to make simultaneous requests to the servlet.

So if you were expecting this test application to determine whether the servlet was going to give out the numbers from 1 to N in sequence or not, I don't believe this code can test that. And I don't see how it can be made to test that either.
Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
Thanks for explanation Paul. I think it over and I think you are right. I am just trying to get into threads and synchronization and understand both topics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent access to servlet