• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help....SingleThreadModel

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Why and why?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vamsi Krishna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Vamsi Krishna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

..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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic