Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 193995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:33:27+00:00 2026-05-11T16:33:27+00:00

I am working on a practical scenario related with Java;a socket program. The existing

  • 0

I am working on a practical scenario related with Java;a socket program. The existing system and the expected system are as follows.

Existing System – The system checks that a certain condition is satisfied. If so It will create some message to be sent and put it into a queue.

The queue processor is a separate thread. It periodically check the queue for existence of items in it. If found any items (messages) it just sends the message to a remote host (hardcoded) and remove the item from queue.

Expected System – This is something like that. The message is created when a certain condition is satisfied but in every case the recipient is not same. So there are many approaches.

  1. putting the message into the same queue but with its receiver ID. In this case the 2nd thread can identify the receiver so the message can be sent to that.

  2. Having multiple threads. In this case when the condition is satisfied and if the receiver in “New” it creates a new queue and put the message into that queue. And a new thread initializes to process that queue. If the next messages are directed to same recipient it should put to the same queue and if not a new queue and the thread should be created.

Now I want to implement the 2nd one, bit stucked. How should I do that? A skeleton would be sufficient and you won’t need to worry to put how to create queues etc… 🙂

Update : I also think that the approach 1 is the best way to do that. I read some articles on threading and came to that decision. But it is really worth to learn how to implement the approach 2 as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:33:27+00:00Added an answer on May 11, 2026 at 4:33 pm

    First of all, if you are planning to have a lot of receivers, I would not use the ONE-THREAD-AND-QUEUE-PER-RECEIVER approach. You could end up with a lot of threads not doing anything most of the time and I could hurt you performance wide. An alternative is using a thread pool of worker threads, just picking tasks from a shared queue, each task with its own receiver ID, and perhaps, a shared dictionary with socket connections to each receiver for the working threads to use.

    Having said so, if you still want to pursue your approach what you could do is:

    1) Create a new class to handle your new thread execution:

    public class Worker implements Runnable {
       private Queue<String> myQueue = new Queue<String>();
       public void run()
       {
           while (true) {
              string messageToProcess = null;
              synchronized (myQueue) {
                 if (!myQueue.empty()) {
                     // get your data from queue
                     messageToProcess = myQueue.pop();
                 }
              }
              if (messageToProcess != null) {
                 // do your stuff
              }
              Thread.sleep(500); // to avoid spinning
           }
       }
       public void queueMessage(String message)
       {
          synchronized(myQueue) {
             myQueue.add(message);
          }
       }
    }
    

    2) On your main thread, create the messages and use a dictionary (hash table) to see if the receiver’s threads is already created. If is is, the just queue the new message. If not, create a new thread, put it in the hashtable and queue the new message:

    while (true) {
       String msg = getNewCreatedMessage(); // you get your messages from here
       int id = getNewCreatedMessageId();   // you get your rec's id from here
       Worker w = myHash(id);
       if (w == null) {   // create new Worker thread
          w = new Worker();
          new Thread(w).start();
       }
       w.queueMessage(msg);
    }
    

    Good luck.

    Edit: you can improve this solution by using BlockingQueue Brian mentioned with this approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 135k
  • Answers 135k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Depending on what you are doing, you could consider having… May 12, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer Excerpts from Rails 5 documentation: Cookies are read and written… May 12, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer Ok, found it using Reflector, looks like its this: bool… May 12, 2026 at 7:00 am

Related Questions

A question I have been thinking about for a while - would Stackoverflow users
I am working on both parts of a data layer. I get to create
I am working on an ASP.NET MVC application that contains a header and menu
I'm struggling to create a generic (or untyped) array in C (I'm aware that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.