Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurency Question

 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All;

I am having trouble grasping this concept.

Guy "A" accesses a servlet. The servlet get an instantiates a class which is a singleton and than runs some code. The code takes about 7 minutes to execute.

2 minutes later Guy "B" calls the same servlet, also requesting a singleton instance, wanted to run the same code.

I am guessing that the Guy B's application will hang while it waits to get a reference to the Singleton.

Does this sound right?

Basically I have want to ensure a single expensive process can only happen one request at a time for all session, otherwise if everyone logs on and does it the server will grind to a halt. Make sense?

Thanks,

Luke
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to synchonize that method (or part of it).
 
Robin Collier
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization will keep the code from processing multiple times concurrently, but only in favor of doing it consecutively. Is this the behaviour you are looking for?

This also has the possibility of using up all the threads allocated to respond to requests, since the threads are blocked for such a lengthy amount of time. (If you have 20 threads in your pool and 20 requests arrive within the 7 minutes - there would be threads left to process new requests)
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for consecutive execution (never concurrent). I want to avoid all threads being consumed by this. Right now I have the class in question in a singleton with the intensive method synchronized.

From the sounds of it this may not be enough. Or is it?

Luke
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am guessing that the Guy B's application will hang while it waits to get a reference to the Singleton.


Yes if thread A has acquired object lock then thread B has to wait. But if thread A has yield the lock then no problem.

But what kind of processing you are doing which takes 7 minutes.
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The process is composing and sending emails out to a collection of contacts.

In some cases there are 1000, others just 5.

7 minutes was a worst case scenario.

Luke
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometime complex report generation can take many hours, I do not tell about complex mathematical tasks requiring many days to finish.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the sounds of it this may not be enough. Or is it?


If the entire method is synchronized, then that should be enough. You should really think about letting the user know that the server is doing some intensive work, and didn't just hang or something. Some users could decide that something went wrong and start refreshing the page - which depending on how your code works might add duplicate intensive requests to the queue.

-Yuriy
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to count the number of queued requests for this proecess and deny any if the queue has more than n requests going.
[ August 26, 2005: Message edited by: Ben Souther ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this was my problem I would consider having a request create an object that encapsulates everything needed to do the job and adding the job object to a queue that a separate Thread (or maybe even a separate application) would work on. That way you can respond to the request with a "your job has been added" message. Subsequent requests could tell the user the status of the job.
Bill
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I was thinking about using Quartz for this (Quartz). The problem is this project is long over due and I am reluctant to add new complexity at this point. I just want to easiest/simplest solution to deal with this. So I am thinking of making it a Singleton and synchronizing the big method. Also I will add a progress bar to the page so when users submit a request and there is a delay they will know the request is being processed. Does this sound reasonable?
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can do that.
But implementing quartz will take same time to do coding for displaying bar.

Quartz will not add any complexity, even it makes your work simple.
 
te zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First , we have to know about the web server, which is the
container of the servlet .
the container make the servlet support multithreading ,so even the
servlet is singleton, the instance also can be used by many users simultaneously.
if you want to test the singleton, try to write a simple class,but not
the Servlet.
it's my suggestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic