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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:45:41+00:00 2026-05-24T08:45:41+00:00

I am working on an application, that processes incoming messages. I am not proficient

  • 0

I am working on an application, that processes incoming messages. I am not proficient in java multithreading and I am asking your help, folks. Is there anything wrong with the following app structure.

There is main application class with stopRequested boolean field. And there is internal runnable class that listens for incoming messages and process them. Also there is another thread that sets stopRequested to true.

Is this approach working and reliable, or I am wrong?

Below there is a part of my code:

class ApplicationClass {

    // we set this var in another thread
    // when it is necessary to stop
    private stopRequested = false;


    public ApplicationClass() {

        // starting message processing thread
        (new Thread(new MessageProcessing())).start();

    }


    private class MessageProcessing implements Runnable {


        public void run() {

            while (!stopRequested) {

                if (getNewMessagesCount() > 0) {

                    processNewMessages();

                }

            }

        }
    }       
} 

Thank you.

  • 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-24T08:45:42+00:00Added an answer on May 24, 2026 at 8:45 am

    There are a few things to think about.

    1. As sbridges noted stopRequested needs to be volatile to resolve visibility problems (a thread on another core may not see the change otherwise).
    2. If getNewMessagesCount() doesn’t block then your while loop will spin and consume the core; this will give you the lowest latency but ties up the entire core.
    3. The code you’ve listed appears to be a simple processing queue; you’re likely going to be better off going with an ArrayBlockingQueue.
    4. It’s dangerous to start a new thread from a constructor. The thing to worry about is what happens if getMessageCount() and processNewMessages() are invoked before ApplicationClass is finished being created. Since the instance of ApplicationClass could be in an incomplete state you could find a rather nasty bug. (For the same reason you never want to have your code subscribe as a listener to events from a constructor, by the way.) Check out Effective Java for more background on this topic.
    5. Your while loop should check if the current thread has been interrupted so that it places nice; it should be while (!stopRequested && !Thread.currentThread().isInterrupted())

    Writing correct concurrent programs is hard. I highly recommend reading Java Concurrency in Practice; it will save you a lot of pain.

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

Sidebar

Related Questions

I'm working on a TCP based application that processes bitpacked messages, meaning: The messages
I'm working on an application that processes audio data. I'm using java (I've added
I am working on an application that processes a large number of files placed
I am currently working on an application that launches separate processes which display additional
I am working on a very large application that has multiple processes running simultaneously;
I'm working on an application that may generate thousands of messages in a fairly
I am currently working on a WPF application that processes data in a database
Problem Background I am currently working on a camel based ETL application that processes
I'm currently working in a dispatcher service that processes thousands of messages delivered in
I'm working on an application that uses multiple threads to process its data. The

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.