Question

I have a question regarding Java threading. This is my scenario:

  • 2 different people will receive a multiquestion poll in their mobile phones.
  • When the questions is sent, the server stays in a wait() status until the answer is received. Then, a notify() signal is sent for the poll to continue, until all questions are sent.
  • User 1 could be answering question 3 while User 2 is still answering question 1.

My approach so far has been to implement a new ChainedPollThread thread. Anyway, by this way, if User 1 answers question 1 both Users 1 and 2 will receive the second question.

Do I need to create a new ChainedPollThread for each user?

If the number of users increments, will this mean I need to create, for example, 100 threads?

Which is the appropiate way to implement what I want to achieve?

Thanks in advance.

Was it helpful?

Solution

The way multithreading goes is that you'll need one thread for each participant.

The way you implemented this now means that as soon as one person answers a question, the whole state of your server-side program is advanced to question two. Having one thread (and thus, if you will, one poll-program) for each of the participants will aleviate the problem.

To reduce server-load due to unnecessary amounts of threads, use thread-pooling (see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html). A thread pool re-uses existing threads once they are not needed any more, thus trying to keep the size of active threads to a minimum. (Creating the threads is the expensive part) If more are needed, the pool grows to a specified maximum, if less are needed it shrinks to a specified minimum.

On the other hand, that whole model has a few dangers: What if a person doesn't answer at all? will there be a timeout on the server? Will it wait forever?

One option might be to install a software / framework on the mobile devices, that is capable of receiving all the questions and possible answers to a poll and present them one after the other on the screen. As soon as the participant presses "finish" the set of chosen answers is sent back to you. This way your server would not need to hold threads for each participant, just receive one string with all the answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top