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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:29:18+00:00 2026-06-11T09:29:18+00:00

I have 2 classes. One (A) collects some data and the other (B) sends

  • 0

I have 2 classes. One (A) collects some data and the other (B) sends the data to TCP/IP clients. The process is asynchronous with refresh rates from nearly zero to a few seconds.
Note that this application has no GUI so I won’t be able to use many built in “onChange” listeners.

In normal conditions I would simply write the code so that A calls a “send” method on B, passing the data, no problems here.

Now, assume that the rate A collects data is critical (real time) and that A cannot wait for B to complete the sending process (note that B uses TCP, not UDP). The way I implemented this is

  • A places the data in a field in B
  • B has a continuous loop that checks if the data is new or now. If new, it will send it out.

If during the send the data is updated a few times it doesn’t matter, as long as it doesn’t slow down A.
Spawning a new thread for each send would in principle not slow down A but it’s likely gonna result in a mess.

You can see that B is working in synchronous mode (but A isn’t) and it’s implemented with a while loop with a Thread.sleep() call. My questions are:

  1. Should I use a timer task instead of the while loop? I know that most people hate the Thread.sleep() call but ultimately the only thing I’m interested is in keeping CPU low.

  2. Isn’t there a more elegant way than the synchronous approach? In some cases the data refresh of A is about 1 second and it would be nice if I could just have a listener that would act on an event. In such case a sleep time of 25ms would be a waste of cycles. In other cases it’s very fast and I’d like no sleep at all.

*Example: imagine that A is submitting screenshots from your screen and B is sending them to the clients. Only the last one matters and B is gonna go as fast as possible *

Any ideas or suggestions? Please keep things as simple and low cpu as possible

thanks a lot!

  • 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-11T09:29:19+00:00Added an answer on June 11, 2026 at 9:29 am

    I would make it like this:

    A collects the data in whatever fashion is appropriate and then post the “next message” to send. If there is already a message pending, let the new message replace / update the previous.

    B checks for ay pending messages, if one is available it grabs it and send it to the client(s). However, if no message is pending, then B waits for one to be available.

    Object lock = new Object();
    Object pending = null;
    
    public void post(Object message) {
        synchronized (lock) {
            pending = message;
            lock.notifyAll();
        }
    }
    
    public Object getNextMessage() {
        Object message;
        synchronized (lock) {
            while (pending == null) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // Ignore
                }
            }
            message = pending;
            pending = null;
        }
        return message;
    }
    

    Using a queue you could instead do

    BlockingDeque<Object> queue = new LinkedBlockingDeque<Object>(1);
    
    public void postMessage(Object message) {
        // If previous message is still pending we replace it.
        queue.clear();
        queue.offer(message);
    }
    
    public Object getNextMessage() {
        while (true) {
            try {
                return queue.take();
            } catch (InterruptedException e) {
                // Ignore interrupts
            }
        }
    }
    

    Of course in both example it would be good to instead of the while (true) use a signal so you can gracefully shut down.

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

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I have two classes, one that inherits from the other. The base class is
I have two classes (MVC view model) which inherits from one abstract base class.
I have some classes that, for one reason or another, cannot be or need
In one of my projects, I have some classes that represent entities that cannot
I have three classes one is a JFrame and other two are JPanel .
I have 2 classes one includes methodA and the other include methodB. So in
I have two classes one the Driver and the other one called FilterOut. In
I have two classes with one extending the other. They both have a method
I have two classes one is derived from CheckBoxList and the second one from

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.