multithreading - How should I handle Multi-threading in Java? -


I am working on a practical scenario related to Java; A socket program. Existing systems and expecting systems are as follows.

Existing Systems - The system checks if a certain condition is satisfied, if it is, then it will send some messages that will be sent to a queue.

The queue processor is a separate thread, it periodically checks the queue for the existence of objects in it. If any item (message) is received, it only sends a message to a remote host (hardcod) and the item is queued from the queue.

Expected System - This is something that is created when a certain situation is satisfied but in every case the recipient does not equal, then there are many approaches

< Ol>
  • Entering a message in the same queue with its receiver ID. In this case the second thread can identify the receiver so that the message can be sent to it.

  • In this situation when there are many threads, the situation is satisfied and if the receiver creates a new line in "new" and puts messages in that queue and that queue A new thread starts to be processed. If the next message is directing the same recipient, then it should be kept in the same line and if there is no new line and the thread should be made.

  • Now I want to apply it, secondly, how do I slow down? A skeleton will be adequate and you will not have to worry about preparing queues etc.)

    Update: I also think that approach 1 is the best way to do this. I read some articles on threading and have come to that decision but to really know how to implement approach 2 is the value.

    First of all, if you are planning to keep a lot of receivers, I'm one-and-three -Using the Q-Per-Receiver approach you can end up with a lot of threads that are not working for most of the time and I can harm you to a wider display using an optional work threads thread pool It's been, just picking up the work from the shared queue, Selecting each task with each receiver ID, and possibly, the dictionary shared with the socket connection of each receiver for the use of working thread.

    After doing this, if you still have your approach, then what can you do:

    1) Create a new class to handle your new thread execution: < / P>

      Implementation of Public Worker Worker Runnabel {Private Quorizment & lt; String & gt; MyQueue = new line & lt; String & gt; (); Public run zero () {while (true) {string messageToProcess = null; Synchronize (myQueue) {if (! MyQueue.empty ()) {// Get your data from the queue messageprocess = myQueue.pop (); }} If (messageToProcess! = Null) {// your stuff} threads. Sleep (500); // To avoid spinning}} Public Zero Queue Message (String Message) {Synchronize (MyQ) {myQueue.add (Message); }}}  

    2) Create a message on your main thread and use a dictionary (hash table) to see if the receiver thread has already been created. If so, just a new message in the queue. If not, create a new thread, put it in the hashtable and queue a new message:

      while (true) {string msg = getNewCreatedMessage (); // You receive your message from here int id = getNewCreatedMessageId (); // You get your REC's ID from here worker w = myHash (id); If (w == zero) {// Create new worker thread = new employee (); New Thread (W) Start (); } W.queueMessage (msg); }  

    Good luck.

    EDIT: You can improve this solution by using Brian using this solution.


    Comments

    Popular posts from this blog

    python - Overriding the save method in Django ModelForm -

    html - CSS autoheight, but fit content to height of div -

    qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -