-1

I am working on java web application. I calling servlet by using web services from android device. When i tried to call servlet using web service from 10 to 15 devices at a time, just 2-3 devices got response remaining all applications on device are crashed. Because my servlet is by default multithreaded its failed to process all requests.

For this i tried implementing SingleThreadModel this will work successfully with 10-15 devices, but for small number of requests it ok. But for 1000 to 10000 devices its not practical to implement SingleThreadModel because it process only single request at a time meanwhile all remaining requests must have to wait in queue.

So any one suggest me any other solution other than implementing SingleThreadModel which will degrade performance of my application.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Aniket
  • 2,204
  • 5
  • 34
  • 51
  • 3
    *Because my servlet is by default multithreaded its failed to process all requests.* This is **completely nonsense**. Please provide the implementation of your servlet so we can guide you in the right direction. – Luiggi Mendoza Dec 18 '14 at 14:31
  • 2
    the solution is simple, make your servlet implementation multi-thread safe. – jtahlborn Dec 18 '14 at 14:35
  • As @jtahlborn said, you need your servlet to be thread safe. You can get some info about thread safe in this post: http://stackoverflow.com/questions/2239981/in-java-how-do-i-make-sure-my-web-application-is-thread-safe. BTW, SingleThreadModel is deprecated. – David SN Dec 18 '14 at 14:43
  • 1
    @DavidSN There are more explanations on the subject (all by BalusC): http://stackoverflow.com/q/3106452/1065197 http://stackoverflow.com/q/2183974/1065197 – Luiggi Mendoza Dec 18 '14 at 14:45
  • @jtahlborn at this moment servlet implementation multi-thread safe is ok. But in future there may hit multiple (thousands) requests to servlet because it is web services application accessed from multiple devices. In that case each request processed separately, so remaining requests must wait in queue. Which is will take more time to process request which is not feasible/practical. – Aniket Dec 20 '14 at 11:12

1 Answers1

0

IMHO, Container creates only one object for each servlet and then uses this object to server multiple requests by spawning thread for each.

  1. Since your servlet is successfully serving multiple request, there is a chance that the container is actually restricting number of request at a time, which you can configure.
  2. Servlets that make database transactions, sessions objects manipulation etc need to be implemented via synchronization to secure data from corruption and miss information.
  3. There is something we need to be careful here, the synchronization should be done on as minimal statements/method as possible so that the other threads don't have to wait long. There is possibility that the browser has decided that server is unresponsive, in case the request is waiting too long because of large synchronized code, and the request is crashing.

Correct me if I am wrong.

saikumarm
  • 1,565
  • 1
  • 15
  • 30
  • *Servlets that make database transactions, sessions objects manipulation etc need to be implemented via synchronization to secure data from corruption and miss information* if you do this then your multi threading environment will behave as single threaded environment since a thread must wait to obtain the lock on a shared synchronized resource. You should avoid synchronization at most. – Luiggi Mendoza Dec 18 '14 at 15:37
  • By the way, all this is greatly and explained with much detail by BalusC in three answers linked in comments. Since OP hasn't provided how he/she implemented his servlet, it's hard to tell where the problem is. – Luiggi Mendoza Dec 18 '14 at 15:39
  • 1
    @LuiggiMendoza yes, but it does make save us from data corruptions or miss information like wrong bank balance information. Synchronization is unavoidable in such a case. But, one should be always try to avoid for the sake of Multi threading. – saikumarm Dec 18 '14 at 15:40
  • *but it does make save us from data corruptions or miss information like wrong bank balance information* not if you declare all your operations atomically, so there's no need to have these problems at all. – Luiggi Mendoza Dec 18 '14 at 15:41
  • @LuiggiMendoza okay. I will look at BalusC answers as you said. – saikumarm Dec 18 '14 at 15:43