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 8573731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:19:03+00:00 2026-06-11T19:19:03+00:00

I have a number of concurrent clients – i.e. threads running and doing something

  • 0

I have a number of concurrent clients – i.e. threads running and doing something concurrently.
Each client implements a listener of some event bus.
A message from that bus can arrive to one or more clients.
My task is to broadcast that message to all the clients.

This task seems simple, but I cannot find a solution which is not ugly in some way.

(1) The straightforward solution:

void onMessageArrived(Message message) {
   broadcast(message);
}
  • is bad because each message will be broadcasted for many times because several clients can receive that message, and thus several onMessageArrived handlers can run.

(2) We can store the broadcasted messages in some list:

void onMessageArrived(Message message) {
 if (alreadyBroadcastedConcurrentMap.putIfAbsent(message)==null) {
     broadcast(message);
 }
}
  • this solves the previously mentioned problem, but many clients will repeat the useless operation of putting the already existing message into that list.

But may be there are some better options?

Java.

  • 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-06-11T19:19:05+00:00Added an answer on June 11, 2026 at 7:19 pm

    Your design contains flaws, or it is badly explained. Either you want to broadcast to all clients, or multicast to a selected list. It doesn’t make sense to specify some recipients and then make each recipient responsible for delivering it to all the other clients, handling duplications and races by hand. Just make all clients listening to the bus, possibly ignoring messages.

    I’d introduce a new central Broadcaster

    public class Broadcaster {
    
      List<Client> clients;
    
      public void broadcast(Message msg) {
        for (Client client: clients)
          if (!msg.from().equals(client))
            msg.send(client);
      }
    }
    

    Obviously each client will have to register to this broadcast domain

    public class Client {
    
      private Broadcaster broadcaster;
    
      public void join(Broadcaster broadcaster) {
        this.broadcaster = broadcaster;
        broadcaster.announce(this);
      }
    
    }
    

    and just forget about multiple delivery. You can go further and simulate a LAN by using multiple broadcast domains, if the number of clients is huge and you have other very strict requirements – which I don’t think you have

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

Sidebar

Related Questions

I have a client which makes a limited number of concurrent web requests. I
We have limit on number of concurrent sitecore users. We are facing a unique
I have a number of classes and they are quite close to each other
I have a number of classes (more than 40), each one has a number
I have a number of client devices that open socket connection exposed by a
I have number of line breaks in a string. But I only want it
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I have a number input that should trigger jQuery on every change. To do
We have a number of data objects that realize INotifyPropertyChanged to allow for WPF
I have a number of aliases defined in my .bashrc . E.g: alias ls='ls

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.