Task: Create a multi-threaded application that works correctly with shared resources and avoids deadlock situations. Any entity desiring to access a shared resource must be a thread. Use java.util.concurrent and java.util.concurrent.locks. Subject: CallCenter. There are several operators. The operator can serve simultaneously one client. In the absence of free operators, CallCenter puts calls in a queue. The customer, standing in line, can hang up and call again after some time. Implement in java.

I singled out the Client, Operator, and CallCenter classes. It is clear that there should be a queue of customers, a queue of operators, and CallCenter should serve them. It is not very clear how to arrange everything.

Question: which of these classes should be streams, which queue is better to use in my case?

  • The word "must be" is somehow superfluous. You can do without threads in this problem. Or with tasks not attached to threads, like gorutin. Or with threads. It all depends on your choice. - VladD
  • Among other things, object methods can be executed in a separate thread without the class itself being a thread. But this is probably a nagging terminology. - VladD
  • Be sure to perform it with threads - Nox
  • Oh, is this a learning task ? Then ask the lecturer, what are you asking us? Be sure to need a stream - make a decision without streams, run an empty stream with the last command, business! - VladD
  • one
    @VladD I see my joke about the dummy[42] array dummy[42] easily summarized into a pattern and will soon become a trend :) - Shamov

1 answer 1

If this is a training task for a pool of resources, it is obvious that the operator will be the resource here, because when you try to lock a client in the pool forever, he will break out, shout that he wants to go home and try to call the police.

And the CallCenter class is obviously superfluous here - after all, there are only clients and operators in the task, the call center is nothing more than an entourage. Of course, the solution will need some container for operators - but to call it better as OperatorsPool , so that it is clear what it does.

By the method of exceptions, the role of the stream remains to the client. A call to an operator is a call to a method that gets an item from the pool. Call cancellation is an interruption of a call, usually such an operation is done via CancellationToken, but you can get by with a timeout. The end of the call is the return of the operator to the pool.

  • Why is CallCenter superfluous? This may be the veiled name of the actual pool. The client pulls the CallCenter pool, he gives him an operator and waits for the operator's client to release it in order to give it to another client. Optionally, you can track the time that the operator spends on the client, and when you exceed a certain threshold, forcibly select the operator. - fori1ton
  • Because the name CallCenter does not mean anything - such classes have a bad habit of growing to divine objects. The name OperatorPool in this plan looks much better. - Pavel Mayorov