aspose file tools*
The moose likes Servlets and the fly likes Help....SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help....SingleThreadModel" Watch "Help....SingleThreadModel" New topic
Author

Help....SingleThreadModel

Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
Hi i am trying to run a genericservlet(not httpservlet) implementing a singlethreadmodel.But everytime i make a new request i am getting new thread id.Here is the code.Can you tell me why??


[ August 28, 2008: Message edited by: vamsikrishna Gunturi ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67

Originally posted by vamsikrishna Gunturi:
Hi i am trying to run a genericservlet(not httpservlet) implementing a singlethreadmodel.

Why and why?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67

Also, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi vamsikrishna Gunturi welcome to Javaranch ,
I don't know much about the generic servlet and the single thread model.
But one thing, its not really good to eat out exceptions.
Just for the sake of curiosity, will you let me know how do you make request to generic servlet.
Thanks


SCJP, SCWCD.
|Asking Good Questions|
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
Hi Amit,
Thank you for your reply.
Actually i am learning servlets from basics.
Generic servlet is just a wrapper class of class Servlet.
It just works same as any other servlet.Just you need to inlcude it in a package and call the servlet from browser by the name given in the url mapping in web.xml.
Thanks,
Vamsi
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
I have taken the code from this book
http://books.google.co.in/books?id=ZFplJ5Sjo2oC&pg=PA42&lpg=PA42&dq=extends+GenericServlet+implements+SingleThreadModel&source=web&ots=1sNbv0MLF6&sig=9LfeUzK7lAlXfMxZSYVJnrZpWzs&hl=en&sa=X&oi=book_result&resnum=7&ct=result#PPA37,M1
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42591
    
  65
Originally posted by vamsikrishna Gunturi:
But everytime i make a new request i am getting new thread id.

Why does that surprise you? What did you expect?


Ping & DNS - my free Android networking tools app
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
When i open the same servlet in three different browser windows and run them one after another quickly,i expect that each counter is unique,but i am getting same counter for all..
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
when i implemented a singlethreadmodel interface , the servlet container need to reuse the same thread for multiple requests to same servlet.But it is creating a new thread for every request.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

i expect that each counter is unique,but i am getting same counter for all..

If the single thread model is really working then declaring the counter variable as a instance variable instead of a method variable should solve your problem.

Hope this helps
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42591
    
  65
when i implemented a singlethreadmodel interface , the servlet container need to reuse the same thread for multiple requests to same servlet.

No. If you use STM you're asking the container not to have two request threads use the same servlet instance at the same time, not to use only a single thread.
But it is creating a new thread for every request.

That's indeed how it should work.
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
hi..i do not know what was the problem?The same servlet is worikng fine in firefox as expected , but creating problems in internet explorer.

To Dittmer,
It is using same thread(not creating new) and same instance when used in firefox and is now just working as i expected.

Anyhow thank you for your support.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
When i open the same servlet in three different browser windows and run them one after another quickly,i expect that each counter is unique,but i am getting same counter for all..


The counter variable is local to the service method which explains the behavior you are noticing.

In order to get unique counter values, you just have to declare the counter as an instance variable of SingleThreadedServlet2.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
...the servlet container need to reuse the same thread for multiple requests to same servlet.But it is creating a new thread for every request.


The mechanism of request dispatch to a service thread by the Container is completely container implementation specific.

There's no way for the servlet developer to instruct the container to do that.

The container "may" end up reusing the same thread,assuming it's using a Thread Pool,to service different requests.But then the servlet developer has no control on that.
[ August 29, 2008: Message edited by: Ajay Saxena ]
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
Hi Ajay,

I have taken the code from this book
web page

Please go through 5 pages from page 37.
Here to prevent simultaneous access to a common external resource,(i.e in order to prevent any uncommitted reads) we are trying to use a SingleThreadModel.
Well , i am not bothered about whether the container is using the same thread or not..but i am bothered why it is allowing multiple threads to access same resourse even though when i mentioned that it has to implement SingleThreadModel..
one more thing to add..My IE7 is behaving the same unexpected way even if i make the counter varaible as instance variable...
please try to run the servlet and analyse the output in console...
hope you got my question..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42591
    
  65
"vamsi", why did you change a valid display name to an invalid one?. Please reverse that change before your next post; accounts with invalid display names are generally closed quickly.

why it is allowing multiple threads to access same resourse even though when i mentioned that it has to implement SingleThreadModel..

You're misunderstanding what STM is supposed to do. Nowhere does it say that multiple threads can't run the same servlet instance. In fact, that is to be expected. The only guarantee that's given is that multiple threads won't do so at the same time.
[ August 29, 2008: Message edited by: Ulf Dittmer ]
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
Hi Dittmer,
you said
The only guarantee that's given is that multiple threads won't do so at the same time.

but i was also asking the same...but forgot add 'at the same time' in my previuus post...so..now my question become..
why it is allowing multiple threads to access same resourse at the same time even though when i mentioned that it has to implement SingleThreadModel.. ..
If possible some one run this servlet in Internet explorer only(preferably 7)..and observe the result..It is allowing to access same resourse for more than one thread at the same time
Thanks..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42591
    
  65
It is allowing to access same resourse for more than one thread at the same time

How do you know that? What exactly are you doing that leads you to conclude this? (People are unlikely to take the time to run the code themselves, so we rely on you telling us the details of what's happening.)
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
well....i tried to run the servlet from three different windows of Internet explorer...i have typed in the url in all three windows and when ready..started hitting "go" in all three of them one by one with a time lag of around 2 seconds...
so as per my code..when first request hits the server..it reads the file from data and increments it but before it could update the incremented value into the new file it is forced to sleep for 6 seconds..during these 6 seconds the second request hits the server...as per STM..the server should not allow the second thread to read the uncommitted value(i.e say when the first request is hit say the count is 5..then it should be 6 for second thread....but it is still 5 as the first thread is sleeping) from the file till the first thread completes it's execution..
so i am getting same value 5 in all outputs..and in console i can see that the thread ids are not unique...
so i concluded that multiple threads are created and simultaneous access was allowed...actually i should get outputs as 6 , 7 and 8..but getting 6 6 and 6...
hope i made it clear..
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
..as per STM..the server should not allow the second thread to read the uncommitted value


Where on earth did you come across the above piece of information?
Vamsi Krishna
Greenhorn

Joined: Aug 28, 2008
Posts: 10
Ajay,
if you have patience you can refer to book that was clearly explaining the problem and resolution ..i gave the link in my previous replies....just click on it when you are free and go through it..
Thanks,
vamsi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help....SingleThreadModel