• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread problem  RSS feed

 
salih ayan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all.
In K&B book there are below sentences about synchronization.


If two threads are about to execute a synchronized method in a class, and
both threads are using the same instance of the class to invoke the method,
only one thread at a time will be able to execute the method. The other
thread will need to wait until the first one finishes its method call. In other
words, once a thread acquires the lock on an object, no other thread can
enter any of the synchronized methods in that class (for that object).



suppose we have a servlet class and in that servlet class we have a synchronized metod.Two different user at same time are accessing that servlet and want to enter that synchronized method. I mean ln case of below sentences both of them at same time must enter the synchronized method because in below explanation saying that ;

"If two threads are about to execute a synchronized method in a class, and
both threads are using the same instance of the class to invoke the method,
only one thread at a time will be able to execute the method."


especially l must worn you about "and both threads are using the same instance"

l want to ask that ;

if at same time two different user want to access this mentioned synchronized method of same servlet class , in this case server creating for each user different instance of that mentioned servlet, if server does this action in that case both of them are able to enter that synchronized method.

Am I right . if i am not please correct me
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this case server creating for each user different instance of that mentioned servlet,


Doesn't happen, only one instance of the servlet class exists normally. One of the user's request Threads will get a lock on the synchronized method.

Yes, the SingleThreadModel exists - see for example, this discussion of why it should not be used. Early thoughts on servlets inserted the SingleThreadModel - thinking it would make it easier for programmers coming from single user systems - bad bad bad idea.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!