Original question:
My question is if I have a web resource, says a JSP page, and I'd like to give priority to certain user to access the page.
i.e. paid user will have faster access than anonymous user
How can I achieve it?
Suggested solution:
How about setting the priority of the thread executing the request:
Well, it is hard to know what the original user wanted, but I'm not so sure this thread-priority idea will work very well.
First of all, my general rule of thumb for multithreaded programming is "Don't use thread priorities to control execution speed." That's because the Thread spec is deliberately vague about scheduling algorithms (to allow for OS-specific optimizations). In practice, many schedulers do NOT follow the practice that all OS's follow for user priorities, namely that all threads make SOME progress. Many just run higher-priority threads in lieu of lower-priority ones, except when the higher-priority ones are waiting on I/O. So, increasing a thread's priority could result in it taking over the whole server. Decreasing it could prevent it from ever running at all. Of course, I'm being paranoid here since there is lots of I/O going on. Still, on a heavily-loaded server, this could be a problem: the unpaying customers might NEVER run.
Second, what if the load is light? The original user didn't say, but I got the impression that unpaid users should still run slowly. With Thread priorities, they'd run just as fast when NO higher-priority threads were around.
Third, in many apps, the only thing that
really matters is how long the database call takes. So, slowing down or speeding up the main thread wouldn't make any difference if the database call takes the same amount of time.
So, if that behavior is really what you want, maybe just do a few small Thread.sleep calls in the unpaid user's response? A Filter could apply this behavior universally without putting that logic in each page separately.
Cheers-
- Marty